简体   繁体   English

从主人变基时推分支

[英]Pushing branch when rebasing from master

We are a development team of 5 people. 我们是一个由5人组成的开发团队。 We have a master branch and whenever we work on something new we just create a new branch. 我们有一个主分支,每当我们处理新事物时,我们就会创建一个新的分支。 When we're done making that change, we push the branch up to GitLab and do a merge/pull request so it gets merged with the master again. 当我们完成更改后,我们将分支推送到GitLab并执行合并/拉取请求,以便它再次与主服务器合并。

I have a branch on my machine that I've been working on for about a week. 我的机器上有一个分支,我已经工作了大约一个星期。 In that week there has been changes to master, so I do this command on my branch: 在那一周,主人有变化,所以我在我的分支上执行此命令:

git rebase master

Once that's done, I do this to push my new branch up: 一旦完成,我这样做是为了推动我的新分支:

git push origin some-branch

When I do that, I get this error: 当我这样做时,我收到此错误:

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.

I do what it suggests, I do a git pull origin some-branch . 我做它的建议,我做一个git pull origin some-branch Now the files I changed have been modified with ===HEAD> text. 现在,我更改的文件已使用===HEAD>文本进行了修改。 So I fix that, and recommit my work. 所以我解决了这个问题,并重新开始工作。

Then, I try to do git push origin some-branch again and the exact same error comes out. 然后,我尝试再次执行git push origin some-branch ,并出现完全相同的错误。

To make it a little more clear, git status on some-branch returns: 为了使它更清晰,某些分支返回的git status

# On branch some-branch
# Your branch and some-branch have diverged,
# and have 3 and 3 different commits each, respectively.

How can I fix this, or am I doing something wrong? 我怎么能解决这个问题,或者我做错了什么?

You should be force-pushing your branch as opposed to merging the changes in. That is, use git push -f after you rebase. 你应该强制推动你的分支,而不是合并更改。也就是说,在你重组之后使用git push -f

When you perform a rebase, you are telling Git to replay the history of master onto your branch, and subsequently, the commits of your branch need to fall in place after the work that was replayed. 当你执行rebase时,你告诉Git将master的历史重放到你的分支上,然后,你的分支的提交需要在重放的工作之后到位。

This changes the history of that branch, which explains why you are seeing the divergence message in git status , but this is entirely intentional. 改变了该分支的历史记录 ,这解释了为什么你在git status看到分歧消息,但这完全是故意的。 However, in merging that in with your remote branch, you are greatly confusing your history; 但是,在与远程分支合并时,您的历史混乱; it now contains merge commits that should never have existed. 它现在包含应该永远不存在的合并提交。

Use a force-push instead next time, but be certain that the work that you've rebased onto passes your tests, be they smoke tests, unit/integration tests, or otherwise. 下次使用强制推送,但要确保您已重新设置的工作通过测试,无论是冒烟测试,单元/集成测试,还是其他。

You can try create a stash for the local changes before make the pull... 您可以尝试为本地更改创建一个存储区,然后再进行拉动...

$ git commit -m "your commit" 
$ git stash
$ git pull
$ git stash apply
$ git push

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

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