➜ 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")
➜ 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")
➜ 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)