Git常用命令備忘

2021-06-21 13:55:13 字數 4507 閱讀 3880

git config --global user.name "robbin"   

git config --global user.email "[email protected]"

git config --global color.ui true

git config --global alias.co checkout

git config --global alias.ci commit

git config --global alias.st status

git config --global alias.br branch

git config --global core.editor "mate -w"

# 設定editor使用textmate

git config -l # 列舉所有配置

使用者的git配置檔案~/.gitconfig

git help   # 顯示command的help

git show # 顯示某次提交的內容

git show $id

git co -- # 拋棄工作區修改

git co . # 拋棄工作區修改

git add # 將工作檔案修改提交到本地暫存區

git add . # 將所有修改過的工作檔案提交暫存區

git rm # 從版本庫中刪除檔案

git rm --cached # 從版本庫中刪除檔案,但不刪除檔案

git reset # 從暫存區恢復到工作檔案

git reset -- . # 從暫存區恢復到工作檔案

git reset --hard # 恢復最近一次提交過的狀態,即放棄上次提交後的所有本次修改

git ci

git ci .

git ci -a # 將git add, git rm和git ci等操作都合併在一起做

git ci -am "some comments"

git ci --amend # 修改最後一次提交記錄

git revert <$id> # 恢復某次提交的狀態,恢復動作本身也建立了一次提交物件

git revert head # 恢復最後一次提交的狀態

git diff      # 比較當前檔案和暫存區檔案差異

git diff

git diff <$id1>

<$id2> # 比較兩次提交之間的差異

git diff

.. # 在兩個分支之間比較

git diff --staged # 比較暫存區和版本庫差異

git diff --cached # 比較暫存區和版本庫差異

git diff --stat # 僅僅比較統計資訊

git log

git log

# 檢視該檔案每次提交記錄

git log -p # 檢視每次詳細修改內容的diff

git log -p -2

# 檢視最近兩次詳細修改內容的diff

git log --stat

# 檢視提交統計資訊

mac上可以使用tig代替diff和log,brew install tig

git br -r           # 檢視遠端分支

git br # 建立新的分支

git br -v # 檢視各個分支最後提交資訊

git br --merged # 檢視已經被合併到當前分支的分支

git br --no-merged # 檢視尚未被合併到當前分支的分支

git co

# 切換到某個分支

git co -b # 建立新的分支,並且切換過去

git co -b

# 基於branch建立新的new_branch

git co $id # 把某次歷史提交記錄checkout出來,但無分支資訊,切換到其他分支會自動刪除

git co $id -b # 把某次歷史提交記錄checkout出來,建立成乙個分支

git br -d

# 刪除某個分支

git br -d

# 強制刪除某個分支 (未被合併的分支被刪除的時候需要強制)

git merge 

# 將branch分支合併到當前分支

git merge origin/master --no-ff # 不要fast-foward合併,這樣可以生成merge提交

git rebase master

# 將master rebase到branch,相當於:

git co

&& git rebase master && git co master && git merge

git diff > ../sync.patch         # 生成補丁
git stash                        # 暫存

git stash list

# 列所有stash

git stash drop # 刪除暫存區

git pull                         # 抓取遠端倉庫所有分支更新並合併到本地

git pull --no-ff # 抓取遠端倉庫所有分支更新並合併到本地,不要快進合併

git fetch origin # 抓取遠端倉庫更新

git merge origin/master # 將遠端主分支合併到本地當前分支

git co --track origin/branch # 跟蹤某個遠端分支建立相應的本地分支

git co -b origin/ # 基於遠端分支建立本地分支,功能同上

git push # push所有分支

git push origin master # 將本地主分支推到遠端主分支

git push -u origin master # 將本地主分支推到遠端(如無遠端主分支則建立,用於初始化遠端倉庫)

git push origin # 建立遠端分支, origin是遠端倉庫名

git push origin : # 建立遠端分支

git push origin : #先刪除本地分支(git br -d

),然後再push刪除遠端分支

git remote -v                    # 檢視遠端伺服器位址和倉庫名稱

git remote show origin # 檢視遠端伺服器倉庫狀態

git remote add origin git@github:robbin/robbin_site.git # 新增遠端倉庫位址

git remote set-url origin [email protected]:robbin/robbin_site.git # 設定遠端倉庫位址(用於修改遠端倉庫位址)

git remote rm # 刪除遠端倉庫

git clone --bare robbin_site robbin_site.git  # 用帶版本的專案建立純版本倉庫

scp -r my_project.git [email protected]:~ # 將純倉庫上傳到伺服器上

mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在伺服器建立純倉庫

git remote add origin [email protected]:robbin/robbin_site.git # 設定遠端倉庫位址

git push -u origin master # 客戶端首次提交

git push -u origin develop # 首次將本地develop分支提交到遠端develop分支,並且track

git remote set-head origin master # 設定遠端倉庫的head指向master分支

也可以命令設定跟蹤遠端庫和本地庫

git branch --set-upstream master origin/master

git branch --set-upstream develop origin/develop

Git常用命令備忘

git config global user.name vito zhou git config global user.email zhouwentao16 gmail.com git config global color.ui true git config global alias.co c...

Git常用命令備忘

git config global user.name robbin git config global user.email fankai gmail.com git config global color.ui true git config global alias.co checkout g...

git常用命令備忘

常用命令 git reset hard 提交號 暫存區賒銷修改 onstage 也可以回退版本 git add 檔名 git commit m git log git status git reflow 命令執行日誌 git checkout 檔名 把工作區撤銷到最近一次commit或add時的狀態...