git: Duplicate Commits After Local Rebase Followed by Pull

删除回忆录丶 提交于 2019-12-12 04:56:24

问题


I have a local git repository and I run the following:

git.exe pull -v --no-rebase --progress "origin" // pull 1
(make a few local commits)
git.exe pull -v --no-rebase --progress "origin" // pull 2
git log --pretty=format:"%h - %an : %s"         // log 1
git rebase -i HEAD~4
(move local commit 1 down 2 positions)
git log --pretty=format:"%h - %an : %s"         // log 2
git.exe pull -v --no-rebase --progress "origin" // pull 3
git log --pretty=format:"%h - %an : %s"         // log 3

After doing this all commits to the remote repository that I retrieved in pull 1 are now duplicated in the log.

Log 1 looks like this:

84e4015 - Me : Local Commit 3
0dbe86a - Me : Local Commit 2
d57ba2a - Me : Merge branch 'master' of remote repository
a86ea35 - Me : Local Commit 1 before reordering
2fc4fe7 - Remote User 2 : Remote Commit 2
b7a8656 - Remote User 1 : Remote Commit 1
8ce80fc - Me : Merge branch 'master' of remote repository

Log 2 looks like this:

cf1ff7b - Me : Local Commit 3
cd14463 - Me : Local Commit 2
b9d44fb - Me : Local Commit 1 after reordering
9777c56 - Remote User 2 : Remote Commit 2
a2d7d8b - Remote User 1 : Remote Commit 1
8ce80fc - Me : Merge branch 'master' of remote repository

And log 3 looks like this:

e8e1a85 - Me : Merge branch 'master' of remote repository
cf1ff7b - Me : Local Commit 3
cd14463 - Me : Local Commit 2
b9d44fb - Me : Local Commit 1 after reordering
9777c56 - Remote User 2 : Remote Commit 2
a2d7d8b - Remote User 1 : Remote Commit 1
2fc4fe7 - Remote User 2 : Remote Commit 2 // duplicate 2
b7a8656 - Remote User 1 : Remote Commit 1 // duplicate 1
8ce80fc - Me : Merge branch 'master' of remote repository

What have I done wrong? How do I prevent? How do I fix?

Note that I've never pushed to the remote repository, only pulled from it and made local commits. Also note that there are a lot of similarly titled threads to this question but all of them are a little different and the answers there don't seem to apply here.


回答1:


It's not you, it's the remote users. It looks like they have rebased commits that you have already based work on, then pushed back to origin. Not very sociable.

The clue is that remote users' commit refs change from log1 to log2 - they rebased and pushed their work. But your work is based on their pre-rebased commits. So your local repo contains those two commits (2fc4fe7, b7a8656) that their rebase has eradicated from their repo and origin (presumably they pushed with a --force). So when you then pull from origin those local commits appear to be reincarnated to ensure your commit history is preserved, and the remote users' rebased commits (9777c56, a2d7d8b) are merged too. Hence the duplicates.




回答2:


The problem is with git pull. I should have used:

git pull --rebase

This rebases my local commits so that they are on top, i.e. more recent, than the commits in the remote repo. So when I rebase my commits to reorder one, I'm not rebasing commits that have been pushed to the remote repo. By rebasing commits that had been pushed to the remote repo I was copying them and assigning them a new SHA, and when I did the second git pull it re-pulled the original SHAs/commits, hence the duplicates.

See here for more details:

git: Pushing Single Commits, Reordering with rebase, Duplicate Commits



来源:https://stackoverflow.com/questions/27749096/git-duplicate-commits-after-local-rebase-followed-by-pull

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!