你怎么能撤消最后一次 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:26:57  来源:igfitidea点击:

How can you undo the last git add?

git

提问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

You cannot undo the latestgit add, but you can undo all adds since the last commit. git resetwithout a commit argument resets the index (unstages staged changes):

您无法撤消最新的git add,但可以撤消add自上次提交以来的所有s。git reset没有提交参数会重置索引(取消暂存阶段的更改):

git reset

回答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 -pwill not help, since its smallest granularity is lines. gitdoesn't know that about the intermediate state of:

在这种情况下,git reset -p将无济于事,因为它的最小粒度是行。git不知道关于以下中间状态的信息:

void foo(int bar) {
    print("$bar");
}

main() {
    foo(1337);
}

any more.

没有了。

回答by davids

You could use git reset(see docs)

你可以使用git reset(见文档

回答by theGiallo

At date git prompts:

在日期 git 提示:

  1. use "git rm --cached <file>..." to unstageif files were not in the repo. It unstages the files keeping them there.
  2. use "git reset HEAD <file>..." to unstageif the files were in the repo, and you are adding them as modified. It keeps the files as they are, and unstages them.
  1. use "git rm --cached <file>..." to unstage如果文件不在回购中。它取消暂存将它们保留在那里的文件。
  2. 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 HEADwon'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).

至少您将拥有当前和以前状态的永久记录(直到您删除该临时分支)。