简体   繁体   中英

Correctly rebasing git branch

I have a branch history something like the following:

develop --- o ---- o
|
feature

I wanted to make changes in the feature branch, so I did the following

git checkout develop
git pull origin develop
git fetch origin feature:feature
git rebase develop

Then I made my changes in the feature branch. Now, I try to push the feature branch and I get

error: failed to push some refs to 'https://github.com/test/test.git'
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.

This is probably due to the remote branch not being rebased. What is the correct way to solve this problem? I dont want to use git push -f

Assuming your develop local branch is upto date with the remote develop branch here is what you need to do

git checkout feature
git rebase origin/develop
git push origin feature --force

what you are doing above is , you rebasing your local feature branch with the local develop branch since , we assumed that your local and remote develop branches are now the same .

Also another point i want to make is , when you pull from the remote , always try to pull and rebase . So that you always know that your local <branchname> and remote <branchname> have identical copies and you don't need to do a separte rebase . here is how you do that.

git checkout <branchname>
git pull --rebase origin <branchname> 

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