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單行顯示:

git log --pretty=oneline

5.顯示某次提交的內容:

git show commitCode

git show commitCode --stat

git show commitCode Filename

   6.查看某行代碼( 如fileName文件中函數xxx_notify() )的提交歷史:

   git blame fileName | grep xxx_notify

五、下載代碼

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,他們的關系如下:

 

 

八、比較提交

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參考網址:

http://www.open-open.com/lib/list/81?pn=8

http://blog.csdn.net/wh_19910525/article/details/7438183


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM