Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git-排除特定的提交和推送_Git_Git Push_Git Commit - Fatal编程技术网

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

Git-排除特定的提交和推送,git,git-push,git-commit,Git,Git Push,Git Commit,如何从一系列提交中排除特定提交。我的意思是如果我有5次提交,我只想推4次提交。如何做到这一点。请帮助解决此问题。您将需要具有所需提交的新分支 你可以用几种方法来做 建议的解决方案: git cherry pick 将新分支签出到要开始的特定sha-1: git checkout <origin branch> git checkout -b <new branch> # now cherry-pick the desired commits onto the nee

如何从一系列提交中排除特定提交。我的意思是如果我有5次提交,我只想推4次提交。如何做到这一点。请帮助解决此问题。

您将需要具有所需提交的新分支

你可以用几种方法来做


建议的解决方案:
git cherry pick
将新分支签出到要开始的特定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>
使用git revert to撤消在不需要的提交中所做的更改,结果将是旧代码和新代码的分支,但当前状态将是原始代码


git-rebase-i
交互式重基。选择不需要的提交并将其删除

# X is the number of commits you wish to squash
git rebase -i HEAD~X
挤压提交后,选择
e
进行编辑并放置所需的代码,然后添加并提交


git过滤器分支
过滤器分支可用于过滤您想要的任何内容

git filter-branch --env-filter '<do what ever you want on this commit range' SHA1..SHA1
git-filter分支--env-filter'使用(将
1
替换为要从顶部忽略的提交数):

注意:要使此命令起作用,远程分支必须存在,否则会出现
错误:无法推送到不合格的目标
。如果出现错误,例如,您可以像往常一样开始推送分支(即包括您不想推送的提交),然后使用附加参数
--force
重复上述命令

其他备选方案(旧答案) 只是想指出一个替代方案,因为创建一个单独的分支,然后做一些魔术,然后删除它,听起来太麻烦了;尤其是如果您已经打开了一个拉请求,并且您需要准确地推送当前所在的分支

一种更简单的方法是(但请不要将其与其他git命令混在一起,否则您可能需要在
reflog
中挖掘要还原的点):

这里使用的技巧是git通常在本地存储您在名为
reflog
的地方执行的破坏性操作。您可以使用
git reflog
命令查看其内容(或者通常可读性更高的
git reflog--date=iso
,不过在本例中,您不会看到更容易编写的标记
HEAD@{n}


如果您不自信,更安全的版本可能是:

$ 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

@YaronIdan的可能副本我与合并无关。我想知道当我不需要推送很少的提交时,如何推送我的提交。所以,正如前面提到的,我需要创建新的分支来实现这一点。这意味着for将在10次提交中推出9次。我需要遵循同样的方法。?完全正确。将所需的提交推到一个新的分支。这太累了。没有办法排除我不想推送的提交吗?@Ved是的,有,请参见下面的答案。我喜欢补丁方法-无状态隐藏。:chef_kisses:for
git push origin HEAD~1:$(git rev parse--abbrev ref HEAD)
git push origin HEAD~1:$(git rev-parse --abbrev-ref HEAD)
$ 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
$ 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