7

以以下场景为例:

  • 你不小心跟踪并提交了一个配置文件
  • 每个开发环境都应该有自己特定的配置文件,所以你应该.gitignoregit add
  • 你很久没有意识到

提交

A - B - C - D - E
    |   |   |   |
    |    \  |  /
    | commits that accidentally track application config
    |
    commit to untrack & .gitignore config
    [finally you did the right thing... but too late?]

如果您曾经重置或挑选回 C、D 或 E,它将覆盖配置文件。

无论如何通过对它们应用 B 提交来重写 C - E 吗?

4

2 回答 2

3

如果配置文件应该被取消跟踪,最好通过 E 取消跟踪配置文件,将其添加到 gitignore 等进行提交。如果配置包含敏感信息,在这种情况下,必须从任何 repo 提交中删除它,你没有其他去,但修改历史(来自 GitHub 的这个帮助页面讨论了如何做到这一点 - http://help.github.com/remove-sensitive-data/

由于 Git 的本质和要求,您不能对提交进行更改并使其看起来是相同的提交。

于 2012-04-23T09:28:12.453 回答
0

如果作者都是你,并且你不需要保留受影响提交的日期(所有这些,AE),那几乎是微不足道的,另一种方式将需要更多

根据您的图片,我将在错误之前添加修订 F 作为提交。假设你在分支'master'上,

git log --oneline master~6..master

应该向您展示这些修订。

git branch corrected master~5   # this is F, master~0 is A i.e. the branch tip

git config advice.detachedhead false  # just to get it to stop blabbing at you

# make corrected commit E
git checkout master~4
git rm --cached yourconfigfile
echo ref: refs/heads/corrected >.git/HEAD
git cat-file -p master~4 | sed 1,/^$/d | git commit -m-

# make corrected commit D
git checkout master~3
git rm --cached yourconfigfile
echo ref: refs/heads/corrected >.git/HEAD
git cat-file -p master~3 | sed 1,/^$/d | git commit -m-

# ... repeat for C, B, and A

在末尾,

echo ref: refs/heads/master > .git/config

你就完成了。保留作者/日期信息是从标题中设置 GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL,DATE} 向git cat-file -p您显示的问题,如果您需要它,我会为您准备一个 sed。

于 2012-04-23T22:03:36.260 回答