如何在我的工作副本中排除非指数的变更?


当前回答

我最喜欢的是

git checkout -p

这使你能够选择性地逆转碎片。

也看:

git add -p

其他回答

cd path_to_project_folder  # take you to your project folder/working directory 
git checkout .             # removes all unstaged changes in working directory

对于当前工作目录使用中的所有未输入的文件:

git restore .

对于特定文件的使用:

git restore path/to/file/to/revert

这与 git 交换器一起取代了过度加载的 git 支票(见这里),从而取消了论点的分歧。

如果一个文件既有步骤和未步骤的变化,则仅在 git diff 中显示的未步骤的变化将被转换。

之前 Git 2.23

对于当前工作目录中的所有未输入的文件:

git checkout -- .

对于一个特定的文件:

git checkout -- path/to/file/to/revert

在这里消除双重性(这被称为论点不双重性)。

没有一个解决方案工作,如果你只是改变了文件的许可(这是DOS/Windoze)

Mon 23/11/2015-15:16:34.80 C:\...\work\checkout\slf4j+> git status
On branch SLF4J_1.5.3
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   .gitignore
        modified:   LICENSE.txt
        modified:   TODO.txt
        modified:   codeStyle.xml
        modified:   pom.xml
        modified:   version.pl

no changes added to commit (use "git add" and/or "git commit -a")

Mon 23/11/2015-15:16:37.87 C:\...\work\checkout\slf4j+> git diff
diff --git a/.gitignore b/.gitignore
old mode 100644
new mode 100755
diff --git a/LICENSE.txt b/LICENSE.txt
old mode 100644
new mode 100755
diff --git a/TODO.txt b/TODO.txt
old mode 100644
new mode 100755
diff --git a/codeStyle.xml b/codeStyle.xml
old mode 100644
new mode 100755
diff --git a/pom.xml b/pom.xml
old mode 100644
new mode 100755
diff --git a/version.pl b/version.pl
old mode 100644
new mode 100755

Mon 23/11/2015-15:16:45.22 C:\...\work\checkout\slf4j+> git reset --hard HEAD
HEAD is now at 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore

Mon 23/11/2015-15:16:47.42 C:\...\work\checkout\slf4j+> git clean -f

Mon 23/11/2015-15:16:53.49 C:\...\work\checkout\slf4j+> git stash save -u
Saved working directory and index state WIP on SLF4J_1.5.3: 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore
HEAD is now at 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore

Mon 23/11/2015-15:17:00.40 C:\...\work\checkout\slf4j+> git stash drop
Dropped refs/stash@{0} (cb4966e9b1e9c9d8daa79ab94edc0c1442a294dd)

Mon 23/11/2015-15:17:06.75 C:\...\work\checkout\slf4j+> git stash drop
Dropped refs/stash@{0} (e6c49c470f433ce344e305c5b778e810625d0529)

Mon 23/11/2015-15:17:08.90 C:\...\work\checkout\slf4j+> git stash drop
No stash found.

Mon 23/11/2015-15:17:15.21 C:\...\work\checkout\slf4j+> git checkout -- .

Mon 23/11/2015-15:22:00.68 C:\...\work\checkout\slf4j+> git checkout -f -- .

Mon 23/11/2015-15:22:04.53 C:\...\work\checkout\slf4j+> git status
On branch SLF4J_1.5.3
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   .gitignore
        modified:   LICENSE.txt
        modified:   TODO.txt
        modified:   codeStyle.xml
        modified:   pom.xml
        modified:   version.pl

no changes added to commit (use "git add" and/or "git commit -a")

Mon 23/11/2015-15:22:13.06 C:\...\work\checkout\slf4j+> git diff
diff --git a/.gitignore b/.gitignore
old mode 100644
new mode 100755
diff --git a/LICENSE.txt b/LICENSE.txt
old mode 100644
new mode 100755
diff --git a/TODO.txt b/TODO.txt
old mode 100644
new mode 100755
diff --git a/codeStyle.xml b/codeStyle.xml
old mode 100644
new mode 100755
diff --git a/pom.xml b/pom.xml
old mode 100644
new mode 100755
diff --git a/version.pl b/version.pl
old mode 100644
new mode 100755

修复这一点的唯一方法是手动重新设置已更改的文件中的权限:

Mon 23/11/2015-15:25:43.79 C:\...\work\checkout\slf4j+> git status -s | egrep "^ M" | cut -c4- | for /f "usebackq tokens=* delims=" %A in (`more`) do chmod 644 %~A

Mon 23/11/2015-15:25:55.37 C:\...\work\checkout\slf4j+> git status
On branch SLF4J_1.5.3
nothing to commit, working directory clean

Mon 23/11/2015-15:25:59.28 C:\...\work\checkout\slf4j+>

Mon 23/11/2015-15:26:31.12 C:\...\work\checkout\slf4j+> git diff

您可以创建自己的标签,描述如何以描述的方式进行。

我用下一个字符来排除变化。


排除工作树中的一个(列表)文件(s)中的变化

discard = checkout --

然后,你可以使用它作为附加,以排除所有变更:

discard .

或者只是一个文件:

discard filename

否则,如果你想删除所有的更改,以及未追踪的文件,我使用一个混合的检查和清洁:

清理和排除工作树中的变更和未追踪文件

cleanout = !git clean -df && git checkout -- .

所以使用是简单的如下:

cleanout

现在在下一个GitHub repo中可用,其中包含许多联盟:

HTTPS://github.com/GitAlias/吉塔利亚

你可以使用 git stash - 如果有什么不对劲,你仍然可以从 stash 返回。 类似于这里的一些其他答案,但这个也删除所有未输入的文件,以及所有未输入的删除:

git add .
git stash

如果你检查一切都没事,请放下:

git stash drop

比拉尔·马克索德(Bilal Maqsood)与吉特清洁的答案也为我工作了,但我有更多的控制权 - 如果我偶然做STH,我仍然可以恢复我的变化

更新

我认为还有1个变化(我不知道为什么这对我以前工作):

git add. - A 而不是 git add.

没有 -A 删除的文件将不会复制