GIT
.
REPO
repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
create a repo and specify the branch, the default.xml file in this repository defines which branch each of the other git repositiories (projects) should be on.
repo forall -c 'git checkout branch_name'
You can run
the cmd
which will checkout the specified branch for all projects that are declared in your current manifest but if there are projects added/removed between different manifests (which there are for example, android sourcecode of gingerbread and ics), then you won't get the code for these projects.
A. if just use git to records your own codebase during developing
#to init an git repository in current folder
$git init
#if already has code here, add them all
$git add .
$git commit -m "init codebase"
B. if develop basing on an already exists codebase, for example, open source projects
#fetch an copy of the codebase from the remote git server
$git clone ssh://git@source.csdn.net/flyuelee/kernel.git
after got the full source code and before doing any changes, it is best to create branches:
#1.create a branch
$git branch newname
#2. and switch to work on this branch
$git checkout newname
you can also combine above two cmds into one:
$git checkout –b newname master
"master" is the original(or remote) branch your "newname" branch references.
you can apply this cmd to create different branches for the same codebase base on your develop scenarios.
for example, a master branch as mainline, a release branch for formal sharing with others, a working branch for all current develop ...
after all above down, you can do whatever you like on your branches.
after some days working or else, if you think one function or item you are developing is OK now,
you can use below command to know the file list that changed:
$git status
then you can phase in into your local codebase by:
$git add .
$git commit -m "some descriptions bout your commit"
the " git add ." means check in all current changes(by files)
if there are files that you do not want to check in by current commit,
replace "." to the file path you want to phase in.
if you'd like to modify the commit comments info after committed, just type as below and you can the commit info directly
$git commit --amend
and if not only the comment info but also some files need refine and check in, just append your file after the command above, which likes
$git commit --amend mypath/myfile
if you want to append all changes into the currently commit at once, type
$git commit -a --amend
even after remember that all steps above, the changes only applied on your cloned codebase locally,
codebase on remote server is not affected at all.
after changes checked in, you can still check the modifications by git cmd
#just for commit summary
$git log
#also show files summary
$git log --stat
#and more details in the files
$git log -p
above cmds just show all commit histories in sequence from latest to oldest, and if you just want to know an special commit,
just type
$git show <commit-sha-value>
and if you apply that by "git log <commit sha>", commits histories just be showed start from that one.
to search all commit history about some code, take "void evalPendingRequest" function as an example
frameworks/opt/telephony$ reset; git log -L '^/void evalPendingRequest/',+50:src/java/com/android/internal/telephony/dataconnection/DctController.java --no-merges --all-match
here we just have an "void evalPendingRequest" not longer than 50 lines and lies in "src/java/com/android/internal/telephony/dataconnection/DctController.java" of current path.
"^/reg/" means to search from the start of the file, and "+50" means following 50 linex since the searched line, the string after ":" is the file you'd like to search.
so, you can change it to whatever you like basing on your own conditions following this rule.
for more details, you can also look forward to "man git log".
another way to show the history more reable is by gitk utils.
if you want to make contributions to the server codebase and let all others got the update, try
$git push
for the same reason as others may also pushed their changes onto the server since you last codebase fecthing,
you can use below command to get the latest update from server
$git pull
as there are some changes local, it is best to append "--rebase" param while "pull".
it is a good habit to try "git pull --rebase" before you run "git push"
as talked earlier and as a fact, most time we'll develop on different branches in parallel. and at some stage, some changes on one branch may be
feasible to apply to other branches, for example, from one branch named "dev" to "master", using:
#1.checkout to "master" firstly
$git checkout master
#2.combine(merge) modifications from "dev" to "master"
$git merge dev
this is the simplest(fast-forward) merge way if all changes on one branches can go smoothly to the other one.
or you can make the merge into an commit by replace the merge cmd as below:
$git merge --squash dev
$git commit –m “something from dev”
that cmd will merge all changes, what if we just want one commit(321d76f) applied but leave all others unchanged?
$git cherry-pick 321d76f
how about two or more(df824, opi934d, ...)?
$git cherry-pick –n 321d76f
$git cherry-pick –n df824
...
$git commit –m “something from dev”
seems all fine now
but you may not be so lucky sometimes and here you got conflicts which means your local changes and the new changes got from server not co-work that harmoniously.
like:
<<<<<<< HEAD
test in master
=======
test in dev
>>>>>>> dev
"<<<<<<<" is stand for an conflict contents start and "HEAD" indicates the latest commit of current codes of yours;
codes below this line and before "=======" are the contents of current codes;
and the one between "=======" and ">>>>>>>" is the conents which want to merge but conflict.
you should check the code details and decide what to remove and keep.
that will always occur while update your local codebase or apply patches of others.
and so we get another part now.
sometimes you may have to provide your changes to others and let them to apply these commits.
we just call the differences of each commits as "patches"
and here we get two topics:
1.generate patches that you've made from your own codebase;
2.apply patches into target codebase.
you can use two ways to generate patches:
one is "diff" and the other is "format-patch"
#to use "diff" to make a patch, you will need redirect the output into a file by hand
$git diff >mypatch.patch
you can also add params if you just want to get two certain commits, for example 7bf03f and abcd68
$git diff 7bf03f abcd68 >mypatch.patch
remember to put the older one as the first param and the newer as the second.
and one other thing to let you know, this kind of patches only contain the code change but with no commit comment info.
the other way just provide the whole info as the commit phased:
$git format-patch
it will also generate patch files directly and the patch names start with numbers like "000**-<commit title>.patch" sequentially.
after patches ready, you can apply them now.
you can use "patch" cmd directly or standard "git apply"/"git am" cmd
$patch -p2 <mypatch.patch
#or
$git apply mypatch.patch
#or
$git am mypatch.patch
generaly speaking, "diff" and "patch" way is more looser for all kinds of patches,
and if using git tool for the whole codebase maintain, "apply/am" will be more recommended.
work not always so happy and especially for apply patch.
for using "patch" cmd, there will be ".org" and ".rej" files generated for the conflict, just check and modify the conflicts and double confirm by cmds provide earlier.
for "am" kind, here provide an example as below
#1#generate patches
code_src/telephony$ git format-patch -s 3ccb59
0001-block-add-customization-2.patch
"-s" param means generate patches since which commit it is "3ccb59" here, if not provided all commits since codebase created will be generated.
#2#try apply patch but conflict occurs
code_target/telephony$ git am ~/Downloads/mypatches/0001-block-add-customization-2.patch
Applying: block add customization 2
error: patch failed: src/java/com/telephony/dataker.java:3558
error: src/java/com/telephony/dataker.java: patch does not apply
Patch failed at 0001 block add customization 2
The copy of the patch that failed is found in:
/home/.../code_target/telephony/.git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
#3#apply as much code as possible and generate more details if any conflict by adding "reject" param
code_target/telephony$ git apply --reject ~/Downloads/mypatches/0001-block-add-customization-2.patch
Checking patch src/java/com/telephony/dataker.java...
Hunk #1 succeeded at 254 (offset -9 lines).
Hunk #2 succeeded at 1829 (offset -50 lines).
error: while searching for:
new Thread(new Runnable() {
public void run() {
synchronized (mLock) {
error: patch failed: src/java/com/telephony/dataker:3558
error: while searching for:
}
/** end feature.@}*/
private boolean Hangup() {
error: patch failed: src/java/com/telephony/dataker.java:5269
Applying patch src/java/com/telephony/dataker.java with 2 rejects...
Hunk #1 applied cleanly.
Hunk #2 applied cleanly.
Rejected hunk #3.
Rejected hunk #4.
the info above provide details where conflict causing the failure,
just correct some details: that you can used the provided "/home/.../code_target/telephony/.git/rebase-apply/patch " directly but not the original patch.
and let's check how many files have been affected now:
code_target/telephony$ git status
On branch dev-mr1
You are in the middle of an am session.
(fix conflicts and then run "git am --continue")
(use "git am --skip" to skip this patch)
(use "git am --abort" to restore the original branch)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: src/java/com/telephony/dataker.java
Untracked files:
(use "git add <file>..." to include in what will be committed)
src/java/com/telephony/dataker.java.rej
no changes added to commit (use "git add" and/or "git commit -a")
tips let us know that we just in an "git am" conflict now and how to skip/abort this operation,
then list the changes, the ".rej" file provide conflict details
so lets open it and the affected file to resolve it
code_target/telephony$ vi -O src/java/com/telephony/dataker.java src/java/com/telephony/dataker.java.rej
2 files to edit
code_target/telephony$ rm src/java/com/telephony/dataker.java.rej
code_target/telephony$ git add .
then double check
code_target/telephony$ git status
On branch dev-mr1
You are in the middle of an am session.
(fix conflicts and then run "git am --continue")
(use "git am --skip" to skip this patch)
(use "git am --abort" to restore the original branch)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: src/java/com/telephony/dataker.java
all right, now we can end the work
code_target/telephony$ git am --resolved
if there are more files conflict, just follow the same way.
it is said can use "git am -3" to apply 3 way merge to solve this kind of problem too.
$git am ~/patch-set/*.patch
the patches path can be wherever you like.
seems all done now, some branches not needed any more? try below cmd for free
$git branch –d dev
or replace the "d" to "D" to force delete the branch.
remember that you are not able to delete the branch you are on now, check out to branches other than the one you are to delete.
to be continued
here are some ref URL i searched before, which you may also interest in
and this one is much helpful:
https://git-scm.com/book/zh/v1/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E5%86%99%E5%8E%86%E5%8F%B2
others are:
http://www.cnblogs.com/mengdd/p/3585038.html
http://blog.csdn.net/sunnylgz/article/details/7609736
http://www.xuebuyuan.com/1362672.html
http://www.cnblogs.com/rayfloyd/archive/2012/10/18/2717898.html
http://blog.csdn.net/lynnos/article/details/6287135
http://segmentfault.com/q/1010000000332524
http://my.oschina.net/jiangyouxin/blog/108717
http://blog.sina.com.cn/s/blog_68af3f090100rp5r.html
http://blog.csdn.net/hudashi/article/details/7664464
http://blog.chinaunix.net/uid-27714502-id-3479018.html
http://www.pizzhacks.com/bugdrome/2011/10/deal-with-git-am-failures/