Reputation: 6066
I see that similar questions have been asked many times, but I still don't understand and I need a practical example for my case.
I have a github repo with two branches:
master
which is my development branch and where I want to push all latest changes and new featuresstable
where I want to push bug fixings only until new features have been tested.Now I have made 4 commits and pushed them to master
. First three commits are new features, last commit is a bug fixing.
In github, if I select stable
branch, it shows that it is 4 commits behind master (which is correct).
Now I want last commit only to be pushed to stable branch. How do I achieve that?
Upvotes: 4
Views: 2961
Reputation: 24156
You can cherry-pick the last-commit-hash
in your stable
branch and push to remote.
$ git fetch
$ git log # copy the last-commit-hash
$ git checkout -b stable origin/stable # create local/stable branch from remote/stable
$ git cherry-pick <last-commit-hash> # take the last-commit
$ git push origin HEAD # update remote/stable
Upvotes: 5
Reputation: 2191
Solution:
You can use git cherry-pick to apply a single commit by itself to your current branch.
git checkout stable
git cherry-pick <last_commit_id>
Background:
git cherry-pick will apply the changes made in an existing commit to another branch, while recording a new commit. Essentially, you can copy commits from branch to branch.
Given the following tree
dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
\
76cada - 62ecb3 - b886a0 [feature]
Let's say we want to copy b886a0 to master (on top of 5a6057).
We can run
git checkout master
git cherry-pick b886a0
Now our tree will look something like:
dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 - a66b23 [master]
\
76cada - 62ecb3 - b886a0 [feature]
Upvotes: 2
Reputation: 16131
You want to selectively take commits from another branch so this sounds exactly like what cherry-pick is supposed to do. Just checkout stable and do git cherry-pick <commit hash>
As for why, you may want to read on git workflows, specifically the one called gitflow. It seems to me like you need a branch like feature in which you do bug fixes and the it can be merged back into the "stable" mainline. Here you just do a gitmerge whereas when you mix feature development and bug fixes in the same branch you run into your situation where you have to cherrypick and carefully ignore commits for new features to get to the bug fix commits that you want.
Upvotes: 0