『现学现忘』Git基础 — 22、Git中文件重命名

我们这篇文章来说说在Git中如何进行文件重命名。

提示一下,下面所说明的是对已经被Git管理的文件进行重命名,未被Git追踪的文件,直接修改文件名称就可以了。

1、用学过的命令进行文件重命名

(1)准备一个需要修改文件名的文件。

# 1.准备一个干净的Git仓库
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

# 2.创建一个文件
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "hello git" > test.txt

# 3.添加文件到暂存区
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

# 4.提交文件到本地版本库
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git commit -m 'add test.txt file'
[master (root-commit) 995fbbe] add test.txt file
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

(2)直接手动修改文件的名字。

# 1.查看工作目录中的文件状态
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

# 2.手动修改文件名
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ mv test.txt readme.txt

# 3.再次查看工作目录中的文件状态
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (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.txt

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

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

说明:上面显示test.txt文件被删除,新增未被追踪的文件readme.txt

换句话说,上面信息的意思是,Git仓库中删除了一个文件,又新增了一个文件。

(3)把工作区的修改提交到暂存区。

# 1.提交工作区的中的修改
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add ./
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory

# 查看工作目录中的文件状态
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    test.txt -> readme.txt

我们可以看到Git还是很智能的,他通过你的操作,分析出你是在进行一个文件重命名的操作,所以在暂存区展示出来的状态是:renamed: test.txt -> readme.txt

说明:

上面执行git add ./命令,相当于执行了如下两条命令:

  • git rm test.txt :把test.txt文件从工作区删除。
  • git add readme.txt:把readme.txt文件添加到暂存区。

(不信你试试,哈哈)

(4)最后进行提交操作,就完成了文件的重命名。

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git commit -m 'rename test.txt -> readme.txt'
[master fe20ce4] rename test.txt -> readme.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename test.txt => readme.txt (100%)

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

2、使用git mv命令进行文件重命名

我们再把readme.txt文件重命名为test.txt

这次直接使用git mv命令实现,一条命令就可以完成上面繁琐的文件重命名步骤,日常工作中就用该命令重命名文件。

# 1.重命名文件
# 前边是要重命名的文件,后边是要变更成的文件名。
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git mv readme.txt test.txt

# 2.查看工作目录中的文件状态
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    readme.txt -> test.txt

# 3.提交到本地版本库
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git commit -m 'rename readme.txt -> test.txt'
[master 92cf82d] rename readme.txt -> test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename readme.txt => test.txt (100%)

可以看到非常简单就把文件进行了重命名。

其实运行git mv readme.txt test.txt命令就相当于运行了下面三条命令:

  • mv readme.txt test.txt
  • git rm readme.txt
  • git add test.txt

说明:文件重命名其实是一个很简单的操作,为什么花费一整篇的内容来讲解,主要是让大家慢慢习惯Git的工作方式。

posted @ 2022-05-07 11:05  繁华似锦Fighting  阅读(1659)  评论(0编辑  收藏  举报