HEAD
What is HEAD in a git repository? - It is a pointer to a commit.
How can I check that?
$ cat .git/HEAD
ref: refs/heads/core-3
This tells HEAD is pointing to core-3 branch. In git branch is also a pointer. For all the branches you can get files created in .git/refs/heads storing commit SHA-1 values.
$ ls -l .git/refs/heads/
total 4
-rw-r--r-- 1 USER 197121 41 Dec 25 10:12 core
-rw-r--r-- 1 USER 197121 41 Dec 25 10:43 core-2
-rw-r--r-- 1 USER 197121 41 Dec 25 16:10 core-3
-rw-r--r-- 1 USER 197121 41 Dec 20 15:29 master
$ cat .git/refs/heads/master
3a86518414ad9fd99164ef5ad1abbba8190f9dda
And if you will view log it will be top most commit. The --decorate flag also tells position of each pointer.
$ git log --oneline --decorate
33fc97f (HEAD -> core-3) Reverts commit -> "Add item 1 and 4 into sidebar"
6ee1d05 Add item 2 and 3 in sidebar
ead6c93 Add item 1 and 4 into sidebar
ac79e95 Reverts commit -> "Adds bottom_bar, edits homepage and delees index"
e671180 Adds bottom_bar, edits homepage and deletes index
1e98c30 Adds commit to sidebar
cb23721 Adds index page of website.
cae4680 Add websites's Home page.
8816355 Adds body tag in header.html
94f8df4 Adds html with header.html
3a86518 (origin/master, master) Initial Checkin
How can I navigate from HEAD?
You can only navigate backwards from HEAD.
- To access previous commit - HEAD^
- To access previous to previous commit - HEAD^^ or HEAD~2
$ git checkout HEAD~5
But if you have SHA-1 value then you can directly take HEAD pointer to that commit using,
$ git checkout <SHA-1_value>
Rewriting History
Git allows you to rewrite history. It is more like a tape on which you can go back and record something else.
Where can it be used? - Consider you did certain commits and you realized that the commit stagings were incorrect. It need be merged or broken into more commits, in such a scenario you need to go back in history and redo the commits as you like. This is where you can use $ git reset.
BE CAUTIOUS while doing this, as you may loose your changes if you don't understand the repercussions.
SOFT Mode
In soft mode, when you do a reset to any previous commit then delta change (post previous commit to current commit) are shown in staging. Then you can do re-commits with modification.
E.g.
$ git log --oneline
0 = 33fc97f Reverts commit -> "Add item 1 and 4 into sidebar"
1 = 6ee1d05 Add item 2 and 3 in sidebar
2 = ead6c93 Add item 1 and 4 into sidebar
3 = ac79e95 Reverts commit -> "Adds bottom_bar, edits homepage and deletes index"
4 = e671180 Adds bottom_bar, edits homepage and deletes index
5 = 1e98c30 Adds commit to sidebar
6 = cb23721 Adds index page of website.
7 = cae4680 Add websites's Home page.
8 = 8816355 Adds body tag in header.html
9 = 94f8df4 Adds html with header.html
10 = 3a86518 Initial Checkin
$ git reset HEAD~7 --soft
$ git log --oneline
cae4680 Add websites's Home page.
8816355 Adds body tag in header.html
94f8df4 Adds html with header.html
3a86518 Initial Checkin
$ git status
On branch core-3
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: index.html
new file: sidebar.html
MIXED Mode
In mixed mode, when you do a reset to any previous commit then delta change (post previous commit to current commit) are shown in working directory. Then you can do re-commits with modification and staging.
E.g.
$ git log --oneline
33fc97f Reverts commit -> "Add item 1 and 4 into sidebar"
6ee1d05 Add item 2 and 3 in sidebar
ead6c93 Add item 1 and 4 into sidebar
ac79e95 Reverts commit -> "Adds bottom_bar, edits homepage and deltes index"
e671180 Adds bottom_bar, edits homepage and deltes index
1e98c30 Adds commit to sidebar
cb23721 Adds index page of website.
cae4680 Add websites's Home page.
8816355 Adds body tag in header.html
94f8df4 Adds html with header.html
3a86518 Initial Checkin
$ git reset HEAD~7 --mixed
$ git log --oneline
cae4680 Add websites's Home page.
8816355 Adds body tag in header.html
94f8df4 Adds html with header.html
3a86518 Initial Checkin
$ git status
On branch core-5
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
sidebar.html
nothing added to commit but untracked files present (use "git add" to track)
This is default mode. If you just execute $ git reset HEAD~7 it will consider --mixed mode.
HARD Mode
This is dangerous if you don't understand. When you do a hard reset to any previous commit then delta changes (post previous commit to current commit) are completely removed from the repository. You will neither find them in working nor in staging.
E.g.
$ git log --oneline
33fc97f Reverts commit -> "Add item 1 and 4 into sidebar"
6ee1d05 Add item 2 and 3 in sidebar
ead6c93 Add item 1 and 4 into sidebar
ac79e95 Reverts commit -> "Adds bottom_bar, edits homepage and deltes index"
e671180 Adds bottom_bar, edits homepage and deltes index
1e98c30 Adds commit to sidebar
cb23721 Adds index page of website.
cae4680 Add websites's Home page.
8816355 Adds body tag in header.html
94f8df4 Adds html with header.html
3a86518 Initial Checkin
$ git reset HEAD~7 --hard
HEAD is now at cae4680 Add websites's Home page.
$ git log --oneline
cae4680 Add websites's Home page.
8816355 Adds body tag in header.html
94f8df4 Adds html with header.html
3a86518 Initial Checkin
$ git status
On branch core-8
nothing to commit, working directory clean
If you want to set the working and staging area as it was after last commit -
$ git reset HEAD -- hard
No comments:
Post a Comment
Your comments are very much valuable for us. Thanks for giving your precious time.