Displaying commit history – Source Code Management with Git and GitOps

Git keeps a history of all commits. To see a list of all commits that you’ve done, you can use the following command:

$ git log

commit 275d24c62a0e946b8858f562607265c269ec5484 (HEAD -> master)

Author: Gaurav Agarwal <[email protected]>

Date:     Wed Apr 19 12:27:13 2023 +0530

My second commit

commit cecfb61b251f9966f50a4d8bb49742b7af014da4

Author: Gaurav Agarwal < [email protected]>

Date:     Wed Apr 19 12:20:02 2023 +0530

My first commit

As we can see, Git has displayed the history of both our commits. Notice that Git marks every commit with a commit ID. We can also delve into what changes were made in the commit by using the git diff <first_commit_id> <second_commit_id> command, as follows:

$ git diff cecfb61b251f9966f50a4d8bb49742b7af014da4 \ 275d24c62a0e946b8858f562607265c269ec5484 diff –git a/file1 b/file1

index e69de29..0cbcf32 100644

  • a/file1

+++ b/file1 @@ -0,0 +1 @@ +This is first line

The output clearly shows that the second commit has added This is first line within file1.

You’ve suddenly realized that you needed to add another line to file1 and wanted to do so in the same commit. We can do this by amending the commit. We’ll look at this in the next section.

Amending the last commit

It is a best practice to have a single commit for your changes to a particular feature. This helps you track the changes better and makes it easier for the reviewer to review them. In turn, it is cleaner to visualize and manage. However, committing frequently is also a best practice so that your changes are not lost. Fortunately, Git allows you to add changes to the last commit.

To demonstrate this, let’s change file1 and add another line:

$ echo “This is second line” >> file1

$ cat file1

This is first line

This is second line

Now, let’s add the changes to the previous commit using the following commands:

$ git add file1

$ git commit –amend

Once you run this command, Git will show you a prompt, allowing you to amend the commit message if you like. It will look something like the following:

My second commit

  • Please enter the commit message for your changes. Lines
  • starting with # will be ignored and an empty message aborts the commit
  • Date Wed Apr 19 12:27:13 2023 +0530
  • on branch master
  • Changes to be commited
  • modified: file1

#

Save this file (use ESC:wq for Vim). This should amend the last commit with the changes. You should get the following output:

Date: Wed Apr 19 12:27:13 2023 +0530

1 file changed, 2 insertions(+)

When Git amends a commit, you can no longer refer to the previous commit with the same commit ID. Instead, Git generates a separate SHA-1 id for the amended commit. So, let’s look at the logs to see this for ourselves:

$ git log

commit d11c13974b679b1c45c8d718f01c9ef4e96767ab (HEAD -> master)

Author: Gaurav Agarwal <[email protected]>

Date:     Wed Apr 19 12:27:13 2023 +0530

My second commit

commit cecfb61b251f9966f50a4d8bb49742b7af014da4

Author: Gaurav Agarwal < [email protected]>

Date:     Wed Apr 19 12:20:02 2023 +0530

My first commit

Now, let’s run the diff command again and see what it is reporting:

$ git diff cecfb61b251f9966f50a4d8bb49742b7af014da4 \ d11c13974b679b1c45c8d718f01c9ef4e96767ab diff –git a/file1 b/file1

index e69de29..655a706 100644

  • a/file1

+++ b/file1

@@ -0,0 +1,2 @@ +This is first line +This is second line

The output clearly shows that the second commit has added This is first line, as well as This is second line, within file1. With that, you’ve successfully amended a commit.

Local repositories are as good as keeping files on your system. However, since you need to share your code with others and keep it secure from laptop OS crashes, theft, physical damage, and more, you need to push your code into a remote repository. We’ll look at remote repositories in the next section.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these