DEV Community

Cover image for How I do Git version control - locally (List of commands)

Posted on

How I do Git version control - locally (List of commands)

Hi guys, I wanted to share with you how I do version control locally.

Git can help you track the changes on your project. At anytime you can go back and check out a previous version. It allows you to create new branches of your app so you can try out new things, and if you don't like how the new features turned out, you don't have to commit those changes to the master branch.

First install Git on your computer. Create a project folder. Open Git Bash, type the path to the project folder. (I'm sure there are other ways to do it, this is just how I like to do it)

$ cd Desktop/tutorial/ 
  • To initialize an empty Git repo in that folder
$ git init
  • Create a file in the repo
$ touch index.html
  • Add a file that should be tracked
$ git add index.html

You can also add multiple files, just type

$ git add .

And it will start tracking all the files in the folder.

  • To check the files in the folder
$ ls -a
  • Check the status of the files in the repo. To see which files have been modified
$ git status
  • Commit the changes
$ git commit-m "Commit message"
  • To see all the commits(previous versions of the app).
$ git log

You'll see something like this

commit f65881c0e1a7866b15da23576ccf176a184561234 (HEAD -> master)
Author:Your name
Date: Date of the commit
commit message

You see this long "number"? It's called hash

  • To go back to a previous version type
$ git checkout f65881c0e1a7866b15da23576ccf176a184561234
  • Now if we want to create a new branch to see how pink background would look like in our app. We can just create a new branch.
$ git branch pinkbg
  • Switch to the pinkbg branch and start coding
$ git checkout pinkbg 
  • See all the branches
$ git branch
  • Merge branch with master
$ git merge pinkbg

And that's it for now! :)

Top comments (3)

shubhamb profile image

Wow...Nicely explained. It would be great if you share how to switch between multiple git accounts on single machine.

emjimadhu profile image
Em Ji Madhu

It's easy. Create multiple ssh keys for each account and in .ssh/config give hostname: GitHub official and point to the newly generated key file. And when you clone a repo put the prefix infront of host name example

jpchateau profile image
Jean-Philippe Chateau

I use my personnal email and name that I configure globally on my device (git config --global -e), and when I work on my clients' projects, I configure git to use my professionnal email and name (git config -e) on each project. Of course, I use the same public ssh key on all my accounts (github, gitlab, bitbucket).
If I have to use different ssh keys, then I use an ssh config file (or if I want to ease some server connections too).