一、拿代码
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,他们的关系如下:
八、比较提交
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参考网址: