『現學現忘』Git基礎 — 22、Git中檔案重新命名

繁華似錦Fighting 發表於 2022-05-07
Git

我們這篇文章來說說在Git中如何進行檔案重新命名。

提示一下,下面所說明的是對已經被Git管理的檔案進行重新命名,未被Git追蹤的檔案,直接修改檔名稱就可以了。

1、用學過的命令進行檔案重新命名

(1)準備一個需要修改檔名的檔案。

# 1.準備一個乾淨的Git倉庫
[email protected] 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.建立一個檔案
[email protected] MINGW64 /j/git-repository/learngit (master)
$ echo "hello git" > test.txt

# 3.新增檔案到暫存區
[email protected] 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.提交檔案到本地版本庫
[email protected] 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.檢視工作目錄中的檔案狀態
[email protected] MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

# 2.手動修改檔名
[email protected] MINGW64 /j/git-repository/learngit (master)
$ mv test.txt readme.txt

# 3.再次檢視工作目錄中的檔案狀態
[email protected] 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.提交工作區的中的修改
[email protected] 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

# 檢視工作目錄中的檔案狀態
[email protected] 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)最後進行提交操作,就完成了檔案的重新命名。

[email protected] 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%)

[email protected] 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.重新命名檔案
# 前邊是要重新命名的檔案,後邊是要變更成的檔名。
[email protected] MINGW64 /j/git-repository/learngit (master)
$ git mv readme.txt test.txt

# 2.檢視工作目錄中的檔案狀態
[email protected] 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.提交到本地版本庫
[email protected] 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的工作方式。