简体   繁体   English

git:重置分支吞噬主分支

[英]git: reset branch swallow up master branch

My git history gets something wrong.我的 git 历史记录出错了。 One of the branches steal master branch commits when I use some commands below:当我使用以下一些命令时,其中一个分支会窃取主分支提交:

$ git clone [repo_url]
$ git checkout [branch_A_name]  -- This branch has a commit
$ git reset --soft [revision_hash]   -- same as HEAD~1
$ git add [some_files]
$ git commit
$ git push -f

After this, I found some commits that I already merged to master branch is stolen by branch_A , which I force-pushed just right before.在此之后,我发现一些我已经合并到 master 分支的提交被branch_A窃取了,这是我之前强制推送的。 These diffs are removed from master .这些差异已从master中删除。

Actually, this is not mine.其实,这不是我的。 My teammates are in this situation and I heard they haven't used git rebase .我的队友就是这种情况,我听说他们没有使用git rebase
They just did reset - commit - force push and they saw a messed up tree in their pull request page.他们只是做了reset - commit - force push ,他们在他们的拉取请求页面中看到了一个混乱的树。

We use bitbucket.我们使用 bitbucket。 Thank you.谢谢你。

From what I can understand your teammate probably did据我所知,你的队友可能做了

git reset  --<probably hard> master
<made changes>
git add <changes>
git commit -m <“message”>
git push -f <whatever> 

While this process will work in some situations it will not work in others.尽管此过程在某些情况下有效,但在其他情况下无效。 It's my guess that your teammate let their branch get out of date by not performing git pull on the master or by doing git remote -v update to get the latest changes before the push.我猜你的队友通过不在主服务器上执行git pull或通过执行git remote -v update以在推送前获取最新更改而让他们的分支过时。 He then did a git reset --<whatever> to update and force pushed the branch.然后他做了一个git reset --<whatever>来更新并强制推送分支。 This means that the commit history they pushed diverged from the master in more ways than just the commit they wanted to change and other commits may show up there.这意味着他们推送的提交历史与 master 的不同之处不仅仅是他们想要更改的提交,而且其他提交可能会出现在那里。

To fix this situation they need to为了解决这种情况,他们需要

git checkout master
git pull
git checkout feature branch
git rebase <master or origin/master>
git log 
<check if commits are correct in the log>
git diff --name-only origin/master
<check if only his commit appears>
git push -f origin <branch name> 

Additionally if you are using Forks on Atlassian there is a potential for another situation - their fork is unsynced.此外,如果您在 Atlassian 上使用 Forks,则可能会出现另一种情况 - 他们的 fork 是不同步的。 Though Atlassian forks are usually set up to auto-sync, a git push -f without specifying the branch name can go rogue between an outdated local repository and a fork.尽管 Atlassian 分叉通常设置为自动同步,但git push -f没有指定分支名称可能 go 流氓在过时的本地存储库和分叉之间。 Make sure that the commit history on their master is the same as the one on the main repo and that they unchecked and recheckard the “automatic sync” button in settings.确保他们的 master 上的提交历史与主 repo 上的相同,并且他们取消选中并重新选中设置中的“自动同步”按钮。 Make sure it says that it was last synced a moment ago.确保它说它是最近一次同步的。 From there they'll need to update their local and all that good stuff.从那里他们需要更新他们的本地和所有好东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM