Git命令 - 推送时过滤提交

4

我已经提交了多个commit,但现在不想推送所有的commit。是否有一种方法只推送我想要的commit?

谢谢

2个回答

5
你有两个选项,

变基

比如说,你想要包含最近的五次提交中的三次:

git rebase -i HEAD~5           # reorder the lines in the text editor, 
                               # leave the 'private' commits at the end
git push origin HEAD~2:master  # push all but the last two

cherry pick

这涉及到一个临时分支,需要进行更多的工作。

git checkout -b temp HEAD~5
git cherrypick <hash1>
git cherrypick <hash2>
git cherrypick <hash3>
git push origin master

建议

  • 我建议不要挑选提交记录,因为这样更容易出错,并且会使相同的提交记录看起来“不同”
  • 使用--cherry-pick--cherry-mark optionsgit log一起使用可以查看引用之间的实际差异(git log --cherry-pick --oneline master...origin/master

如果我在主分支上怎么办? - ashisrai_
那是什么意思?只有分支。如果您坚持:还有“跟踪分支”(也只是分支,但配置为从特定的其他分支推送/拉取)。 - sehe
所以,无论我是在主分支还是其他本地分支中使用上述选项都没有关系。 - ashisrai_
不是从技术上来说。这可能取决于分支的含义(例如,它是否代表公共版本):请参见从上游rebase恢复或“更容易”的修正错误段落 - sehe

1

这个命令可以帮助:

$ git push origin <thelonghash>:master

但是如果你有提交记录 A->B->C->D 并执行 git push origin C:master,则提交记录 A,B,C 将被推送到远程。因此,如果您只需要推送 C,则需要使用 git rebase -i 重新排列提交记录,以便 C 最早。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接