Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox
Sahil Suri, Author at The Linux Juggernaut

Author: Sahil Suri

git: What is detached HEAD state?

Introduction In our previous article on working with the git version control system, we explained how to use the git checkout command to retrieve previously committed versions of files from the git repository. In this article, we will talk about a situation that arises if you don’t mention a file name while running the git checkout command and simply type ‘git checkout HEAD~n’ where n is the commit to which to want to move the head to. How does a repository go into a detached HEAD state? Using the git checkout command we generally check out a branch of the repository to work with the content within that branch. We’ll talk about branches in detail in a separate article.  For the time being, we’ll define branches as movable pointers to commits in the version history of our git repository. When we initialize a git repository we work with a default branch named master. We may create further branches and subsequently work with those branches. Let’s take a look at the state of the files inside the repository we’ve been using four commits before the current committed state. [sahil@linuxnix my_first_repo]$ git diff HEAD~4 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 c66d471..e945f40 100644 --- a/test.txt +++...

Read More

git checkout command explained

Introduction In our previous article on working with the git version control system, we explained and familiarized the concept of the HEAD. In this article, we introduce you to the git checkout command and explain how it can be used to restore files to their previous committed states. What is git checkout? The git checkout command takes the HEAD and moves it to a different point in the commit history of your git repository. So, the git checkout command allows us to check out or restore a previous state of the file. We can commit the restored version of the file and save it as the most recent commit or we could revert to the most recent committed version of the file using git checkout. Demonstration: We’ll now demonstrate how we can use the git checkout command to revert to a previous state of a single file in a git repository and also how to restore the checked out file back to its last committed state. Let’s take a look at the state of our repository two commits before the current commit using git diff. [sahil@linuxnix my_first_repo]$ git diff HEAD~2 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 06ee124..e945f40 100644 --- a/test.txt +++ b/test.txt...

Read More

Using HEAD and git commit hash to differentiate between committed file versions

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.   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...

Read More

Over 16,000 readers, Get fresh content from “The Linux juggernaut”

Email Subscribe

ABOUT ME..!

My photo
My name is Surendra Kumar Anne. I hail from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present I work at Bank of America as Sr. Analyst Systems and Administration. You can contact me at surendra (@) linuxnix dot com.