Git command line samples
Git command recipes
- init $ git init $ git init --bare
- status $ git status
- add $ git add <file> $ git add <file> --patch $ git add -u #只包含刪除的檔案,不包含新增檔案 $ git add . #只包含修改和新增,不包含刪除 $ git addall #通通加到 staging area
addall = !sh -c 'git add . && git add -u'
- commit $ git commit
- checkout $ git checkout <file> $ git checkout <SHA1> $ git checkout <branch_name> $ git checkout -b <new_branch_name> $ git checkout --orphan <new_branch_name>
- diff $ git diff <SHA1> $ git diff <SHA1> <SHA1> $ git diff --stat <SHA1> $ git diff --cached [file] $ git diff --staged [file]
- reset $ git reset <file> $ git reset HEAD --hard #放棄合併 $ git reset <SHA1> $ git reset HEAD^ #留著修改在 working tree $ git reset HEAD^ --soft #留著修改在 staging area $ git reset HEAD^ --hard #完全清除所有修改
- revert $ git revert <SHA1> $ git revert <branch>
- branch $ git branch <branch_name> $ git branch -f <branch_name> <SHA1> $ git branch -m old_name new_name $ git branch -M old_name new_name #強制覆蓋 $ git branch branch_name -d # 刪除分支 $ git branch branch_name -D # 強制刪除分支 $ git branch --set-upstream-to=origin/master master # 設定本地庫的master分支來源為remote/origin/master,當使用 pull 或 merge 時可省略來源。
- merge $ git merge <branch> $ git merge <SHA1> Straight merge(Default) $ git merge <SHA1> $ git merge <SHA1> --no-ff Squashed commit $ git merge new_feature --squash cherry-pick 在第 11 項裡介紹
- cherry-pick $ git cherry-pick <SHA1> [<SHA1> [<SHA1>...]] $ git cherry-pick -x <SHA1> [<SHA1> [<SHA1>...]] # -x參數會在 log 裡加注本來是哪個 SHA1 (適合用在 backpointing 情境)
- rebase $ git rebase targetNode $ git rebase targetNode sourceNode $ git rebase -i <SHA1>
- log $ git log $ git log --oneline $ git log --oneline --decorate --graph
- tag $ git tag tagName $ git tag tagName <SHA1> $ git tag tagName -m "some message" $ git tag -d tagName
- bisect (二分查找)
- grep
- 相對引用(^)
- 相對引用(~)
- clean 刪除 untracked 的檔案 $ git clean -n #列出會被清除的檔案 $ git clean -f #真的清除 $ git clean -x #連 gitignore 所列的檔案也清除
- reflog $ git reflog
- fsck $ git fsck --lost-found
- stash $ git stash $ git stash apply $ git stash clear
Remote
- remote $ git remote [-v | --verbose] $ git remote add [-t <branch>] [-m <master>https://a.b.c/project.git $ git remote rename <old> <new> $ git remote
- clone $ git clone git://github.com/my/project.git [folder]
- fetch
- pull $ git pull origin :local_branch_to_create
- push $ git push --tags $ git push origin :remote_branch_to_delete
#git pull 和 git fetch 不會清除已經刪除的分支
#要更新已刪除分支用 git fetch -p
- branch $ git branch -r $ git checkout -t origin/foo $ git branch -r --merged #列出已經被合併的 $ git branch --track experimental origin/experimental $ git branch --set-upstream-to=origin/master master
留言
張貼留言