I'm trying to help a coworker figure out what a bunch of "empty commit" warnings were about in his recent merge. I opened up gitk, and saw something like this:
_o (Z) Merge branch 'new-branch' (yesterday)
o | (Y) Fix bad merge (person 1)
o_| (X) Merge branch 'master' into new-branch (recent)
o | (W) Last legitimate commit that belongs on new-branch (person 1)
| | ... work on master ...
o | (F) Legitimate commit that actually belongs on new-branch (person 2)
| | ... work on master ...
o | (E) Legitimate commit that should have been on master (person 2)
o | (D') Even more work etc... (committed by person 2)
o | (C') More work in master (committed by person 2)
o | (A') Normal work in master (committed by person 2)
| o (D) Even more work etc... (authored by random person)
| o (C) More work in master (authored by random person)
o | (B) Starting to work on new-branch (person 1)
|_o (A) Normal work in master (authored by random person)
o Common Ancestor (weeks ago)
So obviously the two people working on this branch should have merged from master into their branch more often, and then these piles of merge warnings would have been more obvious. The team member whose name was in the committer field of the duplicated commits says he probably did a pull --rebase to cause them, but I can't wrap my head around how that could work. Can anyone explain what might have happened?
I'm not looking for a way to fix the merge warnings, since they seemed benign. I just want to understand what happened so I can prevent it from happening in the future. My team is relatively new to git, so I'm trying to help them understand it one small step at a time, using trial and error for the most part.
Thanks!
Make sure you are on the branch to which you have been committing. Use git log to check how many commits you want to roll back. Then undo the commits with git reset HEAD~N where “N” is the number of commits you want to undo. Then create a new branch and check it out in one go and add and commit your changes again.
When you do a rebase on a branch, you rewrite that branch's history. All the affected commits will have their IDs changed at least (even if there was no content change within the commit). Because of this, it might appear as if you are having duplicate commit(s), which you actually don't. You have two different commits with the same content.
The way to reproduce this "duplicate commits" behavior is usually to do a rebase of a branch that has been already pushed to a remote repository and then merge it back to that same remote branch. The rebase will change the IDs of your commits and the merge will keep both pairs of "duplicate" commits, even though their changes will result in the same content.
git checkout -b feature
git add . ; git commit -m "added new file on master branch"
git checkout master ; git add . ; git commit -m "added new file on master branch"
git checkout feature ; git push --set-upstream origin feature
git rebase master
git pull
" first. This is the point where you actually get your "duplicate" commits, because of the merge (git pull
is a shorthand for git fetch ; git merge
), also remember that commits' content are the same, but due to the rebase of the branch, the commits' IDs are different now and thus, they are being considered as a different thing, so: git pull
git gui
" (navigate to the main menu - Repository - Visualize feature's History) and you'll see something like this:
git log
to see it directly in the command line:
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