Checkout#
git checkout
is a command that allows you to swich current state of project to the another commit.
Back up commit#
In the next example:
Two commits have been created:
first commit
addfirst_file
.second commit
editsfirst file
and addssecond file
;
I used
git log
to show these commits:Note the (
HEAD->master
) nearsecond commit
, this means you are staying onsecond commit
and the master branch is pointing to it;Note To print
HEAD->master
in jupyter notebook output you have to use the--decorate
option for some reason, in terminal this is not necessary;
I stored the hash of the first commit in the
first_hash
variable and printed it;Using
git checkout $first_hash
I movedHEAD
tofirst commit
, which means it’s now an active commit:Note In real work you can use the hash of the commit and don’t use a variable to store the commit hash;
Note by default
git checkout
prints it’s message to the error stream, so I used2>&1
to redirect it to the out stream;
By calling
git log
again, I showed:That
(HEAD)
is now next to thefirst commit
message;The
second commit
isn’t even shown now - git’s default behaviour forgit log
is to show the history of the commit that created the head commit. Use the--all
option to show all messages;
ls
will show that there is nosecond file
in the project folder now - it’s ok, we’ve moved to a commit where there is no such file;cat first_file
shows that the first file is still the same as it was in the second commit:This is because we reverted git to
first_commit
but not the working directory. Ingit status
you can see thatfirst_file
is marked asmodified
, it is indeed different from the commit that HEAD is currently referring to;To return a file to the state as in
first commit
you simply need to do agit restore
for that file, which is what I am doing and demonstrating thefirst_file
fromfirst commit
.
%%bash
mkdir checkout_example
cd checkout_example
git init &> /dev/null
echo "some text" > first_file
git add first_file
git commit -m "first commit" &> /dev/null
first_hash=$(git rev-parse HEAD)
echo "some text" > second_file
echo "this is the text for second commit" > first_file
git add second_file
git commit -m "second commit" &> /dev/null
echo "=====log====="
git log --decorate
echo
echo "=====first commit hash====="
echo $first_hash
echo
echo "=====git chechout to first commit====="
git checkout $first_hash 2>&1
echo
echo "=====log====="
git log --decorate
echo "=====ls====="
ls
echo "=====first_file====="
cat first_file
echo
echo "====status====="
git status
git restore first_file
echo "=====first_file after restore====="
cat first_file
cd ..
rm -r checkout_example
=====log=====
commit 23e858c0a0924f75ef4f1a9eddf0c357f9d865b5 (HEAD -> master)
Author: Dranikf <kobfedsur@gmail.com>
Date: Thu Sep 7 22:45:41 2023 +0300
second commit
commit f42455672912f0347c9e29165583ddc5accf9b25
Author: Dranikf <kobfedsur@gmail.com>
Date: Thu Sep 7 22:45:41 2023 +0300
first commit
=====first commit hash=====
f42455672912f0347c9e29165583ddc5accf9b25
=====git chechout to first commit=====
Note: switching to 'f42455672912f0347c9e29165583ddc5accf9b25'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at f424556 first commit
M first_file
=====log=====
commit f42455672912f0347c9e29165583ddc5accf9b25 (HEAD)
Author: Dranikf <kobfedsur@gmail.com>
Date: Thu Sep 7 22:45:41 2023 +0300
first commit
=====ls=====
first_file
=====first_file=====
this is the text for second commit
====status=====
HEAD detached at f424556
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: first_file
no changes added to commit (use "git add" and/or "git commit -a")
=====first_file after restore=====
some text