如何从一系列提交中排除特定的提交。我的意思是,如果我有5个提交,并且我只想推送4个提交。如何做到这一点。请帮忙解决这个问题。
您需要创建一个包含所需提交的新分支。
有多种方法可以完成此操作。
git cherry-pick
检出到您想要从特定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>
git revert
git revert SHA-1
使用 git revert 撤销您在不需要的提交中所做的更改,结果将是具有旧代码和新代码的分支,但当前状态将是原始代码。
git rebase -i
交互式变基。选择您不想要的提交并删除它。
# X is the number of commits you wish to squash
git rebase -i HEAD~X
当你压缩提交记录后,选择e
进行编辑,放置你想要的代码,然后添加并提交。
git filter-branch
git filter-branch
可以用于过滤任何你想要的内容。
git filter-branch --env-filter '<do what ever you want on this commit range' SHA1..SHA1
使用(将1
替换为您想要从顶部忽略的提交数):
git push origin HEAD~1:$(git rev-parse --abbrev-ref HEAD)
--force
重复上面的命令。
$ 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
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
git push origin HEAD~1:$(git rev-parse --abbrev-ref HEAD)
,请使用 :chef_kisses:。 - Mark Fox