Git - 排除特定的提交和推送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36112517/
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
Git - exclude specific commit and push
提问by Ved
How to exclude specific commit from series of commits. I mean if I have 5 commits, and I only want to push 4 commits. How to do this. Please help to resolve this.
如何从一系列提交中排除特定提交。我的意思是如果我有 5 次提交,而我只想推送 4 次提交。这该怎么做。请帮助解决这个问题。
回答by CodeWizard
You will need to have a new branch with the desired commits.
您将需要有一个包含所需提交的新分支。
You can do it in several ways
您可以通过多种方式做到这一点
Recommended solution:
推荐解决方案:
git cherry-pick
git cherry-pick
Checkout a new branch to the specific sha-1 you want to start from:
将新分支签出到您要从中开始的特定 sha-1:
git checkout <origin branch>
git checkout -b <new branch>
# now cherry-pick the desired commits onto the nee branch
git cherry-pick commit1 commit2 ... commitN
# now push to remote
git push origin remote <branch name>
Other options:
其他选项:
git revert
git revert
git revert SHA-1
Use git revert to undothe changes you have made in the unwanted commit, the result will be branch with the old code and the new code but the current state will be the original code
使用 git revert撤消您在不需要的提交中所做的更改,结果将使用旧代码和新代码分支,但当前状态将是原始代码
git rebase -i
git rebase -i
Interactive rebase. choose the commit you don't want and remove it.
交互式变基。选择您不想要的提交并将其删除。
# X is the number of commits you wish to squash
git rebase -i HEAD~X
Once you squash your commits - choose the e
for edit and place the code you want it to be, add and commit
一旦你压缩你的提交 - 选择e
编辑并放置你想要的代码,添加并提交
git filter-branch
git filter-branch
Filter branch can be used to filter any content you want.
过滤器分支可用于过滤您想要的任何内容。
git filter-branch --env-filter '<do what ever you want on this commit range' SHA1..SHA1
回答by Hi-Angel
Use (replace the 1
with the number of commits you want to ignore from the top):
使用(替换为1
您想从顶部忽略的提交数):
git push origin HEAD~1:$(git rev-parse --abbrev-ref HEAD)
Note: for this command to work the remote branch needs to exist, or you'd get an error: unable to push to unqualified destination
. If you're getting the error, you may for example start with pushing the branch as usual (i.e. including the commits you didn't want to push), and then repeat the command above with the additional argument --force
.
注意:要使此命令工作,远程分支需要存在,否则您将获得一个error: unable to push to unqualified destination
. 如果您遇到错误,例如您可以像往常一样开始推送分支(即包括您不想推送的提交),然后使用附加参数重复上述命令--force
。
Other alternatives (old answer)
其他选择(旧答案)
Just wanted to note an alternative, because creating a separate branch, then doing some magic, then deleting it, sounds like too much hassle; especially so if you already has a pull request opened, and you need to push exactly the branch you're currently on.
只是想说明一个替代方案,因为创建一个单独的分支,然后做一些魔术,然后删除它,听起来太麻烦了;特别是如果您已经打开了一个拉取请求,并且您需要准确地推送您当前所在的分支。
A simpler way is (but please, don't intersperse it with other git commands, or you may need to dig in reflog
for the point to restore):
一种更简单的方法是(但请不要将它与其他 git 命令穿插在一起,否则您可能需要挖掘reflog
要恢复的点):
$ git reset --hard HEAD~1 # temporarily erase commits, use in place of "1" the number of commits you want to ignore
$ git push myorigin HEAD # do the push wherever you wanted
$ git reset --hard HEAD@{1} # restore commits
The trick used here is that git usually locally stores destructive operations you did in place called reflog
. You can see its content with git reflog
command (or the usually more readable git reflog --date=iso
, though you won't see the easier to write marks HEAD@{n}
in this case).
这里使用的技巧是 git 通常在本地存储您在名为reflog
. 您可以使用git reflog
命令查看其内容(或通常更具可读性的git reflog --date=iso
,尽管HEAD@{n}
在这种情况下您不会看到更容易写的标记)。
If you don't feel confident, a safer version might be:
如果你没有信心,一个更安全的版本可能是:
$ git format-patch -1 --stdout > 1.patch # store commits in a file, use in place of "1" the number of commits you want to ignore
$ git reset --hard HEAD~1 # temporarily erase commits, use in place of "1" the number of commits you want to ignore
$ git push myorigin HEAD # do the push wherever you wanted
$ git am 1.patch # restore commits