开发者

Git: duplicate commits after local rebase, then pull

开发者 https://www.devze.com 2023-04-05 21:46 出处:网络
Background: I have a feature branch A that is one commit ahead of my development branch: 3(develop, origin/develop)

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 th开发者_运维技巧e history and show only one commit?


  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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消