Sungguk Lim
Sungguk Lim

Reputation: 6218

git rebase from master to remote branch.!

I already googled, and read many document. but Unfortunately I cannot understand them all

My situation is.

git branch -r
origin/HEAD -> origin/master
origin/master
origin/team/myteam

git branch -a
master
team/myteam
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/team/myteam

and Now I like to merge every patches from master to remote mybranch.

I tried

git checkout team/myteam

git rebase master

git status
On branch team/myteam 
Your branch and 'origin/team/myteam' have diverged,
and have 238 and 18 diffrent commits each, respectively.
nothing to commit, working directory clean

it seems works.. but push returns error..

I use gerrit.

git push origin HEAD:refs/for/team/mybranch  
...
..
remote: Resolving
deltas: 100% (14481/14481) remote: Processing changes: refs: 1, done
To ssh://156.xxx.xxx.xxx/xxx  ! [remote rejected] HEAD -> refs/for/team/myteam (change 228 closed) 
error: failed to push some refs to 'ssh://156.xxx.xxx.xxx/xxx

Upvotes: 3

Views: 4396

Answers (3)

Gui LeFlea
Gui LeFlea

Reputation: 835

Your problem is a workflow one.

Since you have not historically rebased master under the team/myteam branch before pushing to the remote, this is not possible without a force push. It has not been rebased before and this is why the branches have diverged. In it's current state pushing the rebased merge to the remote is impossible without a force push. Very generally speaking force push is not a good choice unless no one else using the remote branch.

There are many ways to resolve the problem of your local and the remote branches having diverged.

  • You could do a git merge locally then git push to get your remote branch up to date. This probably the simplest.
  • You could deprecate the existing remote branch and push to a new remote branch for your team then coordinate with the team members to start using the new remote branch.
  • You could force push to the remote myteam branch and coordinate with the team members to merge the force push with their local repositories. This could be painful.

To do a git merge on myteam (i.e., branchname), you may need to delete your local branch in order do a clean merge. It's git branch -D branchname to delete a local branch. Then...

git checkout branchname
git pull -r origin branchname
git merge master
git push origin branchname

In the future, you should rebase your local branch with master before each and every push to the remote. There are quicker ways to achieve the rebase than the commands written below, but I've made this verbose for demonstration purposes.

git checkout mybranch
git fetch
git rebase origin/master
git push origin HEAD:refs/for/team/mybranch

PS. Sorry, I don't know Gerrit.

PPS. In git, force push is a combination of two operations, delete remote branch and push local branch to remote.

Upvotes: 3

forvaidya
forvaidya

Reputation: 3315

1) Since you use gerrit and you get change is closed that means you can't push for same ChangeID

2) You workflow is incorrect You should do git fetch origin and then git rebase origin master & Commit and push

Upvotes: -1

cexbrayat
cexbrayat

Reputation: 18402

I assume that you want to push to origin/team/myteam and you are up to date in team/myteam.

If you rebase team/myteam with master, you are rewriting the history of your branch team/myteam so Git doesn't allow you to push it as it conflicts with the history it has in origin/team/myteam.

If you are sure of what you want to do (rewrite the history on the origin repo), you can force push with :

 git push --force

Beware that your team mates might have troubles when they will pull origin/team/myteam : rewriting the shared history is not something recommended.

You might want to do a merge instead of a rebase : in that case you can do a simple push and you will skip potential troubles.

Upvotes: 0

Related Questions