Wednesday, September 30, 2015

Git Learning Part III - Viewing Old Commit

git checkout command serves three functions. Checking out files, checking out commits, checking out branches. Checking out a commit makes the entire working directory match that commit. This can be used to view the old state of the project without altering the current state in anyway. Checking out a file let us see an old version of the particular file, leaving rest of the working directory untouched. 

usage is 

git checkout master 

the above command returns to the master branch. 

other example usages are 

git checkout => Checkout the previous version of the file. This turns the that resides in the working directory into an exact copy of one from the and adds to the staging area 

git checkout   => Updates all files in the working directory to match the specified commit. We can use either a commit hash or tag as the element. This will put in a detached HEAD state. 

In Simple words, Git checkout is an easy way to load any of the saved snapshots on the development machine.  

Checking out an old commit is a read-only operation. Its impossible to harm the repository while viewing an old revision. The “current” state of the project remain untouched in the master branch. During normal course of development HEAD points to master or some other local branch. but when we checkout a previous commit, HEAD no longer points to a branch - it points directly to a commit. This is called detached HEAD state. 

Checking out an old file does affect the current state of the repository. One can re-commit the old version in a new snapshot as he would with any other file. So, in effect, the git checkout serves as a way to revert back to an old version of an individual file. 

The example given is good one that illustrate this, 

Consider we have a repo that 

b7119f2 Continue doing crazy things
872fa7e Try something crazy
a1e8fb5 Make some important changes to hello.py
435b61d Create hello.py
9773e52 Initial import

If we want to look at the a1e8fb5 again and decide after doing some tests, below can be done 

git checkout a1e8fb5

This above makes the working directory exact state as a1e8fb5 commit. One can look at the files, compile project, run tests, and even edit the files without worrying about losing current state of the project. 

to continue developing, we can get back to the current state of the project, by 

git checkout master 


If anything to be reverted at this point, we can use the git revert or git reset command. 

References:

No comments:

Post a Comment