how do you push only some of your local git commits?
Short answer:
git push <latest commit SHA1 until you want commits to be pushed>
Examples:
git push origin fc47b2:master
git push origin HEAD~2:main
Long answer:
Commits are linked together as a chain with a parent/child mechanism. Thus, pushing a commit actually also pushes all parent commits to this commit that where not known to the remote. This is implicitly done when you git push
the current commit: all the previous commits are also pushed because this command is equivalent to git push HEAD
.
So the question might be rewritten into How to push a specific commit and this specific commit might be HEAD~2, for example.
If the commits you want to push are non-consecutive, simply re-order them with a git rebase -i
before the specific push.
Assuming your commits are on the master branch and you want to push them to the remote master branch:
$ git push origin master~3:master
If you were using git-svn:
$ git svn dcommit master~3
In the case of git-svn, you could also use HEAD~3, since it is expecting a commit. In the case of straight git, you need to use the branch name because HEAD isn't evaluated properly in the refspec.
You could also take a longer approach of:
$ git checkout -b tocommit HEAD~3
$ git push origin tocommit:master
If you are making a habit of this type of work flow, you should consider doing your work in a separate branch. Then you could do something like:
$ git checkout master
$ git merge working~3
$ git push origin master:master
Note that the "origin master:master" part is probably optional for your setup.