git:推送一次提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1789017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:23:59  来源:igfitidea点击:

git: push a single commit

git

提问by IttayD

Say I made several commits and wish to cherry pick which ones I push to the remote repository. How can I do that (in ascii: C1->C2->C3->C4 and I want to push C2 and C4). Will reordering with rebase, resetting, pushing and then resetting work? (C1->C2->C3->C4 => C2->C4->C1->C3 => reset C4 => push => reset C3). Is there a nicer way?

假设我做了几次提交,并希望挑选我推送到远程存储库的那些。我该怎么做(在 ascii 中:C1->C2->C3->C4,我想推送 C2 和 C4)。使用 rebase、reset、push 和 reset 重新排序是否有效?(C1->C2->C3->C4 => C2->C4->C1->C3 => 重置 C4 => 推 => 重置 C3)。有更好的方法吗?

采纳答案by midtiby

If you have your commits on a private branch, you can cherry pick commits from the private branch and apply them to the official branch. At this point you can now push all your commits on the official branch (which is the subset that you previously cherry picked).

如果您在私有分支上有提交,则可以从私有分支中挑选提交并将它们应用到官方分支。此时,您现在可以将所有提交推送到官方分支(这是您之前精心挑选的子集)。

回答by user2394284

What you're looking for:

你在找什么:

git push origin commit-id:master

Credit goes to: http://blog.dennisrobinson.name/push-only-one-commit-with-git/

归功于:http: //blog.dennisrobinson.name/push-only-one-commit-with-git/

Explanatory notes:

解释性说明:

  • Pushing a commit pushes all commits before it (as Amber said). The key is to reorder your commits (git rebase -i) first, so they are in the order you want to push them.
  • The suggested branch + cherry-pick method (suggested by midtiby) works too, but why create throwaway branches when you don't need to.
  • commit-iddoesn't have to be a sha1. To push everything before the last N commits, use "HEAD~N" in place of commit-id.
  • 推送提交会推送它之前的所有提交(如 Amber 所说)。关键是首先重新排序您的提交 ( git rebase -i),以便它们按照您想要推送的顺序排列。
  • 建议的分支 + 挑选方法(由 midtiby 建议)也有效,但为什么在不需要时创建一次性分支。
  • commit-id不必是 sha1。要在最后 N 次提交之前推送所有内容,请使用“HEAD~N”代替commit-id


If pushing to a branch that doesn't exist in the remote repository yet, prefix the remote branch with refs/heads/, such as:

如果推送到远程存储库中尚不存在的分支,请在远程分支前加上 前缀refs/heads/,例如:

git push origin HEAD~1:refs/heads/completely-new-branch

(If not, git will punish you with this hopeless error message).

(如果没有,git 会用这个绝望的错误信息惩罚你)。

回答by Nikhil Thombare

$ git push <remote name> <commit hash>:<remote branch name>

# Example:
$ git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master

回答by Amber

IIRC, due to how git considers commits to work, C4 inherently includes C3, so the concept of "pushing C4 but not C3" doesn't make sense to git (and likewise C2 relative to C1). (See the answer to this previous question.)

IIRC,由于 git 考虑提交工作的方式,C4 固有地包含 C3,因此“推 C4 但不推 C3”的概念对 git 没有意义(同样 C2 相对于 C1)。(请参阅上一个问题的答案。)