简体   繁体   中英

Pushing branch when rebasing from master

We are a development team of 5 people. 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.

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 . Now the files I changed have been modified with ===HEAD> text. 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.

To make it a little more clear, git status on some-branch returns:

# 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.

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.

This changes the history of that branch, which explains why you are seeing the divergence message in git status , but this is entirely intentional. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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