首頁  >  文章  >  web前端  >  放棄 Git Checkout:改用 Git Switch 和 Git Restore

放棄 Git Checkout:改用 Git Switch 和 Git Restore

Barbara Streisand
Barbara Streisand原創
2024-10-14 13:28:05798瀏覽

如果你在日常工作中使用Git,git checkout是一個常用的指令。它經常用於切換分支,如果你查看文檔,你可以看到短語“切換分支或恢復工作樹文件”。但 UNIX 應該是做一件事並且把它做好。這很令人困惑,因此 Git 2.23 帶來了一對命令來取代它。


在介紹其工作原理之前,我們需要先簡單了解一下其相關的 Git 概念。

  • 工作副本(工作樹檔案):指儲存庫中出現在硬碟上的檔案。

  • 索引(暫存區或快取):它指的是你有 git add-ed,或者,如果你執行 git commit 會提交什麼。

  • HEAD:指的是「目前」或「活動」分支,當我們需要檢出一個分支時(指的是你嘗試將該分支與工作副本中的內容進行匹配),只有一個可以一次檢查一次。

git checkout 可以簽出一個分支或建立一個新分支並簽入其中:

# Switched to branch 'test'
$ git checkout test

# Switched to a new branch 'test'
$ git checkout -b test

# Switch back to the previous branch
$ git checkout -

# Switched to a commit
$ git checkout master~1

而git switch是用來接管分支相關的,所以它也可以做:

# Switched to branch 'test'
$ git switch test

# Switched to a new branch 'test'
$ git switch -c test

# Switch back to the previous branch
$ git switch -

# Switched to a commit
$ git switch -d master~1

正如我們一開始所說,git checkout 還可以還原工作樹檔案。這部分功能由 git Restore 接手。

以前,我們可以使用 git checkout -- main.c 從索引中恢復工作樹文件,語法是 git checkout [treeish] -- ,其中可以指定treeish為來源。簡單的例子:

# Restoring the working tree from the index
$ git checkout -- ./main.c

# Restoring index content from HEAD
$ git reset -- ./main.c

# Restoring the working tree and index from HEAD
$ git checkout HEAD -- ./main.c

注意,從 HEAD 還原索引內容時,我們只能使用 git reset,而 git checkout 沒有對應的選項。

用圖表顯示:

Ditch Git Checkout: Use Git Switch and Git Restore Instead

git Restore 可以更輕鬆地確定要復原哪些檔案以及將還原到何處。以下選項指定恢復位置:

-W
--worktree
-S
--staged

預設情況下, -W 和 --worktree 將從索引還原工作樹,就像沒有指定選項一樣(如 git Restore -- ./main.c )。而 -S 和 --staged 將從 HEAD 恢復索引內容。當兩者都通過時,索引和工作樹將從 HEAD 恢復。

例如:

# Restoring the working tree from the index
$ git restore -- ./main.c
# Equivalent to
$ git restore --worktree -- ./main.c

# Restoring index content from HEAD
$ git restore --staged -- ./main.c

# Restoring the working tree and index from HEAD
$ git restore --staged --worktree ./main.c

用圖表顯示:

Ditch Git Checkout: Use Git Switch and Git Restore Instead

上述是預設的,如果我們想從不同的提交恢復,我們可以使用--source選項。例如:

# Restore `./main.c` in the working tree with the last commit
$ git restore -s HEAD^ -- ./main.c
# Equivalent to
$ git restore --source=HEAD^ -- ./main.c

另一個有用的 git 復原案例可能是復原錯誤處理的檔案。例如:

# Incorrectly deleted files
$ rm -f ./main.c

# Quickly restore main.c from index
$ git restore ./main.c

批次恢復:

# Restore all C source files to match the version in the index
$ git restore '*.c'

# Restore all files in the current directory
$ git restore .

# Restore all working tree files with top pathspec magic
$ git restore :/

結論

git checkout 的功能區分得很清楚:git switch 用於切換分支,而 git Restore 用於還原工作樹檔案。它們提供了更明確的語義,符合 UNIX 哲學。

這兩個指令都是在 2019 年提出的,到目前為止,它們還處於實驗階段。可能會有變化,但通常不會太大,所以你現在就可以使用它們,它們更容易理解並且更容易混淆。

如果您發現這有幫助,請考慮 訂閱我的電子報 以獲取更多有關 Web 開發的有用文章和工具。感謝您的閱讀!

以上是放棄 Git Checkout:改用 Git Switch 和 Git Restore的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn