Git: duplicate commits after local rebase, then pull
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
- How can I prevent this from happening? How can I mirror my local rebase operation on the remote repo?
- How can I remedy this situation? What would be the most elegant way to clean up th开发者_运维技巧e history and show only one commit?
A rebase is rewriting history - to avoid trouble don't rebase things that are pushed.
You can
push --force
whileA
is checked out.origin/A
history will be overwritten with your version ofA
. Note that this will require manual intervention from other developers in their repos afterwards.
How can I prevent his from happening? How can I mirror my local rebase operation on the remote repo?
How can I remedy this situation? What would be the most elegant way to clean up the history an show only one commit?
Delete the remote branch and repush your new rebased branch. If other members of your team may have pulled your branch 'A' let them know to delete that branch and repull a fresh version.
精彩评论