1. 程式人生 > >總結下git中一些常用命令

總結下git中一些常用命令

一、目錄操作

1、cd   

即change directory,改變目錄,如 cd d:/www,切換到d盤的www目錄。

2、cd ..  

cd+空格+兩個點,回退到上一目錄。

3、pwd       

即 print working directory, 列印工作目錄,顯示當前所在路徑。

4、ls     

即list,列出當前列出當前目錄中的檔案(不包含隱藏檔案),ls -a(包含隱藏檔案),ll 更詳細。

5、touch     

新建一個檔案,如 touch index.php。

6、mv         

即move,移動檔案。

7、rm   

即remove,刪除一個檔案,如 rm index.php。

8、mkdir     

即make directory,新建一個目錄。

9、rm -r     

刪除一個資料夾, r (recusive 即遞迴),先刪除資料夾裡面的內容,再刪除資料夾。

二、配置操作

1、配置專案

git config -e,使用預設編輯器開啟專案所屬的配置檔案,作用域為通過git init初始化過的具體某個專案。

2、配置全域性

git config -e --global,使用預設編輯器開啟全域性配置檔案,通常在C:/Users/Administrator/下的 .gitconfig 檔案中,作用域為登陸此臺計算機的使用者。

3、配置系統

git config -e --system,使用預設編輯器開啟系統級配置檔案,通過在安裝目錄下的 etc 下的 gitconfig 檔案中,作用域為整臺計算機,不分登入賬號和專案。

優先順序為就近原則,即 git config > git confg --global > git config --system 。

三、專案操作

1、git init

在本地新建一個repo,進入一個專案目錄,執行git init,會初始化一個repo,並在當前資料夾下建立一個.git資料夾.

  2、git clone 獲取一個url對應的遠端Git repo, 建立一個local copy. 一般的格式是git clone [url]. clone下來的repo會以url最後一個斜線後面的名稱命名,建立一個資料夾,如果想要指定特定的名稱,可以git clone [url] newname指定.   3、git status 查詢repo的狀態. git status -s: -s表示short, -s的輸出標記會有兩列,第一列是對staging區域而言,第二列是對working目錄而言.   4、git log show commit history of a branch. git log --oneline --number: 每條log只顯示一行,顯示number條. git log --oneline --graph:可以圖形化地表示出分支合併歷史. git log branchname可以顯示特定分支的log. git log --oneline branch1 ^branch2,可以檢視在分支1,卻不在分支2中的提交.^表示排除這個分支(Window下可能要給^branch2加上引號). 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 log --grep --author是OR的關係,即滿足一條即被返回,如果你想讓它們是AND的關係,可以加上--all-match的option. git log -S: filter by introduced diff. 比如: git log -SmethodName (注意S和後面的詞之間沒有等號分隔). git log -p: show patch introduced at each commit. 每一個提交都是一個快照(snapshot),Git會把每次提交的diff計算出來,作為一個patch顯示給你看. 另一種方法是git show [SHA]. git log --stat: show diffstat of changes introduced at each commit.  同樣是用來看改動的相對資訊的,--stat比-p的輸出更簡單一些.      5、git add 在提交之前,Git有一個暫存區(staging area),可以放入新新增的檔案或者加入新的改動. commit時提交的改動是上一次加入到staging area中的改動,而不是我們disk上的改動. git add . 會遞迴地添加當前工作目錄中的所有檔案.   6、git diff 不加引數的git diff: show diff of unstaged changes. 此命令比較的是工作目錄中當前檔案和暫存區域快照之間的差異,也就是修改之後還沒有暫存起來的變化內容.   若要看已經暫存起來的檔案和上次提交時的快照之間的差異,可以用: git diff --cached 命令. show diff of staged changes. (Git 1.6.1 及更高版本還允許使用 git diff --staged,效果是相同的).   git diff HEAD show diff of all staged or unstated changes. 也即比較woking directory和上次提交之間所有的改動.   如果想看自從某個版本之後都改動了什麼,可以用: git diff [version tag] 跟log命令一樣,diff也可以加上--stat引數來簡化輸出.   git diff [branchA] [branchB]可以用來比較兩個分支. 它實際上會返回一個由A到B的patch,不是我們想要的結果. 一般我們想要的結果是兩個分支分開以後各自的改動都是什麼,是由命令: git diff [branchA]…[branchB]給出的. 實際上它是:git diff $(git merge-base [branchA] [branchB]) [branchB]的結果.     7、git commit 提交已經被add進來的改動. git commit -m “the commit message" git commit -a 會先把所有已經track的檔案的改動add進來,然後提交(有點像svn的一次提交,不用先暫存). 對於沒有track的檔案,還是需要git add一下. git commit --amend 增補提交. 會使用與當前提交節點相同的父節點進行一次新的提交,舊的提交將會被取消.   8、git reset undo changes and commits. 這裡的HEAD關鍵字指的是當前分支最末梢最新的一個提交.也就是版本庫中該分支上的最新版本. git reset HEAD: unstage files from index and reset pointer to HEAD 這個命令用來把不小心add進去的檔案從staged狀態取出來,可以單獨針對某一個檔案操作: git reset HEAD - - filename, 這個- - 也可以不加. git reset --soft move HEAD to specific commit reference, index and staging are untouched. git reset --hard unstage files AND undo any changes in the working directory since last commit. 使用git reset —hard HEAD進行reset,即上次提交之後,所有staged的改動和工作目錄的改動都會消失,還原到上次提交的狀態. 這裡的HEAD可以被寫成任何一次提交的SHA-1. 不帶soft和hard引數的git reset,實際上帶的是預設引數mixed.   總結: git reset --mixed id,是將git的HEAD變了(也就是提交記錄變了),但檔案並沒有改變,(也就是working tree並沒有改變). 取消了commit和add的內容. git reset --soft id. 實際上,是git reset –mixed id 後,又做了一次git add.即取消了commit的內容. git reset --hard id.是將git的HEAD變了,檔案也變了. 按改動範圍排序如下: soft (commit) < mixed (commit + add) < hard (commit + add + local working)   9、git revert 反轉撤銷提交.只要把出錯的提交(commit)的名字(reference)作為引數傳給命令就可以了. git revert HEAD: 撤銷最近的一個提交. git revert會建立一個反向的新提交,可以通過引數-n來告訴Git先不要提交.      10、git rm git rm file: 從staging區移除檔案,同時也移除出工作目錄. git rm --cached: 從staging區移除檔案,但留在工作目錄中. git rm --cached從功能上等同於git reset HEAD,清除了快取區,但不動工作目錄樹.   11、git clean git clean是從工作目錄中移除沒有track的檔案. 通常的引數是git clean -df: -d表示同時移除目錄,-f表示force,因為在git的配置檔案中, clean.requireForce=true,如果不加-f,clean將會拒絕執行.   12、git mv git rm - - cached orig; mv orig new; git add new   13、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: 刪除所有專案.   14、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   15、git checkout git checkout (branchname) 切換到一個分支, git checkout -b (branchname): 建立並切換到新的分支. 這個命令是將git branch newbranch和git checkout newbranch合在一起的結果. checkout還有另一個作用:替換本地改動: git checkout --<filename> 此命令會使用HEAD中的最新內容替換掉你的工作目錄中的檔案.已新增到暫存區的改動以及新檔案都不會受到影響. 注意:git checkout filename會刪除該檔案中所有沒有暫存和提交的改動,這個操作是不可逆的.   16、git merge 把一個分支merge進當前的分支. git merge [alias]/[branch] 把遠端分支merge到當前分支.   如果出現衝突,需要手動修改,可以用git mergetool. 解決衝突的時候可以用到git diff,解決完之後用git add新增,即表示衝突已經被resolved.   17、git tag tag a point in history as import. 會在一個提交上建立永久性的書籤,通常是釋出一個release版本或者ship了什麼東西之後加tag.  比如: git tag v1.0 git tag -a v1.0, -a引數會允許你新增一些資訊,即make an annotated tag. 當你執行git tag -a命令的時候,Git會開啟一個編輯器讓你輸入tag資訊.       我們可以利用commit SHA來給一個過去的提交打tag: git tag -a v0.9 XXXX   push的時候是不包含tag的,如果想包含,可以在push時加上--tags引數. fetch的時候,branch HEAD可以reach的tags是自動被fetch下來的, tags that aren’t reachable from branch heads will be skipped.如果想確保所有的tags都被包含進來,需要加上--tags選項.   18、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不同的存取地址.   19、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到它們.    20、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.      21、git rebase --rebase不會產生合併的提交,它會將本地的所有提交臨時儲存為補丁(patch),放在”.git/rebase”目錄中,然後將當前分支更新到最新的分支尖端,最後把儲存的補丁應用到分支上. rebase的過程中,也許會出現衝突,Git會停止rebase並讓你解決衝突,在解決完衝突之後,用git add去更新這些內容,然後無需執行commit,只需要: git rebase --continue就會繼續打餘下的補丁. git rebase --abort將會終止rebase,當前分支將會回到rebase之前的狀態.   22、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,這樣就保證了所有人的改動都會被考慮進來.   23、git reflog git reflog是對reflog進行管理的命令,reflog是git用來記錄引用變化的一種機制,比如記錄分支的變化或者是HEAD引用的變化. 當git reflog不指定引用的時候,預設列出HEAD的reflog. [email protected]{0}代表HEAD當前的值,[email protected]{3}代表HEAD在3次變化之前的值. git會將變化記錄到HEAD對應的reflog檔案中,其路徑為.git/logs/HEAD, 分支的reflog檔案都放在.git/logs/refs目錄下的子目錄中.