I’ve been working on my local master branch on a new feature that is not yet ready to be pushed live for production. However, I just discovered a separate bug in my live app, and so I quickly fixed it locally. However, I want to push this bug fix to my remote master branch without pushing this new feature I’ve been working on. How might I do this?
Share
Less serious answer:
By going back in time and using a proper branch model for your development. Stop working on your production branch and start using feature branches. The situation you describe is exactly why you should be using branches: The ability to set your development work aside, check out master, perform bug fixes and return to your development branch. By ignoring branching, you’re ignoring a lot of what makes Git awesome.
More serious answer:
More practically, you can reorder the commits, point your local master to the one you want to push, push, and then check out your development commit as a new branch.
If your commit history looks like this:
You can use
git rebase -i HEAD~2to re-order the last two commits to look like this (just switch the order of the lines in the editor that comes up):Make a note of the SHA1 of
B, you’re about to temporarily cut it out of your master branch. Once you’ve recorded the SHA1, you can usegit update-ref refs/heads/master [SHA1 of A], which results inYou can now
git pushto mergeAintoorigin/masterand send the results toorigin.Lastly, to get your development work (commit
B) back, create a new development branch (which you should have done in the first place) pointing at yourBcommit:git branch development [SHA1 of B]Your repository will now look like this:
When you’re ready to have that development work merged into master, you can: