Git Checkout: Play with files

OpenInfo
OpenInfo
Published in
2 min readMar 18, 2018
“Binders and boxes on shelves in a large archive” by Samuel Zeller on Unsplash

Working on a git project as a beginner gives fear to people that they can destroy some files. Here Checkout comes into picture.Majorly Git checkout serves three purposes:

  • Checkout file: Checking out a file lets you see the older version of the file, leaving the current working directory untouched.
ansible[@[:~/ansible/ansible$ git branch
devel
* local_changes
release1.5.0
ansible[@[:~/ansible/ansible$ git checkout README.md
ansible[@[:~/ansible/ansible$

Important: Checking out an older file can overwrite the current changes in local working space. Those changes can be recommitted as part of the new snapshot. So, in effect, this usage of git checkout serves as a way to revert back to an older version of an individual file.

ansible[@[:~/ansible/ansible$ git status
On branch local_changes
Changes not staged for commit:
modified: setup.pyno changes added to commit (use "git add" and/or "git commit -a")ansible[@[:~/ansible/ansible$ git checkout setup.py ansible[@[:~/ansible/ansible$ git status
On branch local_changes
nothing to commit, working directory clean
ansible[@[:~/ansible/ansible$
  • Checkout commit: Checking out a commit makes the current working directory match that commit. It shows the old state of your project without effecting the present changes. It is not necessary to provide complete hash. Initials will be enough for a checkout.
  • Important: Checking out an older commit doesn’t alter current working space or current changes. It is only to view the older commit files.
ansible[@[:~/ansible/ansible$ git log
commit 74139d323f4c9cbbd5b1f78f0e33017dcd67042f
Author: OpenInfo <OpenInfo.collection@gmail.com>
Date: Sat Mar 17 17:27:26 2018 -0700
Added print statement in file setup.pycommit 6221a2740f5c3023c817d13e4a564f301ed3bc73
Author: James Cammarata <jcammarata@ansibleworks.com>
Date: Fri Feb 28 14:17:07 2014 -0600
Updating files for new upstream release 1.5.0ansible[@[:~/ansible/ansible$ git checkout 74139d323f4c9cbbd5b1f78f0e33017dcd67042f
Note: checking out '74139d323f4c9cbbd5b1f78f0e33017dcd67042f'.
HEAD is now at 74139d3... Added print statement in file setup.py
ansible[@[:~/ansible/ansible$
ansible[@[:~/ansible/ansible$ git checkout 6221a2
Previous HEAD position was 4b281ca... Make the npm production parameter in the docs list the choices.
HEAD is now at 6221a27... Updating files for new upstream release 1.5.0
ansible[@[:~/ansible/ansible$
  • Checkout branch: Checking out a branch views the all files of a branch.
ansible[@[:~/ansible/ansible$ git branch
* (HEAD detached at 6221a27)
devel
local_changes
release1.5.0
ansible[@[:~/ansible/ansible$ git checkout local_changes
Previous HEAD position was 6221a27... Updating files for new upstream release 1.5.0
Switched to branch 'local_changes'
ansible[@[:~/ansible/ansible$ git branch
devel
* local_changes
release1.5.0

Now, you can use your git project as a playground and fearlessly alters the files .

--

--