GIT 常用命令大全及其說明

創建

  • 克隆一個已經存在的倉庫
$ git clone ssh://[email protected]/repo.git
  • 創建一個新的本地倉庫
$ git init

本地變化

  • 查看你的工作目錄下被改動過的文件
$ git status
  • 查看和被追蹤文件對比有哪些變化(即當前文件和被追蹤文件的不同)
$ git diff
  • 將所有現在的改動添加到下一次提交中
$ git add .
  • 將 file 中的一些改動添加到下一次提交中
$ git add -p <file>
  • 提交將被追蹤文件中的所有本地改動
$ git commit -a
  • 提交先前已經暫存的改動
$ git commit
  • 修改最後一次提交
    不要修改已經發布的提交記錄!
$ git commit --amend

提交歷史

  • 展示所有提交記錄,以最新提交開始開頭
$ git log
  • 展示過去的時間內指定的文件的改動
$ git log -p <file>
  • 在file 中什麼人在什麼時候改過什麼內容
$ git blame <file>

分支和標籤

  • 列出所有存在的分支
$ git branch
  • 切換當前分支(Switch HEAD branch)
$ git checkout <branch>
  • 基於你的當前分支創建一個新分支
$ git branch <new-branch>
  • 基於一個遠程分支創建一個新的跟蹤分支
$ git branch --track <new-branch> <remote-branch>
  • 刪除一個本地分支
$ git branch -d <branch>
  • 用一個標籤標記當前提交記錄
$ git tag <tag-name>

更新和發佈

  • 列出當前配置的遠程倉庫
$ git remote -v
  • 展示關於一個遠程倉庫的信息
$ git remote show <remote>
  • 添加新的遠程倉庫,名字叫做 reomte
$ git remote add <remote> <url>
  • 從 remote 下載所有改動,但是不集成到 HEAD中
$ git fetch <remote>
  • 下載所有改動並且直接merge/integrate(合併/集成)到HEAD(頭節點)中
$ git pull <remote> <branch>
  • 在一個遠程倉庫上發佈本地改動
$ git push <remote> <branch>
  • 在一個遠程倉庫上刪除一個分支
$ git push <remote> :<branch>

(譯者注:另一種方式爲  $ push origin --delete <branch>)
  • 發佈你的標籤
$ git push --tags

合併和重定基底(merge & rebase)

  • 合併 branch 到你的當前HEAD分支
$ git merge <branch> 
  • 將你的當前HEAD重定基底到 branch
    不要對已發佈的提交記錄進行重定基底!
$ git rebase <branch>
  • 中止重定基底
$ git rebase --abort
  • 在解決衝突之後繼續重定基底
$ git rebase --continue
  • 使用你配置的合併工具來解決衝突
$ git mergetool
  • 使用你的編輯器來手動解決衝突並且(在解決完衝突後)標記文件爲已經解決衝突
$ git add <resolved-file>

$ git rm <resolved-file>

撤銷

  • 刪除在你的工作目錄中的所有本地改動
$ git reset --hard  HEAD
  • 刪除指定文件中的本地改動
$ git checkout HEAD <file>
  • 復原一個提交記錄(通過以相反的改動生成一個新的提交記錄實現)
$ git revert <commit>
  • 重置你的HEAD指針爲一個先前的提交

    • 忽略從那個提交開始的所有改動

      $ git reset --hard <commit>
    • 保存所有改動爲未暫存的改動

      $ git  reset   <commit>
    • 保存所有未提交的本地改動

      $ git reset --keep <commit>

原文

https://www.git-tower.com/blog/git-cheat-sheet/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章