INTRODUCTION
In an earlier article, we showed you how to look at differences between a file after you’ve made a change to it. In this article, we will talk about how to view differences between committed versions while also explaining the term HEAD as it pertains to git terminology. We will also demonstrate how to use the git commit hash to refer to commits.
What is HEAD?
The HEAD in git terminology refers to the current committed state of the file in the git repository. Actually, the HEAD is a reference to the currently checked out commit. If you are using multiple branches in your git repository then the HEAD refers to the commit at the tip of the current branch. HEAD is just a convenient name to mean “what you have checked out” and we will demonstrate in the following examples how you can use the HEAD to refer to previous commits in the repository.
The HEAD in git terminology refers to the current committed state of the file in the git repository. Actually, the HEAD is a reference to the currently checked out commit. If you are using multiple branches in your git repository then the HEAD refers to the commit at the tip of the current branch. HEAD is just a convenient name to mean “what you have checked out” and we will demonstrate in the following examples how you can use the HEAD to refer to previous commits in the repository.
USING HEAD WHILE RUNNING GIT COMMANDS:
Example 1: To view differences between the current committed state of the folder and one commit before, type the following command.
[sahil@linuxnix my_first_repo]$ git diff HEAD~1 diff --git a/test.txt b/test.txt index ba08292..e945f40 100644 --- a/test.txt +++ b/test.txt @@ -1,2 +1,4 @@ This is a test file! Added another line to test file +Adding a third line to test file +Adding a fourth line to test file [sahil@linuxnix my_first_repo]$
Example 2: To view differences between the current committed state of the folder and four commits before, type the following command.
[sahil@linuxnix my_first_repo]$ git diff HEAD~4 diff --git a/README1.md b/README1.md index acea079..21a6b29 100644 --- a/README1.md +++ b/README1.md @@ -1,2 +1,2 @@ This is a readme file for my first git repository -This is a second line +This is another line diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e945f40 --- /dev/null +++ b/test.txt @@ -0,0 +1,4 @@ +This is a test file! +Added another line to test file +Adding a third line to test file +Adding a fourth line to test file [sahil@linuxnix my_first_repo]$
The /dev/null state for the file test.txt indicates that this file did not exist four commits earlier.
Example 3: Check how far back we can go with the HEAD
Let’s run the git log command to view the number of commits we have thus far in our repository.
Let’s run the git log command to view the number of commits we have thus far in our repository.
[sahil@linuxnix my_first_repo]$ git log --oneline 41bfa8f Added third line to test.txt 23c9770 Added another line to test.txt a291f69 Added file test.txt nad updated .md files d76dd61 Modified the *.md files 99fa732 Created a new file README1.md f9849b2 Added a line to README.md f7eccb7 Added my first file
The above output tells us that we have a total of seven commits so using the HEAD, we can go as far back as six commits. If we run the git diff HEAD~6 command we will observe that both the files that currently reside in the repository had a previous state of /dev/null.
[sahil@linuxnix my_first_repo]$ git diff HEAD~6 diff --git a/README1.md b/README1.md new file mode 100644 index 0000000..21a6b29 --- /dev/null +++ b/README1.md @@ -0,0 +1,2 @@ +This is a readme file for my first git repository +This is another line diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e945f40 --- /dev/null +++ b/test.txt @@ -0,0 +1,4 @@ +This is a test file! +Added another line to test file +Adding a third line to test file +Adding a fourth line to test file
If we try to look back further than the number of commits we have, git will give us an error message. Given below is an example.
[sahil@linuxnix my_first_repo]$ git diff HEAD~7 fatal: ambiguous argument 'HEAD~7': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
Example 4: Using the git commit hash
Referring to the previously committed versions of files using the HEAD appears to be convenient but can become cumbersome if the number of commits in the repository is very high. So, there’s a simpler way to refer to previous commits and that is by using the commit hash. Let’s take a look at the current git log for our repository.
Referring to the previously committed versions of files using the HEAD appears to be convenient but can become cumbersome if the number of commits in the repository is very high. So, there’s a simpler way to refer to previous commits and that is by using the commit hash. Let’s take a look at the current git log for our repository.
[sahil@linuxnix my_first_repo]$ git log --oneline 7d39d7a modified test.txt 2d6f7f2 deleted file README.md 41bfa8f Added third line to test.txt 23c9770 Added another line to test.txt a291f69 Added file test.txt nad updated .md files d76dd61 Modified the *.md files 99fa732 Created a new file README1.md f9849b2 Added a line to README.md f7eccb7 Added my first file
To refer to a particular commit I just need to use the first seven digits of the commit hash that appear in the git log –oneline output. Let’s use the commit hash 23c9770 of the third commit from the current commit.
[sahil@linuxnix my_first_repo]$ git diff 23c9770 diff --git a/README.md b/README.md deleted file mode 100644 index ba76a74..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -This is a readme file for my first git repository diff --git a/test.txt b/test.txt index ba08292..e945f40 100644 --- a/test.txt +++ b/test.txt @@ -1,2 +1,4 @@ This is a test file! Added another line to test file +Adding a third line to test file +Adding a fourth line to test file [sahil@linuxnix my_first_repo]$
As you can see the output is the same as it would’ve been if we had used HEAD. We could’ve used the entire string present in the commit hash but the first seven characters are unique enough to suffice on their own.
0 comments:
Post a Comment