I have pulled the master branch. In the local master I have made several commits. Now I want to push only the last commit (and not the previous ones). How do I achieve this?
I found a good explanation of how to do it: How to push only one commit with Git. So, the command should be something like this:
git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master
Or, in a more "abstract" form:
git push <remote name> <commit hash>:<remote branch name>
However, as far as I understood, in a general case, it will not push the single commit (given by its hash). Instead, it will push the specified commit as well as all commits that have been done before the specified commit.
So, to achieve the goal (pushing only one single commit) we need first to reorder the commits such that the commit that we want to push is see as the oldest one (the first one in the sequence of the commits). How to do it is described here: How to reorder commits with Git. This description is clear, the only thing that confuses me is this phrase:
In my case, I wanted to bring my top most commit where I fixed the project file down to the bottom so that I can push only that one commit to the remote server.
But isn't it the case that we want to do a kind of opposite? If we bring the commit "down to the bottom", it will be considered as the last (most recent) commit and if we use its hash when we push, we will push everything that comes before it (which means "all commits").
So, what we want to do is to put our commit higher in the file that we see when we execute git rebase -i HEAD~5
. Am I right?
If you'd like to push only one commit cfoo
to a branch bbar
, here's a simple solution:
git fetch origin bbar
git checkout FETCH_HEAD
git cherry-pick cfoo
git push origin HEAD:bbar
In a real case, you may need to run git stash
or do something else to backup the workaround.
As for "higher", yes, in the text editor that pops up after git rebase -i
, the first line is the oldest commit. If you'd like to push only one commit, then put it in the first line.