#10 Git log: Viewing and changing commits

So in this post we will learn about git log command and its usage with different different options. To do that first we have to clone a repository.

Here I have cloned the pandas repository in my F drive. Pandas is a module in Python programming language used for data analysis, so lots of commits have been made on it. After cloning the pandas repository I’ll go into the pandas folder using the cd command. I’ll run the “git status” command over there.

Now , to see the list of commits made on this repository I’ll execute following command:

git log

Git log command gives the above output. This is the list of commits made on this particular repository by different contributors, we can scroll down this list using the up and down arrow and can exit this list by pressing the “q” key. However, we can try multiple options with git log and we will see them one by one.

To get the commits with difference

git log -p

Above command gives us the author name, hash number, date of commit and mainly gives difference between commits.

To get the specified number of commits

git log -p -3

This command will show the log for number of commits you have specified with their difference. In this case it will show up to 3 commit logs.

To get the commits with the staticstics

git log --stat

It returns the Author Name, Date of commit, Changes made, Files that got changed, with insertion and deletion. So It basically gives short statistics of each git commit.

Print the commits in oneline

Git log --pretty=oneline

This command shows each commit in one line. That online consist of commit hash and commit message.

To get the short information about each commit

git log --pretty=short

This command will return the commit hash, author and commit message.

To get the full information of each commit.

git log --pretty=full

This command returns the commit author, commit hash, commit message.

To print the commits till the specified time.

git log --since=2.days

This command will return commit hash, commit author, date of commit and commit message which has happened in two days. We can do it for weeks, months and years also. We just have to replace the days in the above command.

Print using patterns

git log --pretty=format:" %h -- %an"

This command will return a short commit hash and author name. In the above command “h” means abbreviated hash commit and “an” means Author name. So this is how we can customize our query to get particular information about commits. You can explore other options on the git official website.

Now If we want to change the previous commit message then we can do it in following way:

Make changes into any file and add it to the staging area, here I have modified the AUTHORS.md file now I’ll use the following command to make changes to the recent commit message.

git commit --amend -m "new commit message here"

After executing above command we will get to see that commit message for author “Maciej Kos” is changed.

Previous Message
Commit message after amend command

So this is how we can change the previous commit messages.

Leave a Comment

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