在 Git 中將本地更改恢復到以前的狀態
    
    Ashok Chapagai
    2023年1月30日
    
    Git
    Git Revert
    
 
假設 Mario 被分配了一個任務並且即將完成它,但是客戶改變了他們的要求並要求 Mario 停止執行之前分配的任務,那麼解決這個困境的完美解決方案是什麼?
在本文中,你將學習如何在 Git 中恢復對先前狀態的本地更改。
在 Git 中恢復未暫存的本地更改
如果你沒有使用通常將檔案推送到暫存區的命令 git add,你可以按照以下步驟輕鬆導航到之前的狀態。
- 
使用git status確認檔案的狀態。$ git status On branch dev Your branch is up to date with 'origin/dev'. 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: frontend/src/index.tsx modified: frontend/src/common/components/index.tsx modified: frontend/src/common/components/index.css
- 
在已知狀態的情況下,你可以根據自己的喜好使用以下任何選項。- 覆蓋本地更改
 git checkout -- <file>- 儲存本地更改以便以後在專案中使用,
 git stash- 放棄對檔案所做的所有更改
 git reset --hard
撤銷 Git 中的本地修改
如果你使用 git add 命令新增了檔案,我們可以按照以下步驟將其恢復到以前的狀態。
- 
使用git status確認新增檔案的可用性。
- 
現在你已經看到了暫存的檔案,你可以根據情況選擇要恢復的檔案並使用以下命令。- 保留對檔案的更改,但不要暫存。
 git restore --staged <file_name_with.path>- 取消暫存所有保持更改的檔案,
 git reset- 丟棄所有更改並儲存以備後用。
 git stash注意:使用 git stash pop撤消git stash的效果並使用git stash list列出可用的儲存。- 丟棄一切
 git reset --hard
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Ashok Chapagai
    
