I have made several commits on different files, but so far I would like to push to my remote repository only a specific commit.
Is that possible?
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:
See also:
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.
I believe you would have to "git revert" back to that commit and then push it. Or you could cherry-pick
a commit into a new branch, and push that to the branch on the remote repository. Something like:
git branch onecommit
git checkout onecommit
git cherry-pick 7300a6130d9447e18a931e898b64eefedea19544 # From the other branch
git push origin {branch}
The simplest way to accomplish this is to use two commands.
First, get the local directory into the state that you want. Then,
$ git push origin +HEAD^:someBranch
removes the last commit from someBranch
in the remote only, not local. You can do this a few times in a row, or change +HEAD^
to reflect the number of commits that you want to batch remove from remote. Now you're back on your feet, and use
$ git push origin someBranch
as normal to update the remote.