还原项目20次以上的提交,然后仅选择性地重新应用其中的一些

时间:2018-07-06 16:14:41

标签: git github

我有一个git项目,其中进行了几次提交,我想还原大多数提交。但是,在这段时间内,我想保留一些错误修复和其他提交,因此我想执行以下操作:

  1. 将存储库还原为我要还原的第一次提交之前的提交
  2. 经历提交并仅重新应用其中的一些,其中一些仅部分应用

是否有一些git命令序列可以使我实现这一目标?我可以弄清楚如何还原到某些提交,但是我什至不知道是否有可能进行选择性重新提交。

1 个答案:

答案 0 :(得分:3)

您不必还原。您可以使用git rebase in interactive mode编辑历史记录。例如,在我当前的主题分支上,如果我运行git rebase -i master,则会在VIM中得到以下视图:

pick 23299aa6 improve unit test messages
pick cf1c3c0c move sagas tests to where they'll get run
pick f7ff7fa7 use docker-compose up to rebuild the ui image more quickly
pick 544902ec split definitions file into helpers and given, when, then files
pick 55e94e7b remove canary test
pick 8a4b0862 move expectTitleToBe function to helper file

# Rebase 72b74f26..8a4b0862 onto 72b74f26 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

从此视图中,我可以删除不想提交的行,如果我实际上不希望删除canary测试,则可以删除或注释掉该行,或者使用 drop 命令。我可以在提交时停止重新设置,并使用 edit 更改要提交的内容(您可以在此处通过删除更改来部分提交 ),对于列出的其他命令,依此类推:< / p>

edit 23299aa6 improve unit test messages
pick cf1c3c0c move sagas tests to where they'll get run
pick f7ff7fa7 use docker-compose up to rebuild the ui image more quickly
pick 544902ec split definitions file into helpers and given, when, then files
drop 55e94e7b remove canary test
pick 8a4b0862 move expectTitleToBe function to helper file

# Rebase 72b74f26..8a4b0862 onto 72b74f26 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

针对您的情况,以自己而不是其他分支为基础,您可以回顾git log来查找要还原到的提交的提交哈希。通常是在您开始的工作之前 进行一次提交:git rebase -i <where to "revert">