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?
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With