Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: duplicate commits after local rebase, then pull

Tags:

git

Background:

I have a feature branch A that is one commit ahead of my development branch:

3   (develop, origin/develop)
| 2 (A, origin/A) some feature branch commit
|/
1   some commit

Then I rebase A on develop (git checkout A, git rebase develop), so I get:

2'  (A) some feature branch commit
|
3   (develop, origin/develop)
| 2 (origin/A) some feature branch commit
|/
1   some commit

Now I can no longer push A to origin as Git will reject a non-fast forward commit. It tells me to first pull the remote changes.

When I do so and then push, I end up with the following history:

4   (A, origin/A) merged origin/A into A
|\
2'| some feature branch commit
| |
3 | (develop, origin/develop)
| 2 (origin/A) some feature branch commit
|/
1   some commit

I end up with a history containing the 2 commit twice -- technically different commits although they do the same thing.

Questions

  1. How can I prevent this from happening? How can I mirror my local rebase operation on the remote repo?
  2. How can I remedy this situation? What would be the most elegant way to clean up the history and show only one commit?
like image 934
avdgaag Avatar asked Sep 18 '11 15:09

avdgaag


People also ask

What happens if I rebase twice?

Rebasing a branch on another "re-applies" (fairly smartly, these days) the commits of the currently checked out branch on top of the tip of the target. So, yes, if you do it over and over again then you will keep your current branch up to date.

Does rebase rewrite commit history?

Changing older or multiple commits. To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.

What is git Double commit?

When you save and exit the editor, Git applies all two changes and then puts you back into the editor to merge the three commit messages: # This is a combination of commits. # The first commit's message is: Removed most clearfixs in templates # This is the 2nd commit message: Removed most clearfixs in templates.


1 Answers

  1. A rebase is rewriting history - to avoid trouble don't rebase things that are pushed.

  2. You can push --force while A is checked out. origin/A history will be overwritten with your version of A. Note that this will require manual intervention from other developers in their repos afterwards.

like image 114
Paweł Obrok Avatar answered Oct 26 '22 17:10

Paweł Obrok