是否有可能取消git中最后一个暂存(未提交)的更改?假设当前分支中有很多文件,有些是阶段性的,有些不是。在某个时刻,某个愚蠢的程序员不小心执行了:
git add -- .
...而不是:
git checkout -- .
这个程序员现在可以用一些神奇的git命令取消他最后的更改吗?或者他应该在一开始尝试之前就做出承诺?
是否有可能取消git中最后一个暂存(未提交)的更改?假设当前分支中有很多文件,有些是阶段性的,有些不是。在某个时刻,某个愚蠢的程序员不小心执行了:
git add -- .
...而不是:
git checkout -- .
这个程序员现在可以用一些神奇的git命令取消他最后的更改吗?或者他应该在一开始尝试之前就做出承诺?
当前回答
Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy: git reset head <file> Reset the file to the last state from HEAD, undoing changes and removing them from the index: git reset HEAD <file> git checkout <file> # If you have a `<branch>` named like `<file>`, use: git checkout -- <file> This is needed since git reset --hard HEAD won't work with single files. Remove <file> from index and versioning, keeping the un-versioned file with changes in working copy: git rm --cached <file> Remove <file> from working copy and versioning completely: git rm <file>
其他回答
你不能撤销最新的git添加,但是你可以撤销自上次提交以来的所有添加。Git reset不带commit参数会重置索引(取消阶段性变化):
git reset
Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy: git reset head <file> Reset the file to the last state from HEAD, undoing changes and removing them from the index: git reset HEAD <file> git checkout <file> # If you have a `<branch>` named like `<file>`, use: git checkout -- <file> This is needed since git reset --hard HEAD won't work with single files. Remove <file> from index and versioning, keeping the un-versioned file with changes in working copy: git rm --cached <file> Remove <file> from working copy and versioning completely: git rm <file>
根据难度的大小和规模,您可以创建一个临时分支,并将当前的工作提交到那里。
然后切换到原始分支并签出,并从从头提交中选择适当的文件。
至少您将拥有当前和以前的状态的永久记录(直到您删除那个scratch分支为止)。
如果你想从git中删除所有添加的文件
git reset
如果要删除单个文件
git rm <file>
在date git提示:
如果文件不在repo中,使用"git rm——cached <file>…"它会取消文件,把它们保存在那里。 使用“git reset HEAD <file>…”如果文件在repo中,则取消阶段,并按修改后的方式添加它们。它保持文件原样,并取消它们的阶段。
据我所知,您无法撤消git add—但您可以取消上面提到的文件列表。