1. 程式人生 > 其它 >Pro Git --- 讀書筆記 (Chaptero5)

Pro Git --- 讀書筆記 (Chaptero5)

附錄C:Git命令

配置預設編輯器
git config --global core.editor "xxx"
配置提交時的使用者名稱稱和郵箱
git config --global user.name "xxx"
git config --global user.email xxx.com
配置Git命令的別名
git config --global alias.xx 'xxx'
新建一個倉庫
git init
克隆一個倉庫
git clone [-o [remoteName]] <url>
從工作目錄到暫存區
git add [./file]
檢視狀態
git status
  • 會顯示工作區以及暫存區中不同狀態的檔案
檢視任意兩棵樹的差異
  1. 工作區和暫存區的差異
git diff
  1. 暫存區和最後提交的差異
git diff --staged
  1. 兩個提交之間的差異
git diff <branch> <branch>
  1. 檢視分支之間的某段差異
git diff A..B
// 大於A小於B,即存在於B但不在A中的差異
提交
  1. 簡單提交
git commit -m ‘xxx’
  1. 跳過add提交
git commit -a -m 'aaa'
// 將已跟蹤檔案的修改全部加入暫存區,然後提交
  1. 重做提交
git commit --amend
// 合併最後一次提交的資訊
重置
  1. 重置HEAD
git reset --soft <commit hash>
  1. 重置HEAD和暫存區
git reset --mixed <commit hash>
  1. 全部重置
git reset --hard <commit hash>
分支
  1. 建立一個新分支
git branch <branch>
  1. 分支檢視
git branch [-v] [-vv]
  1. 分支刪除
git branch -d <branch>
  1. 檢視和當前分支有沒有合併的分支
git branch [--merged] [--no-merged]
  1. 指定跟蹤分支
git branch -u <remote>/<branch>
分支檢出
  1. 分支切換
git checkout <branch>
  1. 建立分支並切換
git checkout -b <branch>
  1. 建立或切換分支並指定跟蹤分支
git checkout -b <branch> <remote>/<branch>
合併
  1. 分支合併
git merge <branch>
  1. 單獨合併某一個提交的差異到當前分支
git cherry-pick <commit hash>
提交歷史
  1. 檢視歷史,預設從當前分支的最近提交開始
git log
  1. 限制檢視數量
git log -<num>
  1. 檢視每次提交所引入的差異
git log -p
  1. 限制格式化
git log --pretfen zhi
git log --author
  1. 關鍵字匹配
git log --grep 
貯藏
  1. 貯藏暫存區和工作目錄
git stash
  1. 檢視所有貯藏
git stash list
  1. 應用貯藏
git stash apply
  1. 指定應用貯藏
git stash apply stash@{num}
  1. 暫存區恢復
git stash apply --index
  1. 刪除貯藏
git stash drop stash@{num}
  1. 應用並丟棄
git stash pop
  1. 從貯藏建立分支
git stash branch <branch>
拉取
  1. 抓取
git fetch <remote>
拉取併合並
  1. 拉取合併
git pull
推送
  1. 推送
git push <remote> <branch>
遠端倉庫管理
  1. 檢視遠端倉庫
git remote [-v]
  1. 新增遠端倉庫
git remote add <name> <url>
  1. 檢視某個遠端倉庫
git remote show <remote>
  1. 重新命名遠端倉庫的本地記號
git remote rename <old> <new>
  1. 刪除遠端倉庫
git remote remove <remote>
變基
  1. 變基合併
git rebase <base> <topic>
  1. 三分支變基
  • 取出b3分支找出它與b2不同的補丁,然後將這些補丁變基在b1上
git rebase --onto <b1> <b2> <b3>
資料恢復
  1. 查詢丟失的commit
// 簡單資訊
git reflog
// 詳細資訊
git log -g
// 最後手段
git fsck --full
  1. 分支指向一個commit
git branch <branch> <commit>