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…”
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…
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”
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.
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.
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.
git log -1 –pretty=%B remove “prettty” from command
Hi Vazid. Why would you like me to remove “–pretty” from the command?