Try   HackMD

🌿 Git Cheat Sheet

Sorucetree 動作對應

Checkout Commit

目前的前一個 commit

git checkout HEAD~1

復原

git checkout -

- is a alias of @{-1} which represents the name of the previous current branch

Stage & Unstage

Stage File

git add <fileName> ga
git add .

The difference is that git add -A also stages files in higher directories that still belong to the same git repository.

git add --all git add -A gaa

https://gist.github.com/dsernst/ee240ae8cac2c98e7d5d

Unstage File

git reset <fileName> grh <fileName>

Unstage all files

git reset grh

Commit

git commit gm

從檔案取得 commit message

git commit --file <fileName> git commit -F <fileName>

Reset to the commit

撤銷最新的 commit

# 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

git checkout -b <name> gcb <name>

建立 branch

git branch <name> gb <name>

移除分支 (delete branch)

# 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>

查看所有分支(包含遠端)

git branch --all gba

刪除所有已經合并的分支

git branch --merged | grep -Ev "(^\*|master|main|dev)" | xargs git branch -d

Discard

git checkout -- <file> gco <file>

discard glob expansion

git checkout -- a\b\c\**

Stash

只 Stash staged 檔案

git stash push --staged git stash push -S gsta -S

Stash 指定檔案

git stash push -m <message> <file>

Remote

新增 Remote

git remote add <name> <url>

顯示 Remote

git remote -v

https://stackoverflow.com/questions/58031165/how-to-get-rid-of-would-clobber-existing-tag

其他

刪除本地所有已合併的分支

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

git fetch --tags -f

印出 commit 的檔案變化

git --no-pager diff --name-only commit1..commit2

GIT CHEAT SHEET

https://education.github.com/git-cheat-sheet-education.pdf****