# git 退版 reset / command ### 先理解git的每個狀態 ![](https://i.imgur.com/ttROcY8.png) --- ### local本地退版,並保留做錯的文檔 > 從 7a525f0 回到 8cfae32,但是想在工作區保留這段時間(7a525f0)更改的文檔. 1. 在 8cfae32 reset current branch to this commit -> hard. 2. 再到之前的head 7a525f0 reset current branch to this commit -> soft. 3. stage 區域就會有 7a525f0 的修改. 4. 最後結果不會砍掉 7a525f0 而是以 cae30e2 覆蓋. ![](https://i.imgur.com/ROUpNab.png) --- ### 遠端退版,並以舊的版本push到遠端 > 從 9cc62eb 退到 5e4ee6e. 並以 5e4ee6e push to remote. ![](https://i.imgur.com/pFcCFto.png) 1. 計算要退5個commit. 如果要退的commit數很多,可以少退幾個確認head在哪再繼續退. ![](https://i.imgur.com/eWymltR.png) 2. `git reset --hard HEAD~4` 退四版確認head位置 3. `git reset --hard HEAD~` 4. `git push --force` 退版後的版本push到遠方 ### git command ```shell= ## 查詢現在分支. $ git checkout ## 切換分支. $ git checkout [branch name] ## 查出所有歷程 $ git reflog ## 前面n筆資料 $ git log --oneline -n ## 同步遠端分支 $ git remote update -p ## 退到上一個commit $ git reset --hard HEAD~ $ git reset --hard HEAD~1 ## 退到n個commit前 $ git reset --hard HEAD~n ## 刪除遠端分支 :[branch name] $ git push origin :feature/name $ git branch -d <branch> ``` reference: https://gitbook.tw/chapters/github/delete-remote-branch.html https://dotblogs.com.tw/michaelfang/2016/09/07/git-reset-log-reflog https://gitbook.tw/chapters/using-git/reset-commit