Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge after rebasing feature branch in git

Tags:

git

merge

rebase

I rebase my feature branch on origin/develop to be current on the current state of develop.

git checkout feature-X
git rebase origin/develop

But when I do this I have to merge immediately after. This makes things messy.

I thought that the whole idea of rebasing was that you could rewrite history (pretend we're working off the tip of origin/develop) and only have to merge when you want to integrate your changes into origin/develop.

Am I doing something wrong?

Update: I can't exactly reproduce what I did without lots more git magic but I'm trying to piece together a functioning feature-branch model mostly going off this gist.

I "git rebase origin/develop" when I'm in my feature branch. That completes but then tells me I have widely diverged from stuff which I then try to fix by doing a push/pull. That creates a merge which I think I don't want.

like image 665
Alper Avatar asked Oct 14 '15 11:10

Alper


2 Answers

Assuming feature-X has been branched off develop, a better way to do it would be to pull changes from remote onto your local develop branch and rebasing feature-X from the local branch. That will avoid the merge commit. You can merge feature-X later into your local develop and then push it.

If feature-X has been pushed onto the remote, as @crea1 has answered you will have to run this with every rebase from develop

git push -f origin feature-x

The reason being that the rebase creates entirely new commit ids for the rebased commits. As far as git is concerned they are new commits. That is what is causing your git pull to go for a merge commit. As long as no one is using it, force pushing should be fine. Also IMHO, it is better if your pull.rebase is set to true in your gitconfig.

like image 93
TheGeorgeous Avatar answered Oct 16 '22 06:10

TheGeorgeous


If you rebase feature-x on top of origin/develop, you do this on your local repo. This means feature-x is still in the state before rebasing in the remote repository. Since they don't have the same ancestor anymore, git will try to merge.

To update the remote branch, you can do a force push.

git push origin feature-x --force

But beware, if other persons than you are working on feature-x, and starts pushing/pulling it can get messy.

I find this link useful for learning about merging vs rebasing

like image 35
crea1 Avatar answered Oct 16 '22 05:10

crea1