How do these git duplicate in the wrong branch?

I am trying to help a colleague find out that in his recent merge there were a lot of warnings about "empty commit". I opened 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) 

Thus, it is obvious that the two people working on this industry should more often unite with the master in their industry, and then these piles of merger warnings would be more obvious. A member of the team whose name was in the committer field of the duplicate commits says that he probably did pull-rebbase to call them, but I can't wrap my head around how this might work. Can someone explain what could happen?

I'm not looking for a way to fix merge warnings, as they looked benign. I just want to understand what happened so that I can prevent this from happening again. My team is relatively new to git, so I'm trying to help them figure it out one small step at a time, using the trial version and the error for the most part.

Thanks!

+6
source share
1 answer

When you rebase on a branch, you rewrite this branch history. All affected commits will at least change their identifiers (even if there was no content change in the commit). Because of this, it might seem that you have duplicate commits that you don't have. You have two different commits with the same content.

A way to reproduce this “duplicate commit” behavior is usually to reinstall a branch that has already been moved to a remote repository, and then merge it back into the same remote branch. Babaza will change the identifiers of your commits, and the merge will hold both pairs of "duplicated" commits, even if their changes lead to the same content.

  • In your existing repository, create a new "feature" branch from the "master" branch: git checkout -b feature
  • Add a new file "file-feature.txt" and transfer it to the "feature" branch: git add . ; git commit -m "added new file on master branch" git add . ; git commit -m "added new file on master branch"
  • Go to the "master" branch and add another new file there and do it too: git checkout master ; 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"
  • Click on the function branch in the remote repository: git checkout feature ; git push --set-upstream origin feature git checkout feature ; git push --set-upstream origin feature
  • Now reformat the "feature" branch to the "master" branch: git rebase master
  • If you try to drag this feature branch to the remote repository, you will get an error message that you need to run git pull first. This is the moment when you actually get your “duplicate” commits due to merging ( git pull is short for git fetch ; git merge ), also remember that the contents of commits are the same, but due to branch redirection, commit identifiers are now are different, and therefore they are considered as another thing, therefore: git pull
  • Inspect your branch in the GUI using " git gui " (go to the main menu - "Repository" - "Visualization History"), and you will see something like this: enter image description here
  • Or just use git log to see it directly on the command line: enter image description here
+1
source

All Articles