Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge git commits in the develop branch to a feature branch

I have a develop branch and a feature branch in my git repo. I added a commit to develop and now I want that commit to be merged to my feature branch. If I do this

git checkout feature git merge develop 

I end up with a merge commit. Since I'll be merging new commits on develop to my feature branch frequently, I'd like to avoid all these unnecessary merge commits. I saw this answer that suggested doing a git rebase develop but it ends up rewinding my branch way too far and the rebase fails.

Update: What I ended up doing was

git checkout feature git merge develop # this creates a merge commit that I don't want git rebase # this gets rid of the merge commit but keeps the commits from develop that I do want git push 

Update: I just noticed that the original commit on develop gets a different hash when I merge then rebase to the feature branch. I don't think that's what I want because eventually I'll merge feature back into develop and I'm guessing this won't play nice.

like image 959
gitq Avatar asked Jul 25 '13 13:07

gitq


People also ask

How will you merge the branch to the develop branch considering you are in the develop branch?

To merge, you first checkout the branch you want to merge to. This should be master . Then, to merge the development branch into the master branch, you right-click the development branch in your Git client and select “Merge into 'master'”. Fork will ask you whether you want to create a merge commit.

How do I merge a branch with develop?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

How do I merge main branch into feature branch GitHub?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.


2 Answers

To integrate one branch into another, you have to either merge or rebase. Since it's only safe to rebase commits that aren't referenced anywhere else (not merged to other local branches; not pushed to any remote), it's generally better to merge.

If your feature branch is purely local, you can rebase it on top of develop. However, it takes time to understand how rebase works, and before you do, it's quite easy to accidentally produce duplicated or dropped commits. Merge commits might look noisy but merging is guaranteed to always be safe and predictable.

For a better view, try logging everything together in a graph:

git log --all --graph --oneline --decorate 

It's also worth considering whether you really need the commits on develop merged into feature. Often they're things that can be left seperate until feature is merged into develop later.

If you regularly find you do need develop code on feature then it might be a sign that your feature branches are too long-running. Ideally features should be split in such a way that they can be worked on independently, without needing regular integration along the way.

like image 76
ben_h Avatar answered Sep 20 '22 08:09

ben_h


If you only want one commit from the develop branch you can cherry-pick it in your feature branch:

 git checkout feature  git cherry-pick -x <commit-SHA1> 

The commit will be applied as a new one on top of your branch (provided it doesn't generate a conflict), and when you'll merge back the feature branch Git will cope with it without conflicts.

like image 24
CharlesB Avatar answered Sep 17 '22 08:09

CharlesB