解决方法git commit

时间:2013-02-02 19:40:53

标签: git merge commit ignore

假设我有3个git提交:

  1. 在侧栏上提交更改
  2. 提交页脚更改
  3. 提交标题上的更改
  4. 现在让我们假设我已经通过精神觉醒,并意识到我只需要改变标题和侧边栏,页脚很好,不需要更改。

    是否有执行以下操作的命令?

    • 合并提交#3减去提交#2
    • 删除提交#2
    • 合并提交#3加上提交#1

    (或任何其他方法,使得#2从未发生过。)

3 个答案:

答案 0 :(得分:2)

交互式rebase允许您修改,重新排列,合并提交,更改消息等:

git rebase --interactive

引用帮助:

# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

失去提交是你想要的。

答案 1 :(得分:1)

您始终可以使用git-revert从单个提交中恢复更改。

  

给定一个或多个现有提交,还原相关修补程序引入的更改,并记录一些记录它们的新提交。这要求您的工作树是干净的(没有HEAD提交的修改)。

     

注意:git revert用于记录一些新的提交以反转某些早期提交的效果(通常只有一个错误的提交)。如果要丢弃工作目录中所有未提交的更改,您应该看到git-reset(1),尤其是--hard选项。如果要像在另一次提交中那样提取特定文件,您应该看到git-checkout(1),特别是git checkout <commit> -- <filename> syntax。请注意这些替代方案,因为它们都会丢弃工作目录中未提交的更改。

所以:

git revert <commit-id-of-footer-changes>

答案 2 :(得分:1)

您可以使用cherry-pick合并您想要的提交,然后revert开发分支在页脚更改之前提交。

git checkout branch_to_merge_into
git cherry-pick commit#1 commit#3