无法更改旧提交

时间:2014-05-27 17:21:40

标签: git git-commit

我正在尝试更改39次提交的提交(在此提交之前有39次提交)。 我只需要在一个文件中更改几行。

这是我做过的事,谢谢to this question:

git rebase --interactive e0ea83ac7c68df747705e3f39864cd4931416ae4^

我写了edit而不是选择此提交旁边的行,输入' Control + X'在nano上打字" Y"然后输入,我得到了:

⭑ shideneyu project/nuddz/public(master) ✔»git rebase --interactive e0ea83ac7c68df747705e3f39864cd4931416ae4^                                               [19:02:01]
Stopped at e0ea83a... Migrated every Reponse + ProvisionalReponse data to Answer
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

问题在于:由于文件版本已经过时,我无法进行必要的更改。我得到的文件并不是我提交该文件时所拥有的文件。实际上,我眼前的那个文件与最近的提交相同!

也许这里有一个提示,当我做git log时,这就是我得到的:

⭑ shideneyu ~/project/nuddz(02b5f2c) ✔»git log 
commit 02b5f2c9c4987d433329b172c9cda28bf74d9db8
Author: sidney <shiden**@gmail.com>
Date:   Mon May 12 15:20:01 2014 +0200

    Migrated every Reponse + ProvisionalReponse data to Answer

commit 75468d2a2dba8d06540781b6a13783610130eed8
Author: Alexis <*****@yahoo.fr>
Date:   Mon May 26 10:44:40 2014 +0200

    Moved debugger gem in developpment

commit 2ff563c63ce395f72b1f9acb4a0de7c0d64fb204
Author: Alexis <*****@yahoo.fr>
Date:   Fri May 23 19:30:31 2014 +0200

    Assets precompile before Heroku push

[...]

我故意截断(记录太多)。我不认为这是正常的。年表(12可能是最近的?如果我想更改十几天前存在的提交,如果最近的更改存在,那么5月26日如何存在呢?)很奇怪。

我用rebase --abort中止了rebase,然后查看了不同提交的不同之处:我是对的,我不是疯了,这是事实,rebase没有将本地文件更改为他们应该在39个版本之前提交。

我怎样才能做出改变呢?我该怎么办?

编辑:我上传此屏幕截图,通过gitk --all查看日志时显示差异,并在执行cat Gemfile时使用git rebase --interactive e0ea83ac7c68df747705e3f39864cd4931416ae4^可视化特定文件

enter image description here

1 个答案:

答案 0 :(得分:1)

如果我想更改旧提交的内容,我会将其删除并将其合并回分支的顶部。

git checkout <your_branch>
git revert <old_commit>
git merge <old_commit>

进行更新......然后

git commit --amend -m "Re-applying <old_commit> with the addition of <blablabla>"

我认为很高兴能够看到这已经在分支机构的历史中完成,所以以后不会有混淆。

如果你想保持清洁,那么使用一个临时分支进行清理工作,并将它集成到你的分支上一个新版本。

git checkout -b <temp_branch> <your_branch>
git revert <old_commit>
git merge <old_commit>

进行更新......然后

git commit --amend -m "..."
git checkout <your_branch>
git merge --squash <temp_branch>
git commit --amend -m "Explain what was done"