After I have done a merge to my master branch from a working branch with git, I sometimes want to find to find the last commit on master before the merge happened. How do I do this?
Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.
To see the merge commit's message and other details, use git show-merge with the same arguments.
If you want to look at previous commits, you can use git log and its many arguments. If you want to checkout an actual commit to view the files in an editor, just use git checkout to move to any commit you want. When you are finished, just do git checkout master to return to your current state.
Try simply git whatchanged master.. staging to see what changed on staging since you last merged from staging to master.
The quick way to determine commit after merge occured is to use the reflog.
Assuming that last occured operation was a merge, then:
git log HEAD@{1} -1
HEAD@{1}
refers to the previous HEAD before the last operation, so you can address it using log and reflog.
git log
will show you sequence of the commits in the current branch, so after a merge it always will be a merge commit, and right before it will be commits from the merged branch. git reflog
shows the sequence of operations in your repository (for example merge, rebase). As explained in the docs:
Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference.
A quick way to do this is to type
git log --pretty=format:'%h : %s' --graph
then just follow down the graph on the right hand side till you find the merge point. You can also do
git log --pretty=format:'%h : %s' --graph > temp.txt
which puts the output into a file, temp.txt
, that which you can open in your editor and use the search facility to look for text like merge.
This approach is useful to answer lots of other questions about the lineage of your latest commit so have put
alias git_graph="git log --pretty=format:'%h : %s' --graph"
in my .bash_profile
file so I can just use ```git_log`` to see this information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With