How can I push a specific commit to a remote, and not previous commits?
To push up through a given commit, you can write:
git push <remotename> <commit SHA>:<remotebranchname>
provided <remotebranchname>
already exists on the remote. (If it doesn't, you can use git push <remotename> <commit SHA>:refs/heads/<remotebranchname>
to autocreate it.)
If you want to push a commit without pushing previous commits, you should first use git rebase -i
to re-order the commits.
The other answers are lacking on the reordering descriptions.
git push <remotename> <commit SHA>:<remotebranchname>
will push a single commit, but that commit has to be the OLDEST of your local, non-pushed, commits, not to be confused with the top, first, or tip commit, which are all ambiguous descriptions in my opinion. The commit needs to the oldest of your commits, i.e. the furthest from your most recent commit. If it's not the oldest commit then all commits from your oldest, local, non-pushed SHA to the SHA specified will be pushed. To reorder the commits use:
git rebase -i HEAD~xxx
After reordering the commit you can safely push it to the remote repository.
To summarize, I used
git rebase -i HEAD~<number of commits to SHA>
git push origin <post-rebase SHA>:master
to push a single commit to my remote master branch.
References:
- http://blog.dennisrobinson.name/push-only-one-commit-with-git/
- http://blog.dennisrobinson.name/reorder-commits-with-git/
See also:
- git: Duplicate Commits After Local Rebase Followed by Pull
- git: Pushing Single Commits, Reordering with rebase, Duplicate Commits
I'd suggest using git rebase -i
; move the commit you want to push to the top of the commits you've made. Then use git log
to get the SHA of the rebased commit, check it out, and push it. The rebase will have ensures that all your other commits are now children of the one you pushed, so future pushes will work fine too.
Cherry-pick works best compared to all other methods while pushing a specific commit.
The way to do that is:
Create a new branch -
git branch <new-branch>
Update your new-branch with your origin branch -
git fetch
git rebase
These actions will make sure that you exactly have the same stuff as your origin has.
Cherry-pick the sha id
that you want to do push -
git cherry-pick <sha id of the commit>
You can get the sha id
by running
git log
Push it to your origin -
git push
Run gitk
to see that everything looks the same way you wanted.