如何在Git中还原新创建的文件

时间:2014-03-07 00:53:53

标签: git

我在上一次提交中添加了一个新文件 uploader.rb 。我的意思是这个文件在之前的任何提交中都没有。但我不想提交这个文件。在使用此文件的此提交中,已提交了两个旧文件。我试过 git reset uploader.rb。但这不起作用。有什么办法可以让我只重置这个文件吗?提前谢谢。

3 个答案:

答案 0 :(得分:0)

首先,您需要将当前分支重置为"撤消"最后一次提交,它将使uploader.rb和其他两个文件处于最新提交之前的状态:

git reset HEAD^

然后重新发送您想要在原始提交中保留的两个文件:

git add otherfile1 otherfile2
git commit

答案 1 :(得分:0)

好吧,假设你提交了goodbad,第二个不应该被提交。首先假设这是最后一次提交。然后你可以:

git reset HEAD^     # Go back one commit, undo
git add good        # Set up to commit just the good stuff
git commit          # Redo the botched commit

如果这是一些提交,您可以重写历史记录来修复它。但要小心!

git tag OldPlace        # Mark for case something explodes

gitk --all              # Show history, find the commit *before* the mess
git rebase -i SHA-FROM_ABOVE
                        # Mark the relevant commit for editing, exit the editor
                        # When you get the prompt back...
git add good
git commit              # redo...
git rebase --continue   # Rip through the other changes

git tag -d OldPlace     # Get rid of the mark, if everything is OK

答案 2 :(得分:0)

假设您没有推动修改,我认为您可以尝试删除文件

git rm uploader.rb
git commit -m "deleted uploader.rb"

然后执行交互式rebase来清理提交历史记录

git rebase -i HEAD~2

它会显示这样的内容,最后两次提交:

pick f7f3f6d deleted uploader.rb
pick 310154e message of the previous commit

用'squash'替换'pick'然后保存

squash f7f3f6d deleted uploader.rb
pick 310154e message of the previous commit

它会合并你的两个提交。删除操作将消失历史记录。

否则,如果您已经推送了修改,则修改yu提交历史记录。您必须删除文件并提交修改。