How to Read Previous Git Commit Messages

Git is a powerful version control system that’s used to keep track of changes in text files. A project managed with Git is called a Git repository, and changes are recorded as commits. I personally use it to keep track of the source code and builds of my software projects.

 

Now, the problem is…

 

So you also use Git to manage that project of yours. However, you need to view the last commit message or some previous commit message(s) even older than the last one. Possibly it’s because you want to know where you stopped working. Or you want to know the changes made by your colleague(s) or collaborator(s). Whatever your reasons, here is a quick tutorial that helps you view the past commit messages of any Git repo(sitory). Note that I will use Ubuntu to demonstrate the needed commands, but they will work on any Linux or Mac OS computer that has Git installed.

 

Git says “Your last words to me were…”

Step 1 - How to read previous Git commit messages- In a terminal or command prompt, navigate to the repo whose last commit message you want to view and type: git log -1

Viewing your last commit message is very easy, really. Just fire up a terminal and navigate to the root directory of your Git repository:

cd /path/to/repository/

In my own case, my repository was in ~/projects/. (Note: ~ is a shorthand symbol for /home/$USERNAME in Ubuntu. So the absolute path to the repo is /home/folusho/projects/.) So, in order to view the very last commit message, just will enter this command:

git log -1

PRO TIP: The path you navigate to in the terminal can be either an absolute or relative path. For the sake of clarity, I chose to navigate using the absolute path to my repo. If I wanted to use the relative path instead, since I was already in my home directory (i.e in /home/folusho), I would have used cd projects/, and that would be it.

 

Wait, I want to see more than one…

Step 2 - How to read previous Git commit messages- It is possible to see more than one previous commit message. For example, to see two (2) previous commit messages, type: git log -2. For the last three (3) messages, type: git commit -3. And so on.

However, you might want to see more than the immediate last commit message. Well, that’s easy too. For example, if you want to see the last two commit messages, just change that last argument to -2, like this:

git log -2

And voila! Your last two commit messages will be on the screen. The same method is applicable if you want to read 3, 4, 5 and more previous commit messages.

 

Then you said “I want to see all my commit messages”

Step 3 - How to read previous Git commit messages- To view ALL your previous commit messages, just enter: git log. The terminal will only print the last 10 commits initially, so you will have to paginate through the rest using the Enter or Return key.

In order to see all the commit messages since the creation of your repository, just leave out the last argument. So, what you will enter is:

git log

Note that if you have many commit messages, the terminal will only display the most recent ten (10) of them. So you will have to paginate through the rest by pressing the Enter or Return key again and again. In this case though, my repo only had three commits.

 

Finally, we can make the output more concise.

Step 4.1 - How to read previous Git commit messages - You can get more concise output from "git log" using the --pretty=%B argument.

If you consider the output of the above commands to be rather verbose, there’s a way to make it more concise. Git provides an additional argument to the log command that strips away all flab and gives you ONLY the commit message. So, for example, if we wanted to view just the commit message for the last commit, the command to enter is:

git log -1 --prettty=%B

Did you see that? That --pretty=%B argument does some magic and makes the output concise and straight to the point. You can see the screenshot below for how I used this new argument for all the steps discussed above.

Step 4 - How to read previous Git commit messages - Whenever you want more concise output from git log, just give it the --pretty=%B argument. So you can type: git log -1 --pretty=%B

 

I hope you found this post interesting. If you know of some even better arguments to git log, let me know in the comments below. Feel free to also post any other feedback you have.

Merci pour la lecture!

 

2 comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.