天天看點

Git 常用指令

 一、拿代碼

repo init -u url

初始化版本庫,在目前目錄建立一個".repo",  -u 參數指定一個url, 從這個url 中取得repository 的 manifest

檔案.

1.拿android主線上所有的sourcecode:

  repo init -u git://android.git.kernel.org/platform/manifest.git

2.拿某個branch而不是主線上的代碼,加入-b參數:

repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake

3.拿某一個project中的某一部分代碼,用git

clone:

git clone git://android.git.kernel.org/kernel/common.git

二、同步代碼

repository的代碼到本地

repo sync

三、檢視分支

  1.檢視本地和遠端分支, remote開頭的都是遠端分支:

git branch -av

2.檢視本地分支:

git branch

3.如果沒有本地分支,需要建立本地分支:

git branch branch1

或者

git checkout -b

branch1 origin/branch1

如果有多個本地分支,可以用git

checkout thebranchyouwannaon 切換到你想在的本地分支。

4,删除本地分支:

git branch -d

thebranchyouwannatodelete

5.檢視處在哪個遠端分支:

git remote

-v

git romote show

aosp

四、檢視送出曆史

1.git

log

2.檢視送出曆史并列出修改内容 -p,

-2表示隻顯示兩次送出記錄。

git log -p

-2

3.顯示被送出的檔案名:

git log

--stat

4.将每次送出的commitcode和commitcomment單行顯示:

--pretty=oneline

5.顯示某次送出的某個檔案的修改内容:

git show

commitcode filename

五、下載下傳代碼

1.git pull

如果遠端分支和本地分支有沖突,會遇到merge conflict提示,然後要手動解決沖突。

2.git fetch

 git merge origin/ branch1

fetch下載下傳伺服器代碼到本地,但不自動合并。可以先git checkout origin/

branch1,切換到遠端分支,看看代碼修改情況,

然後再決定是否merge。git pull = git fetch + git merge.

 3.git checkout

branch1

    git merge branch2

切換到branch1,然後将branch2上的代碼merge到branch1上。

六、送出修改

修改相關檔案後可通過git status檢視被修改的檔案,如a.c:

1.從working

directory送出到index

git add

a.c

2.從index送出到本地repository

git commit -am

"modify a.c" 

3.從本地repository送出到遠端repository

git push origin branch1

七、送出關系

在本地的代碼中分為working directory, index, repository,他們的關系如下:

Git 常用指令

八、比較送出

1.比較working directory 和 index:

git diff

2.比較index 和 repository:

git diff --cached

3.比較working directory 和

repository:

git diff head

4.比較遠端分支檔案 和 working directory:

git diff remote/remtotebranch

workingdirectoryfilename

5.比較兩次已送出版本:

git diff commitcode1 commitcode2

九、代碼回退

1.git reset head~1

回退repository 和 index, 但不回退working

directory。head~1表示回退到前一次送出。

2.git reset --soft head~2

隻回退repository。head~2表示回退到前2次送出。

3.git reset --hard head~3

repository、index 和 working

directory全部回退。head~3表示回退到前3次送出。

git參考網址: