Author: Sahil Suri

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

git log command explained with examples

Introduction In our previous articles on working with the git version control system, we explained how to initialize a local git repository, add content to it and we also worked with the git add in detail. In this article, we will focus on the git log command. We will understand the significance of the git log command, the information it provides and usage examples. The significance of git log The git log command allows us to read and review a history all the changes and updates that place in a git repository. This is essential in tracking changes that were made to a file. The git log command also provides us the required information to revert to a previous version of the file or at times even recover a deleted file. We will talk in greater detail about reverting to previous versions of files and deleted file recovery in a subsequent article. Information provided by ‘git log’ A typical git log will provide you with information about all the commits made to a git repository since its inception. It contains the following details corresponding to each commit: A commit hash: A SHA1 40 character checksum of the commits’ contents. Since it is generated based on the commit contents it will always be unique. Commit Author metadata: The name and email address of the author of the commit. Commit Date metadata: A...

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.