1. 程式人生 > >git 程式碼管理工具 命令總結

git 程式碼管理工具 命令總結

個人習慣 一般情況下現在遠端建立程式碼庫,然後克隆到本地,這樣的好處是直接了當編輯器命令列推送,簡單方便,所以就不討論 git init 了,想要了解的自行上網查閱資料;

git clone
git clone -b 分支名  git專案地址
git cone git專案地址  //預設克隆的是主分支的專案

clone下來的repo會以url最後一個斜線後面的名稱命名,建立一個資料夾,如果想要指定特定的名稱,可以git clone [url] newname指定.

git status
 查詢repo的狀態.
 git status -s: -s表示short, -s的輸出標記會有兩列,第一列是對staging區域而言,第二列是對working目錄而言.
git log
 show commit history of a branch.
 git log --oneline --number: 每條log只顯示一行,顯示number條.
 git log --oneline --graph:可以圖形化地表示出分支合併歷史.
 git log branchname可以顯示特定分支的log.
 git log --decorate會顯示出tag資訊.
 git log --author=[author name] 可以指定作者的提交歷史.
 git log --since --before --until --after 根據提交時間篩選log.
 --no-merges可以將merge的commits排除在外.
 git log --grep 根據commit資訊過濾log: git log --grep=keywords
git add
 在提交之前,Git有一個暫存區(staging area),可以放入新新增的檔案或者加入新的改動
 git add .
 會遞迴地添加當前工作目錄中的所有檔案.
git commit
 提交已經被add進來的改動.
 git commit -m “the commit message"
 git commit -a 會先把所有已經track的檔案的改動add進來,然後提交(有點像svn的一次提交,不用先暫存).
 git commit --amend 增補提交. 會使用與當前提交節點相同的父節點進行一次新的提交,舊的提交將會被取消.
git revert
 反轉撤銷提交.只要把出錯的提交(commit)的名字(reference)作為引數傳給命令就可以了.
 git revert HEAD: 撤銷最近的一個提交.
 git revert會建立一個反向的新提交,可以通過引數-n來告訴Git先不要提交.
git rm
 git rm file: 從staging區移除檔案,同時也移除出工作目錄.
 git rm --cached: 從staging區移除檔案,但留在工作目錄中.
 git rm --cached從功能上等同於git reset HEAD,清除了快取區,但不動工作目錄樹.
git clean
 git clean是從工作目錄中移除沒有track的檔案.
 通常的引數是git clean -df:
 -d表示同時移除目錄,-f表示force,因為在git的配置檔案中, clean.requireForce=true,如果不加-f,clean將會拒絕執行.
git mv
 git rm - - cached orig; mv orig new; git add new
git stash
 把當前的改動壓入一個棧.
 git stash將會把當前目錄和index中的所有改動(但不包括未track的檔案)壓入一個棧,
 然後留給你一個clean的工作狀態,即處於上一次最新提交處.
 git stash list會顯示這個棧的list.
 git stash apply:取出stash中的上一個專案([email protected]{0}),並且應用於當前的工作目錄.
 也可以指定別的專案,比如git stash apply [email protected]{1}.
 如果你在應用stash中專案的同時想要刪除它,可以用git stash pop
 刪除stash中的專案:
 git stash drop: 刪除上一個,也可指定引數刪除指定的一個專案.
 git stash clear: 刪除所有專案.
git branch
 git branch可以用來列出分支,建立分支和刪除分支.
 git branch -v可以看見每一個分支的最後一次提交.
 git branch: 列出本地所有分支,當前分支會被星號標示出.
 git branch (branchname): 建立一個新的分支(當你用這種方式建立分支的時候,分支是基於你的上一次提交建立的). 
 git branch -d (branchname): 刪除一個分支.
 刪除remote的分支:
 git push (remote-name) :(branch-name): delete a remote branch.
 這個是因為完整的命令形式是:
 git push remote-name local-branch:remote-branch
 而這裡local-branch的部分為空,就意味著刪除了remote-branch
git checkout

git checkout (branchname)
切換到一個分支.
git checkout -b (branchname): 建立並切換到新的分支.
這個命令是將git branch newbranch和git checkout newbranch合在一起的結果.
checkout還有另一個作用:替換本地改動:
git checkout –
此命令會使用HEAD中的最新內容替換掉你的工作目錄中的檔案.已新增到暫存區的改動以及新檔案都不會受到影響.
注意:git checkout filename會刪除該檔案中所有沒有暫存和提交的改動,這個操作是不可逆的.

git merge
 把一個分支merge進當前的分支.
 git merge [alias]/[branch]
 把遠端分支merge到當前分支.

 如果出現衝突,需要手動修改,可以用git mergetool.
 解決衝突的時候可以用到git diff,解決完之後用git add新增,即表示衝突已經被resolved.
git remote
 list, add and delete remote repository aliases.
 因為不需要每次都用完整的url,所以Git為每一個remote repo的url都建立一個別名,然後用git remote來管理這個list.
 git remote: 列出remote aliases.
 如果你clone一個project,Git會自動將原來的url新增進來,別名就叫做:origin.
 git remote -v:可以看見每一個別名對應的實際url.
 git remote add [alias] [url]: 新增一個新的remote repo.
 git remote rm [alias]: 刪除一個存在的remote alias.
 git remote rename [old-alias] [new-alias]: 重新命名.
 git remote set-url [alias] [url]:更新url. 可以加上—push和fetch引數,為同一個別名set不同的存取地址.
git fetch
 download new branches and data from a remote repository.
 可以git fetch [alias]取某一個遠端repo,也可以git fetch --all取到全部repo
 fetch將會取到所有你本地沒有的資料,所有取下來的分支可以被叫做remote branches,
 它們和本地分支一樣(可以看diff,log等,也可以merge到其他分支),但是Git不允許你checkout到它們. 
git pull
 fetch from a remote repo and try to merge into the current branch.
 pull == fetch + merge FETCH_HEAD
 git pull會首先執行git fetch,然後執行git merge,把取來的分支的head merge到當前分支.
 這個merge操作會產生一個新的commit.    
 如果使用--rebase引數,它會執行git rebase來取代原來的git merge.
git rebase
 --rebase不會產生合併的提交,它會將本地的所有提交臨時儲存為補丁(patch),放在”.git/rebase”目錄中,
 然後將當前分支更新到最新的分支尖端,最後把儲存的補丁應用到分支上.
 rebase的過程中,也許會出現衝突,Git會停止rebase並讓你解決衝突,在解決完衝突之後,用git add去更新這些內容,
 然後無需執行commit,只需要:
 git rebase --continue就會繼續打餘下的補丁.
 git rebase --abort將會終止rebase,當前分支將會回到rebase之前的狀態.
git push
 push your new branches and data to a remote repository.
 git push [alias] [branch]
 將會把當前分支merge到alias上的[branch]分支.如果分支已經存在,將會更新,如果不存在,將會新增這個分支.
 如果有多個人向同一個remote repo push程式碼, Git會首先在你試圖push的分支上執行git log,
 檢查它的歷史中是否能看到server上的branch現在的tip,如果本地歷史中不能看到server的tip,
 說明本地的程式碼不是最新的,Git會拒絕你的push,讓你先fetch,merge,之後再push,這樣就保證了所有人的改動都會被考慮進來.