如何在 Git 中暫存更改?


這個問題可以改述為“如何在 Git 中儲存工作進度 (WIP) 並方便時返回?”

問題 − 當我們切換分支時,Git 會將我們的工作目錄重置為包含目標分支上次提交中儲存的快照。例如,如果我們從feature分支切換到master分支,Git 將用master分支的最後一次提交的內容替換工作目錄中的內容。但是,如果我們工作目錄中有一些尚未提交的本地更改,這些更改將會丟失。在這種情況下,Git 將不允許我們切換分支。讓我們看看這是如何發生的以及我們應該如何解決這個問題。

示例 − 建立一個空倉庫並執行以下命令。此示例顯示我們正在feature分支中工作,編輯和新增檔案。突然,我們可能需要切換到 master 分支來立即修復master分支中的錯誤。由於我們在feature分支中有正在進行的工作 (WIP),它將不允許我們切換到master分支。

$ git init                  // initialize an empty repo
$ echo abc>abc.txt          // create a file and add some text to it
$ git add abc.txt           // stage the file
$ git commit −m abc.txt     // commit changes to the repo
$ git checkout feature      // Switch to the feature branch
$ git log −−oneline         // Display commit history
$ echo lmno>lmno.txt        // create a file and add some text
$ git add lmno.txt          // stage the file
$ git commit −m 'save lmno' // commit changes to the repo
$ echo lmno lmno>>lmno.txt  // append data to the file
$ git checkout master       // Switch to the master branch

當我們嘗試切換分支時,Git 會提示我們暫存更改。

delI@DESKTOP-N961NR5 MINGW64 —/Desktop/Internship/Batch_01/Session03/git/test-re
po (feature)
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout
Imno. txt
please commit your changes or stash them before you switch branches.
Aborting

將工作進度 (WIP) 推送到暫存區

我們不想提交更改,因為我們還沒有完成。在這種情況下,我們需要暫存我們的更改。暫存意味著將東西儲存在安全的地方。我們將其儲存在 Git 倉庫中的一個單獨的暫存區中。暫存不會成為我們歷史記錄的一部分。讓我們看看它是如何工作的。

以下命令將位於暫存區中並等待提交的工作推送到暫存區。−m標誌用於提供暫存訊息。

$ git stash push −m 'working on lmno file not completed '

預設情況下,未跟蹤的檔案不包含在您的暫存區中。要推送未跟蹤的檔案,即工作目錄中的檔案,我們需要包含選項−am標誌。讓我們來看一個例子 −

$ echo hello > newfile.txt //create a file in the working area
$ git stash push −am 'my new stash' //push untracked files into the stash

要檢視所有暫存區,請使用命令 − git stash list

$ git stash list

該命令返回所有暫存區。請注意,最新的暫存區將列在列表的頂部,即索引 0。

stash@{0}: On feature: my new stash
stash@{1}: On feature: working on lmno file not completed

暫存後,工作樹將是乾淨的。現在協作者可以切換到任何其他分支並進行一些重要工作。完成後,協作者可以再次切換回feature分支。此時,我們決定將其中一個暫存區應用到我們的工作目錄。

應用暫存區

要將暫存的更改應用回我們的工作目錄,我們可以使用以下命令 −

$ git stash apply <index_number>

以下示例應用索引 0 和索引 1 處的暫存序列

$ git stash apply 0
$ git stash apply 1

輸出顯示暫存區已應用,現在我們的工作目錄將按預期包含所有更改。

dell@DESKTOP−N96LNR5 MINGw64 /e/tut_repo (feature)
$ git stash apply 0
Al ready up to date!
On branch feature
untracked files:
(use ''git add <file>… " to include in what will be committed)

newfile. txt
nothing added to commit but untracked fi les present

dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git stash apply 1
On branch feature
Changes not staged for commit:
(use "git add <file>…" to update what will be committed)
(use "git restore <file>… " to discard changes in working directory)
modified: Imno.txt
untracked files:
(use "git add <file>… " to include in what will be committed)
no changes added to commit (use "git add" and/or "git commit −a")

我們可以使用以下命令驗證工作目錄的內容 −

$ ls // lists contents of the current directory
$ cat newfile.txt // lists contents of newfile.txt
$ cat lmno.txt // list contents of lmno.txt

輸出如下所示 −

//output of ls
abc.txt lmno.txt newfile.txt
//output of cat newfile.txt
hello
//output of cat lmno.txt
lmno
lmno lmno

清理暫存區

成功應用暫存區後,我們可能需要清理內容,因為暫存區不會預設刪除。我們可以使用以下命令刪除特定暫存區。

$ git stash drop 0

要一次性清除暫存區中的所有內容,我們可以使用以下命令

$ git stash clear

更新於:2021年2月20日

700 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.