How can I push a specific commit to a remote, and not previous commits?
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:
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.
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, ie 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:
上一篇: 如何恢复已经推送到远程分支的合并提交?