# 🌿 Git Cheat Sheet Sorucetree 動作對應 ## Checkout Commit 目前的前一個 commit ```bash= git checkout HEAD~1 ``` 復原 ```bash= git checkout - ``` > \- is a alias of `@{-1}` which represents the name of the previous current branch ## Stage & Unstage ### Stage File ```bash= git add <fileName> ga ``` ```bash= git add . ``` The difference is that `git add -A` also stages files in **higher directories** that still belong to the same git repository. ```bash= git add --all git add -A gaa ``` https://gist.github.com/dsernst/ee240ae8cac2c98e7d5d ### Unstage File ```bash= git reset <fileName> grh <fileName> ``` Unstage all files ```bash= git reset grh ``` ### Commit ```bash= git commit gm ``` 從檔案取得 commit message ```bash= git commit --file <fileName> git commit -F <fileName> ``` ## Reset to the commit 撤銷最新的 commit ```bash= # keep working copy but reset index git reset HEAD~1 grh HEAD~1 # keep all local changes git reset --soft HEAD~1 grhs HEAD~1 # discard all working copy changes git reset --hard HEAD~1 grhh HEAD~1 ``` ## Branch ### 建立並切換至該 branch ```bash= git checkout -b <name> gcb <name> ``` ### 建立 branch ```bash= git branch <name> gb <name> ``` ### 移除分支 (++d++elete branch) ```bash= # delete fully merged branch git branch --delete <name> git branch -d <name> gbd <name> # delete branch (even if not merged) = force delete git branch -D <name> ``` ### 查看所有分支(包含遠端) ```bash= git branch --all gba ``` ### 刪除所有已經合并的分支 ```bash= git branch --merged | grep -Ev "(^\*|master|main|dev)" | xargs git branch -d ``` ## Discard ```bash= git checkout -- <file> gco <file> ``` discard glob expansion ```bash= git checkout -- a\b\c\** ``` ## Stash ### 只 Stash staged 檔案 ```bash= git stash push --staged git stash push -S gsta -S ``` ### Stash 指定檔案 ```bash= git stash push -m <message> <file> ``` ## Remote ### 新增 Remote ```bash= git remote add <name> <url> ``` ### 顯示 Remote ```bash= git remote -v ``` > https://stackoverflow.com/questions/58031165/how-to-get-rid-of-would-clobber-existing-tag ## 其他 ### 刪除本地所有已合併的分支 ```bash= git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d ``` > https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged ### "would clobber existing tag" 重新 fetch 遠端 tag ```bash= git fetch --tags -f ``` ## 印出 commit 的檔案變化 ```bash= git --no-pager diff --name-only commit1..commit2 ``` ## GIT CHEAT SHEET https://education.github.com/git-cheat-sheet-education.pdf****