--- tags: Git --- # Git筆記 | 修改人員 | 版本 | 註解 | 修改時間 | | :--- |:-------------:|:-----|:---| | Pemo | 1.0.0 | 新增文件與說明 | 2022/08/01 | | Pemo | 1.1.0 | 新增branch說明 | 2022/09/26 | | Pemo | 1.2.0 | 新增merge說明 | 2022/10/03 | | Pemo | 1.3.0 | 新增stash命令 | 2022/12/01 | | Pemo | 1.3.1 | 新增退出說明&stash說明修改 | 2022/12/02 | | Pemo | 1.4.0 | 新增stash說明 | 2022/12/20 | | Pemo | 1.5.0 | 新增merge說明 | 2022/12/22 | | Pemo | 1.6.0 | 新增commit操作說明 | 2023/02/03 | | Pemo | 1.7.0 | 新增reflog操作說明 | 2023/02/06 | | Pemo | 1.8.0 | 新增log說明,新增tag說明 | 2023/02/14 | | Pemo | 1.9.0 | 新增unstaged說明 | 2023/03/10 | | Pemo | 1.9.1 | 補充log說明 | 2023/06/19 | - commit範例 ``` //新增commit git commit -m "commit message" //刪除做好的commit git log //查看commit git reset aaaaa...^ //刪除該commit,建議在還沒上傳前使用 ``` - merge範例 ``` // 將<branch>合併至當前分支 git merge <branch> // 將合併取消 git merge --abort ``` - stash範例 ``` //save git stash -m "message" //check stash list git stash list //load git stash apple "stash@{number}" //load & remove git stash pop "stash@{number}" //remove git stash drop "stash@{number}" //remove all git stash clear ``` - rebase範例 ``` git rebase <branch> 不想要此次rebase可以使用 git reset ORIG_HEAD --hard ``` - requash範例 ``` git rebase -i <commit> 處理好要pick和squash的commit後,按下ESC,輸入:wq 儲存並離開的意思 解決衝突 重新輸入commit名稱 checkout到dev分支,cherry-pick過去 ``` - cherry-pick範例 ``` git cherry-pick <commit> ``` - reset範例 ``` git reset --hard HEAD~1 # 數字表示移動到 HEAD後面第幾個 ``` - branch範例 ``` 查看所有分支 git branch -a 刪除本地分支 git branch --delete <branch> 刪除遠端分支 git push <remote> --delete <branch> ``` - reflog範例,用於拯救被reset的branch和commit的搜尋commit方法 ``` //查看最近HEAD的移動紀錄,可以得到被reset的commit SHA1 git reflog //reset回reset之前的commit git reset <要復原的commit的SHA1碼> --hard ``` - log範例 ``` //check recently commit SHA-1(abbreviation)、branch name、tag name、commit message //查看近期的commit的SHA-1(縮寫)、分支名稱、標籤名稱、commit訊息 git log --oneline //check recently commit SHA-1(all)、branch name、tag name、commit message、create personnel、create time //查看近期的commit的SHA-1(完整)、分支名稱、標籤名稱、commit訊息、建立人員、建立時間 git log ``` - tag範例 ``` //create tag at commit //在commit上新增標籤 git tag <tag name> <commit SHA-1> -a -m <"tag message"> //create tag at commit //在當前commit上新增標籤 git tag -a "tag name" -m "tag message" //check all tag //查看全部標籤 git tag //push all tags to remote //上傳所有tag到遠端 git push origin --tags ``` - 還原unstaged範例 ``` //還原單檔 git checkout -- <file> //還原所有unstaged git checkout -- . ``` - 查看太多資訊時最後會出現END,此時輸入q就可以退出 [參考資料](https://git-scm.com/book/zh-tw/v2/Git-%E5%9F%BA%E7%A4%8E-%E5%BE%A9%E5%8E%9F) 參考來源: [gitbook.tw](https://gitbook.tw/interview)