简体   繁体   English

Git - 排除特定的提交和推送

[英]Git - exclude specific commit and push

How to exclude specific commit from series of commits.如何从一系列提交中排除特定提交。 I mean if I have 5 commits, and I only want to push 4 commits.我的意思是如果我有 5 次提交,而我只想推送 4 次提交。 How to do this.这该怎么做。 Please help to resolve this.请帮助解决这个问题。

You will need to have a new branch with the desired commits.您将需要有一个包含所需提交的新分支。

You can do it in several ways您可以通过多种方式做到这一点


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 new branch
git cherry-pick commit1 commit2 ... commitN

# now push to remote
git push origin remote <branch name>

Other options:其他选项:

git revert

git revert SHA-1

Use git revert to undo the 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

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

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

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 .注意:要使此命令工作,远程分支需要存在,否则您会收到error: unable to push to unqualified destination If you're getting the error, you may for example start with pushing the branch as usual (ie including the commits you didn't want to push) , and then repeat the command above with the additional argument --force .如果您收到错误消息,例如您可以像往常一样开始推送分支(即包括您不想推送的提交) ,然后使用附加参数--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 .这里使用的技巧是 git 通常在本地存储您执行的破坏性操作,称为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 --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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM