简体   繁体   English

撤消最近在git bash上触发的“ git add”命令

[英]Undo recent “git add .” command fired on git bash

I have mistakenly did git add . 我错误地做了git add . Now I have multiple files in staging area. 现在,我在暂存区中有多个文件。 I know I can use git reset to unstage files but this would take most of my time. 我知道我可以使用git reset撤消文件,但这将花费我的大部分时间。

I am looking for a solution that would save my time. 我正在寻找可以节省时间的解决方案。 Now I want to know that 现在我想知道

  • Is there a possible way to undo last git add . 有没有一种方法可以撤消最后的git add . command fired on git. 命令在git上触发。 So that I don't have to do git reset file multiple times. 这样我就不必多次执行git reset file

  • If the above way is not possible then is there a way I could do reset multiple files using a single command. 如果上述方法不可行,那么有一种方法可以使用单个命令重置多个文件。

Did you have staged files you want to keep staged, or are you willing to have all files go back to completely unstaged? 你已经上演你想保留筹备的文件,或者你愿意拥有的所有文件回到完全不分级的?

The latter is very easy: use git reset with no additional arguments, as galath commented . 后者非常简单:如galath所评论的 ,使用git reset带任何其他参数。 This does a --mixed reset: 这会执行--mixed重置:

  • move the current branch to the named commit, but with no named commit, that means move the current branch to HEAD , and HEAD is the current commit, so we move the current commit to itself, which does nothing; 将当前分支移至指定的提交,但不进行命名提交,这意味着将当前分支移至HEAD ,而HEAD是当前提交,因此我们将当前提交移至其自身,不执行任何操作; then 然后
  • replace files in the index with their versions in the named commit (which, again, we didn't name, so that's the HEAD commit). 用命名提交中的版本替换索引中的文件(同样,我们没有命名,所以这是HEAD提交)。

(These two steps are the --soft and --mixed parts; without --hard , git reset skips the third step of wiping out work-tree changes. Obviously you don't want those wiped out, so you should not use --hard .) (这两个步骤是--soft--mixed部分;如果没有--hard ,则git reset 跳过擦除工作树更改的第三步。显然,您不希望删除这些内容,因此不应使用--hard 。)

The former is harder. 前者更难。 To get a list of all staged files, use: 要获取所有暂存文件的列表,请使用:

git diff --cached --name-only

If you want to selectively re-set most, but not all, of these files, you can write those file names to a file (preferably outside your work-tree) such as /tmp/list , then edit the file to remove the names you want to keep staged. 如果要有选择地重设大多数(但不是全部)文件,则可以将这些文件名写入文件(最好是在工作树之外),例如/tmp/list ,然后编辑该文件以删除名称您想继续上演。 Then: 然后:

git reset -- $(cat /tmp/list)

or: 要么:

xargs git reset -- < /tmp/list

to run git reset -- on each path name. 在每个路径名上运行git reset -- This assumes there is no white-space in the file names; 假设文件名中没有空格; if there is, use additional shell trickery to break only at newlines (assuming no newlines in the path names). 如果存在,请使用其他shell技巧来仅在换行符处断开(假设路径名中没有换行符)。

If you had carefully staged some but not all of some files using git add -p , it is usually possible to recover these using git fsck --lost-found and poking through all the resulting "dangling blobs", but that's rarely worth the effort. 如果你一直在小心翼翼地上演一些但不是所有使用某些文件的git add -p ,通常可以收回这些使用git fsck --lost-found ,并通过所有产生的“晃来晃去斑点”戳,但是这是很不值得的努力。

看来这应该工作: git reset -p <file_name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM