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?
Join Stack Overflow to learn, share knowledge, and build your career.
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.
EDIT
to a more complete description of what to do check @Samuel's answer: https://stackoverflow.com/a/27907287/889213
References:
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
– dminer
Jan 6 '12 at 20:32
git push <remotename> <commit SHA>:refs/heads/<new remote branch name>. After this, push as the answer describes.
– Wes Oldenbeuving
Sep 7 '12 at 9:54
git push origin HEAD~1:master.
– artless noise
May 1 '13 at 20:58
-f flag.
– Ian Vaughan
Jun 16 '14 at 14:42
Tried the suggested solution:
git push <remotename> <commit SHA>:<remotebranchname>
like this:
git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master
In my case master was 5 commits ahead and I just wanted to push my last commit but the above ended up pushing all of my changes up to and including the named commit. It seems to me that the cherry-pick method might be a better approach for this usecase.
git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master will push all the commits before 712acff81033eddc90bb2b45e1e4cd031fefc50f including itself. If you want to only push 712acff81033eddc90bb2b45e1e4cd031fefc50f, then you should use git rebase -i to reorder the commit as the first one, and then run git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master
– dminer
Jan 6 '12 at 20:36
git push origin <rebased-SHA>:master
– Tripp Lilley
Jan 22 '14 at 17:49
git push -f ... to force the push. Not a good idea generally, but in local, dev, testing, and non-prod it happens. Just had to A/B test a deploy change on a staging server, also good for cheap ghetto roll-backs.
– Marc
Oct 27 '15 at 0:53
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.
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.
– Vinay Bhargav
Mar 4 '15 at 10:35
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 repo. Something like :
git branch onecommit
git checkout onecommit
git cherry-pick 7300a6130d9447e18a931e898b64eefedea19544 # From the other branch
git push origin {branch}