# Git基础(四)

本节将给出几个重构项目时常用的`Git`命令。

## 一、删除不再追踪的内容

当我们需要在项目中删除某个文件，你可以手动删除并将修改再添加到暂存区。 看一个例子，首先在项目中创建一个文件并提交。

```
➜  git_test git:(master) touch test_rm.md
➜  git_test git:(master) ✗ git add test_rm.md
➜  git_test git:(master) ✗ git commit -m "first commit"
[master (root-commit) cab6745] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test_rm.md
➜  git_test git:(master) git status
On branch master
nothing to commit, working tree clean
➜  git_test git:(master) ls
test_rm.md
```

然后我们使用`rm`命令删除，再看看当前项目的状态：

```
➜  git_test git:(master) rm test_rm.md
➜  git_test git:(master) ✗ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    test_rm.md

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

可以看到，当`rm`命令执行后，修改发生在工作区，此时你还可以使用`git restore <file>`来将工作区的修改恢复。接下来我们使用`git rm <file>`，然后再来看看效果。

```
➜  git_test git:(master) git rm test_rm.md
rm 'test_rm.md'
➜  git_test git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    test_rm.md
```

可以看到，让我们使用`git rm`之后，修改被直接添加到了暂存区。实际上，`git rm`就相当于：

```
rm <file>
git add <file>
```

## 二、挪动文件的位置或者为文件重命名

当需要挪动文件位置或者重命名某个文件时，你可以使用`mv`命令手动移动后再将修改添加到暂存区。

来看一个例子，我们将`test_mv.md`挪到`test`文件夹下。

```
➜  git_test git:(master) ✗ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    test_mv.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    test/

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

修改被添加到了工作区，还需要手动将修改添加到暂存区，这和前面的`rm`类似。接下来使用`git mv`命令，再看看效果，先把之前的修改恢复吧。

```
➜  git_test git:(master) ✗ git restore test_mv.md
➜  git_test git:(master) ✗ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    test/

nothing added to commit but untracked files present (use "git add" to track)
```

可以看到，尽管`test_mv.md`已经被恢复到了原目录下，但是这时我们仍在存在`Untracked`的`test`文件夹，看看里面的内容。

```
➜  git_test git:(master) ✗ ls test
test_mv.md
```

被`mv`进来的文件还在！看来`git`并没有为我们执行类似于`un-mv`的操作，这很好理解，因为前面执行`mv`之后，使用`git status`查看的结果是显示`test_mv.md`文件被删除了。**原因是`test_mv.md`被重命名为`test/test_mv.md`，git直追踪了`test_mv.md`而并没有追踪`test/test_mv.md`，所以git经过推断，得出该文件已经被删除的结论。**

如果使用`git mv`，效果是什么呢？

```
➜  git_test git:(master) git mv test_mv.md test_mv_new.md
➜  git_test git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    renamed:    test_mv.md -> test_mv_new.md
```

可以看到，git直接将修改添加到了暂存区，而且推断出这是一次重命名。实际上，该命令就相当于:

```
mv test_mv.md test_mv_new.md
git add -A
```

## 三、总结

当需要重命名或者删除一些文件时，使用`git rm`和`git mv`可以减少我们的操作次数。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rodneycheung.gitbook.io/handbook/2.-huan-jing-pian/gong-ju-bu-shu-he-shi-yong/ban-ben-kong-zhi/git/1.-ji-chu/4_git_fundamental.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
