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:

  • http://blog.dennisrobinson.name/push-only-one-commit-with-git/
  • http://blog.dennisrobinson.name/reorder-commits-with-git/

  • 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:

  • 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
  • 链接地址: http://www.djcxy.com/p/1388.html

    上一篇: 如何恢复已经推送到远程分支的合并提交?

    下一篇: 我如何将特定提交推送到远程,而不是以前的提交?