git push:推送除最后一次以外的所有提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8879375/
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-19 06:20:49  来源:igfitidea点击:

git push: Push all commits except the last one

gitpush

提问by python dude

Is there a way to push all my local commits to the remote repository except the most recent one? I'd like to keep the last one locally just in case I need to do an amend.

有没有办法将我所有的本地提交推送到远程存储库,除了最近的提交?我想在本地保留最后一个,以防万一我需要修改。

回答by KL-7

Try this (assuming you're working with masterbranch and your remote is called origin):

试试这个(假设你正在使用master分支并且你的遥控器被称为origin):

git push origin HEAD^:master

HEAD^points to the commit before the last one in the current branch (the last commit can be referred as HEAD) so this command pushes this commit (with all previous commits) to remote origin/masterbranch.

HEAD^指向当前分支中最后一个提交之前的提交(最后一个提交可以称为HEAD),因此该命令将此提交(以及所有先前的提交)推送到远程origin/master分支。

In case you're interested you can find more information about specifying revisions in this man page.

如果您有兴趣,可以在此手册页中找到有关指定修订的更多信息。

Update:I doubt that's the case, but anyway, you should be careful with that command if your last commit is merge. With merge commit in the HEADHEAD^refers to the first parent of that commit, HEAD^2- to its second parent, etc.

更新:我怀疑是这样,但无论如何,如果您的最后一次提交是合并,您应该小心使用该命令。合并提交是HEADHEAD^指该提交的第一个父级,HEAD^2- 到它的第二个父级,等等。

回答by SherylHohman

A more general approach that works to pushup to a certain commit, is to specify the commit hash.

一种适用于push某个提交的更通用的方法是指定提交哈希。

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

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

For example, if you have these commits:
111111<-- first commit
222222
333333
444444
555555
666666<-- last commit

例如,如果您有这些提交:
111111<-- 第一次提交
222222
333333
444444
555555
666666<-- 最后一次提交

git push origin 555555:master

..Will push all but your last commit to your remote masterbranch, and

.. 将除了最后一次提交之外的所有内容推送到远程master分支,并且

git push origin 333333:myOtherBranch  

..Will push commits up to and including 333333to your remote branch myOtherBranch

.. 将提交推送到并包括333333到您的远程分支myOtherBranch

回答by Jarett Millard

Another possibility is to

另一种可能是

git reset --soft HEAD^

to uncommit your most recent commit and move the changes to staged. Then you can

取消提交您最近的提交并将更改移至暂存状态。然后你可以

git push

and it will only push the remaining commits. This way you can see what will be pushed (via git log) before pushing.

它只会推送剩余的提交。通过这种方式,您可以在推送git log之前查看将要推送的内容(通过)。