It’s happened to me many times – I do loads of coding on a feature branch, go off to work on something else for a while, and when I come back to my branch I can’t remember what was in my last commit. So you want to see your last git commit message, and the files you changed in your last commit. This post will explain how to ‘git show last commit‘ so you can see both the commit message and the contents of that commit. I’ll also describe how to get the last git commit hash id.
Table of contents
How to see the last commit on all branches
This solution will show you a simple summary of the last commit on each branch, with the current branch marked in colour and with an asterisk (*). As you can see below, it shows you :
- the name of the branch
- how many unpushed commits you have on your branch
- a summary of the most recent commit, including the hash id and the commit message of the last commit
In the following example using the -v
flag with the git branch
command, I have 2 unpushed commits on my branch main and the last commit has a commit message of “added settings page” :
//this command....
$ git branch -v
//....shows you this summary
* main 11086e3 [ahead 2] Added settings page
In the image below you can see a busier repo, with many branches, and summary of the last commit on each branch :
But what if this simple summary isn’t enough detail? Maybe you want to see commit messages for all the commits?
How to git show last commit
To view all detail about your last commit use the --source
flag of the git show
command, like this:
git show --source
This solution shows you the following detail about your last commit:
- the author
- date
- the commit message
- and the diff –git for all changed files in latest commit.
Jump to my other post if you want to know how to undo your last git commit!
Get last commit hash id
You can obviously see the hash id of the last commit in some of the previous solutions. However this command will get you only the last commit hash id with no other detail :
git log -n 1 --pretty=format:"%h" | tail -n 1
View and edit last commit message
To display only the most recent commit message use this command :
git log -1
It will show you the author, the date and time that the commit was made, the full hash id and the commit message. It doesn’t show you the file that was changed.
However for a more succinct summary use git log -1 --oneline
which will only sow you the sha1 and commit message.
View last commit message on a specific branch
If you want to see the same information as above, but on a specific branch, simply add the branch name as a flag to the command :
git log -1 branch_name
Summary
You should now know how to git show last commit on all your branches, how to view only the last commit on your current branch, the last commit on a specific branch.