1. Overview
1.概述
A Git working directory can contain different types of files including staged files, unstaged files, and untracked files.
一个Git工作目录可以包含不同类型的文件,包括阶段性文件、非阶段性文件和非跟踪文件。
In this tutorial, we’ll see how to discard changes in our working directory that are not in the index.
在本教程中,我们将看到如何丢弃工作目录中不在索引中的变化。
2. Analyzing the State of a Working Directory
2.分析一个工作目录的状态
For our example, let’s say that we’ve forked and cloned a Git Repository and then made some changes to our working directory.
对于我们的例子,假设我们已经分叉并克隆了一个Git 仓库,然后对我们的工作目录做了一些修改。
Let’s check the status of our working directory:
让我们检查一下我们的工作目录的状态。
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java
Untracked files:
(use "git add <file>..." to include in what will be committed)
gradle/maven-to-gradle/src/main/java/com/sample/javacode/TimeZones.java
no changes added to commit (use "git add" and/or "git commit -a")
Here, we can see some files that are modified and unstaged, along with a new file we added.
在这里,我们可以看到一些被修改过的和未缓存的文件,以及我们添加的一个新文件。
Now, let’s stage the existing Java file by using git add and check the state again:
现在,让我们通过使用git add 将现有的Java文件分阶段,并再次检查状态。
$ git add gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
gradle/maven-to-gradle/src/main/java/com/sample/javacode/TimeZones.java
Here, we can see that we have three categories of files in our working directory:
在这里,我们可以看到我们的工作目录中有三类文件。
- staged files – DisplayTime.java
- unstaged files – README.md
- untracked files – TimeZones.java
3. Removing the Untracked Files
3.删除未被追踪的文件
Untracked files are those which are new to the repository and haven’t been added to version control. We can remove these with the clean command:
未跟踪的文件是那些新加入版本库的文件,还没有被添加到版本控制中。我们可以用clean命令删除这些文件。
$ git clean -df
The -df option ensures that removal is forced and that untracked directories are also included for removal. Running this command will output which files were removed:
-df选项确保强制删除,并且未被追踪的目录也被包括在内进行删除。运行这个命令将输出哪些文件被删除。
Removing gradle/maven-to-gradle/src/main/java/com/sample/javacode/TimeZones.java
Now, let’s check the status again:
现在,让我们再次检查一下状态。
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
We can see how the clean command has removed the untracked file from our working directory.
我们可以看到clean命令已经从我们的工作目录中删除了未跟踪的文件。
4. Removing the Changes Not Staged for Commit
4.移除未分阶段提交的修改
Now that we’ve removed the untracked files, we’re left to deal with the staged and unstaged files inside our working directory. We can use the checkout command with the “–” option to remove all the changes that are not staged for commit:
现在我们已经删除了未跟踪的文件,剩下的就是处理工作目录中的已缓存和未缓存的文件。我们可以使用带有”-“选项的checkout命令来删除所有未被暂存提交的修改。
$ git checkout -- .
Let’s check the status again after running the command:
让我们在运行该命令后再次检查状态。
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java
We can see that our working directory now contains only the staged changes.
我们可以看到,我们的工作目录现在只包含了阶段性的变化。
5. Conclusion
5.总结
In this tutorial, we saw how our working directory can contain staged, unstaged, and untracked files among the files that aren’t currently under Git version control.
在本教程中,我们看到我们的工作目录如何在当前不在 Git 版本控制之下的文件中包含暂存、未暂存和未跟踪的文件。
We also saw how git clean -df and git checkout — . can remove all unstaged changes from our working directory.
我们还看到了git clean -df和git checkout – .如何从我们的工作目录中删除所有未缓存的修改。