你怎么能撤消最后一次 git add ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12132272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you undo the last git add?
提问by Septagram
Is it possible to unstage the last staged (not committed) change in git? Suppose there were a lot of files in the current branch, some staged, some not. At some point, some foolish programmer accidentally executed:
是否可以取消git 中最后一次暂存(未提交)的更改?假设当前分支中有很多文件,有些已暂存,有些没有。在某个时候,一些愚蠢的程序员不小心执行了:
git add -- .
...instead of:
...代替:
git checkout -- .
Can this programmer now unstage his last changes with some magical gitcommand? Or should he have committed before experimenting in the first place?
这个程序员现在可以用一些神奇的git命令取消他最后的更改吗?或者他应该在首先进行实验之前做出承诺?
回答by thameera
You can use git reset
. This will 'unstage' all the files you've added after your last commit.
您可以使用git reset
. 这将“取消暂存”您在上次提交后添加的所有文件。
If you want to unstage only some files, use git reset -- <file 1> <file 2> <file n>
.
如果您只想取消暂存某些文件,请使用git reset -- <file 1> <file 2> <file n>
.
Also it's possible to unstage some of the changes in files by using git reset -p
.
还可以使用 .unstage 取消文件中的某些更改git reset -p
。
回答by knittl
回答by stanm
So the real answer to
所以真正的答案是
Can this programmer now unstage his last changes with some magical git command?
这个程序员现在可以用一些神奇的 git 命令取消他最后的更改吗?
is actually: No, you cannot unstage just the last git add
.
实际上是:不,您不能只取消最后一个git add
.
That is if we interpret the question as in the following situation:
也就是说,如果我们将问题解释为以下情况:
Initial file:
初始文件:
void foo() {
}
main() {
foo();
}
First change followed by git add
:
第一个变化,然后是git add
:
void foo(int bar) {
print("$bar");
}
main() {
foo(1337);
}
Second change followed by git add
:
其次是git add
:
void foo(int bar, String baz) {
print("$bar $baz");
}
main() {
foo(1337, "h4x0r");
}
In this case, git reset -p
will not help, since its smallest granularity is lines. git
doesn't know that about the intermediate state of:
在这种情况下,git reset -p
将无济于事,因为它的最小粒度是行。git
不知道关于以下中间状态的信息:
void foo(int bar) {
print("$bar");
}
main() {
foo(1337);
}
any more.
没有了。
回答by theGiallo
At date git prompts:
在日期 git 提示:
use "git rm --cached <file>..." to unstage
if files were not in the repo. It unstages the files keeping them there.use "git reset HEAD <file>..." to unstage
if the files were in the repo, and you are adding them as modified. It keeps the files as they are, and unstages them.
use "git rm --cached <file>..." to unstage
如果文件不在回购中。它取消暂存将它们保留在那里的文件。use "git reset HEAD <file>..." to unstage
如果文件在 repo 中,并且您将它们添加为已修改。它保持文件原样,并取消暂存它们。
At my knowledge you cannot undo the git add --
but you can unstage a list of files as mentioned above.
据我所知,您无法撤消,git add --
但可以取消暂存上述文件列表。
回答by Faisal
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 reset head <file>
从 HEAD 将文件重置为最后一个状态,撤消更改并将其从索引中删除:
git reset HEAD <file> git checkout <file> # If you have a `<branch>` named like `<file>`, use: git checkout -- <file>
这是必需的,因为
git reset --hard HEAD
不适用于单个文件。<file>
从索引和版本控制中删除,在工作副本中保留未版本控制的文件:git rm --cached <file>
<file>
完全从工作副本和版本控制中删除:git rm <file>
回答by BSB
If you want to remove all added filesfrom git for commit
如果你想从 git 中删除所有添加的文件进行提交
git reset
If you want to remove an individual file
如果要删除单个文件
git rm <file>
回答by ireshika piyumalie
You can use
您可以使用
git reset
to undo the recently added local files
撤消最近添加的本地文件
git reset file_name
to undo the changes for a specific file
撤消对特定文件的更改
回答by Philip Oakley
Depending on size and scale of the difficultly, you could create a scratch (temporary) branch and commit the current work there.
根据难度的大小和规模,您可以创建一个临时(临时)分支并在那里提交当前工作。
Then switch to and checkout your original branch, and pick the appropriate files from the scratch commit.
然后切换到并签出您的原始分支,并从临时提交中选择适当的文件。
At least you would have a permanent record of the current and previous states to work from (until you delete that scratch branch).
至少您将拥有当前和以前状态的永久记录(直到您删除该临时分支)。