git rebase -i 在 rebase 和强制推送后显示错误的提交历史 [英] git rebase -i shows wrong commit history after a rebase and force push

查看:51
本文介绍了git rebase -i 在 rebase 和强制推送后显示错误的提交历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将一个分支基于另一个分支后,我遇到了一个有趣的问题;git rebase -i HEAD~n 不仅会显示错误的提交历史(旧分支),还会显示错误的提交数量.

I encountered an interesting problem after rebasing a branch on another branch; git rebase -i HEAD~n shows not only the wrong commit history (the old branch) but also the incorrect amount of commits.

我希望能够在正确的提交历史记录上执行 git rebase -i HEAD~n,以便从基于我的分支的旧分支中压缩剩余的提交.

I want to be able to do a git rebase -i HEAD~n on the correct commit history in order to squash a leftover commit from the old branch I based my branch on.

# On another Feature branch
git branch -b NewFeautureBranch
# Develop my commit
git add editedfile.extension
git commit -m "Commit message"
# I squashed and merged the feature branch into the development branch, as well as some other feature branches
git fetch --all
git checkout DevelopmentBranch
git pull
git checkout NewFeatureBranch
git rebase DevelopmentBranch
git push -f

运行 git loggitk 将显示应有的历史记录,这是我当前的提交和它最初基于的功能分支的旧提交,正如预期的那样.但是,如果我随后运行 git rebase -i HEAD~4 我会得到旧分支的历史记录以及 11 次提交,而不是我要求的 4 次:

Running git log and gitk will show the history as it should be, which is my current commit and an older commit from the feature branch it was originally based on, as expected. However, if I then run git rebase -i HEAD~4 I get the old branch's history as well as 11 commits instead of the 4 I asked for:

# Git rebase -i HEAD~4 opens up in VIM like this:
pick f79316dc Commit on old Branch base
pick ba742f2f Commit on old Branch base
pick 7577ea7a Commit on old Branch base
pick 91c4088c Commit on old Branch base
pick 98feed6d Commit on old Branch base
pick e8a73d78 Commit on old Branch base
pick 198f79e7 Commit on old Branch base
pick 10bb699c Commit on old Branch base
pick 1d15a926 Commit on old Branch base
pick 0cf569bb Previous commit I want to squash with
pick 470de8d9 Current FeatureBranch commit

但是 git log 会打印:

# As it should be when seeing git log and gitk
commit 470de8d92bb490dd14c31d5741b7edec82ca7597 (HEAD -> FeatureBranch, origin/FeatureBranch)
Author: evFox <evFox@fake-email.com>
Date:   Wed Jan 3 16:08:24 2018 +0100
    Feature commit message

commit 0cf569bba43b5747831a28d6cb42209dab1c2ffa
Author: evFox <evFox@fake-email.com>
Date:   Wed Jan 3 12:09:48 2018 +0100
    Feature from old branch still relevant to this; what I want to merge with the later commit.

commit 982c30d9c3b46539340fe48c241192e377d3e136 (origin/Development, Development)
Merge: ab6e7c9d 1d15a926
Author: evFox <evFox@fake-email.com>
Date:   Tue Jan 9 10:51:06 2018 +0000
    Merged PR XX: Merge OldFeatureBranch to Development

我尝试过的

我尝试在本地删除分支并从远程存储库中提取一个新分支:

What I've tried

I've tried to delete the branch locally and pull a fresh one off of the remote repository:

git checkout DevelopmentBranch
git branch -D NewFeatureBranch
git checkout -b NewFeatureBranch origin/NewFeatureBranch

我试图通过增加 git rebase -i 中的提交来获取更多历史记录,我可以清楚地看到这些提交是旧的,属于 OldFeatureBranch ,就像它在更新之前一样合并:

I have tried to get more history by increasing the commits in the git rebase -i and I can clearly see that the commits are old, belonging to the OldFeatureBranch as it was before it was updated and merged:

# Git rebase -i HEAD~20 opens up in VIM like this:
pick bt67f432 Commit on old version DevelopmentBranch
pick 5g67f33s Commit on old version DevelopmentBranch
pick rt53d563 Commit on old version DevelopmentBranch
pick ew5r45fg Commit on old version DevelopmentBranch
pick 9gy3f74f Commit on old version DevelopmentBranch
pick 58u5hh63 Commit on old version DevelopmentBranch
pick 34fdg5d5 Commit on old version DevelopmentBranch
pick n678hcn7 Commit on old version DevelopmentBranch
pick mh7y88dr PR merge of DevelopmentBranch where old Branch base were branched out originally, but not where DevelopmentBranch was when old Branch Base was squashed and merged.
pick f79316dc Commit on old Branch base
pick ba742f2f Commit on old Branch base
pick 7577ea7a Commit on old Branch base
pick 91c4088c Commit on old Branch base
pick 98feed6d Commit on old Branch base
pick e8a73d78 Commit on old Branch base
pick 198f79e7 Commit on old Branch base
pick 10bb699c Commit on old Branch base
pick 1d15a926 Commit on old Branch base
pick 0cf569bb Previous commit I want to squash with
pick 470de8d9 Current FeatureBranch commit

但问题仍然存在.

我想我可以尝试进行软重置并修改我想要压缩的提交:

I suppose I could try to do a soft reset and amend to the commit I want to squash into:

git reset --soft HEAD~1

但这并不能解决我当前的 git rebase -i HEAD~n 历史记录混乱的问题.

But that doesn't fix my current issue of having the git rebase -i HEAD~n history messed up.

推荐答案

但是,如果我随后运行 git rebase -i HEAD~4 我会得到旧分支的历史记录以及 11 次提交,而不是我要求的 4 次:

However, if I then run git rebase -i HEAD~4 I get the old branch's history as well as 11 commits instead of the 4 I asked for:

您的一个提交是合并.每当你尝试 squash [interactive rebase] 你都会得到所有的提交.如果中间有合并,您将获得此合并中的所有提交.

One of your commits is a merge. Whenever you try to squash [interactive rebase] you will get all the commits. If there is a merge in the middle you will get all the commits form this merge.

这篇关于git rebase -i 在 rebase 和强制推送后显示错误的提交历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆