Git command line samples

Git command recipes


  1. init $ git init $ git init --bare
  2. status $ git status
  3. 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'
  4. commit $ git commit
  5. checkout $ git checkout <file> $ git checkout <SHA1> $ git checkout <branch_name> $ git checkout -b <new_branch_name> $ git checkout --orphan <new_branch_name>
  6. diff $ git diff <SHA1> $ git diff <SHA1> <SHA1> $ git diff --stat <SHA1> $ git diff --cached [file] $ git diff --staged [file]
  7. 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 #完全清除所有修改
  8. revert $ git revert <SHA1> $ git revert <branch>
  9. 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 時可省略來源。
  10. 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 項裡介紹
  11. cherry-pick $ git cherry-pick <SHA1> [<SHA1> [<SHA1>...]] $ git cherry-pick -x <SHA1> [<SHA1> [<SHA1>...]] # -x參數會在 log 裡加注本來是哪個 SHA1 (適合用在 backpointing 情境)
  12. rebase $ git rebase targetNode $ git rebase targetNode sourceNode $ git rebase -i <SHA1>
  13. log $ git log $ git log --oneline $ git log --oneline --decorate --graph
  14. tag $ git tag tagName $ git tag tagName <SHA1> $ git tag tagName -m "some message" $ git tag -d tagName
  15. bisect (二分查找)
  16. grep
  17. 相對引用(^)
  18. 相對引用(~)
  19. clean 刪除 untracked 的檔案 $ git clean -n #列出會被清除的檔案 $ git clean -f #真的清除 $ git clean -x #連 gitignore 所列的檔案也清除
  20. reflog $ git reflog
  21. fsck $ git fsck --lost-found
  22. stash $ git stash $ git stash apply $ git stash clear

Remote

  1. 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
  2. clone $ git clone git://github.com/my/project.git [folder]
  3. fetch
  4. pull $ git pull origin :local_branch_to_create
  5. push $ git push --tags $ git push origin :remote_branch_to_delete
    #git pull 和 git fetch 不會清除已經刪除的分支

    #要更新已刪除分支用 git fetch -p


  6. 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

留言

這個網誌中的熱門文章

ng2-smart-table 自定 column 資料

C# Create An AutoComplete TextBox on WinForm

查出哪個程式佔用 port 443