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?
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.
git push <remotename> <commit SHA>:<remotebranchname>
works. the trick is to combine it with git rebase -i
to move the commit you want as the first commit, and specify that commit-sha
git push <remotename> <commit SHA>:refs/heads/<new remote branch name>
. After this, push as the answer describes.
Commented
Sep 7, 2012 at 9:54
git push origin HEAD~1:master
.
Commented
May 1, 2013 at 20:58
-f
flag.
Commented
Jun 16, 2014 at 14:42
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 be 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:
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.
git rebase -i
will be ideal solution as suggested in above solutions. Cherry pick must be used only when you want to duplicate the commit.
Commented
Mar 4, 2015 at 10:35
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.
git log
step?
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}
cherry-pick
the commit you want.
The simplest way to accomplish this is with 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.
You could also, in another directory: