2011年10月13日 星期四

Show current branch only

只要顯示目前的 branch $git rev-parse --abbrev-ref HEAD 這要 git 1.6.3 以上版本才有效

雖然$git branch 會 show 出所有 branch 和目前 branch,
但是跟 repo 一起用的時候就有麻煩:
--- 要知道目前每一個 project 的 branch 是...

用以下 command 就可以清楚列出: $repo forall -p -c 'git rev-parse --abbrev-ref HEAD'

ref:
http://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git

2011年6月8日 星期三

diff , ignore white space

如果有人改了 white space 的數量,或是以 white space 來代替 tab, diff 的結果就會一團亂。

這時候可以下
git diff -w
來忽略 white space.

如果是在 difftool 模式 (invoke vimdiff),就要在 vi 裡下 command:
:set diffopt+=iwhite



ref: http://stackoverflow.com/questions/1265410/is-there-a-way-to-configure-vimdiff-to-ignore-all-whitespaces

ref: http://myvicommandhelp.blogspot.com/2011/06/diff-ignore-white-space.html

2011年5月15日 星期日

format-patch & am

在作 rebase, merge 的時後,fail 得太嚴重。所以希望手動來作。
手動作 rebase.. 好確保每個 commit 一一個上去。

可以用git format-patch 'rev-name'產生一堆從 'rev-name' 開始的patch , 一個 commit,一個 patch。

有這些patch檔,就可以用git am 'patch-filename'一個一個上回去。
這樣產生的 patch file 和 am 後的 rev ,會把 commit log 也保留住。

如果一次要全上,可以用: git am *.patch
會自動依照 001, 0002. 003 的順序 patch .

format-patch 可以加 option '-3' 表示 只要該rev 後的三個 patch



ref:
http://gitready.com/intermediate/2009/03/04/pick-out-individual-commits.html

2011年5月5日 星期四

revert/undo rebase

rebase 後發現 fail,要 rollback 怎辦?

列出 reflog,用 git log 找出 rebase 之前的那個 head 是哪一個,然後用 reset --hard 切過去。

git reflog
afa93f0 HEAD@{0}: rebase: tidy up the driver/misc/Makefile, keeps the 9.4 kernel content, just append the new source added by us
7a3b48e HEAD@{1}: checkout: moving from rtk2 to 7a3b48ec1ba6eee2298ee654c0cee1e06f464fed^0
f4fbe68 HEAD@{2}: checkout: moving from rtk to rtk2
6d5d23c HEAD@{3}: checkout: moving from rtk to rtk
6d5d23c HEAD@{4}: checkout: moving from rtk2 to rtk
f4fbe68 HEAD@{5}: checkout: moving from debug_suspend_wakeup to rtk2
4c13583 HEAD@{6}: commit: add more comment : wake_lock_internal & alarm.c
d8cf098 HEAD@{7}: rebase: add a lot message to trace the lock sequence
fe3b73a HEAD@{8}: rebase: use external sd as boot
f4fbe68 HEAD@{9}: checkout: moving from debug_suspend_wakeup to f4fbe680ef41ed3d38ecc1f49a8b2c32a294c178^0
733e20f HEAD@{10}: checkout: moving from rtk2 to debug_suspend_wakeup
f4fbe68 HEAD@{11}: commit: tidy up the driver/misc/Makefile, keeps the 9.4 kernel content, just append the new source added by us
6d5d23c HEAD@{12}: checkout: moving from rtk to rtk2
6d5d23c HEAD@{13}: checkout: moving from imx_r9.4 to rtk
901321c HEAD@{14}: checkout: moving from rtk to imx_r9.4
6d5d23c HEAD@{15}: checkout: moving from 37bace68774d42fc2bea3b2aa2fb8aafb3a6f5a0 to rtk
37bace6 HEAD@{16}: checkout: moving from rtk to 37bace68774d42fc2bea3b2aa2fb8aafb3a6f5a0
6d5d23c HEAD@{17}: checkout: moving from debug_suspend_wakeup to rtk

reflog 會列出每次 operation, HEAD 變動的 log,後面列的是HEAD 的 latest log。

所以可以猜出 HEAD@{11} 大概是 rebase 之前的 head.
... rebase 包含很多動作,rollback, patch them, patch mine... 所以HEAD 變動很多次。

為了確認,可以用 git log 看一下
git log --pretty=oneline HEAD@{11}
f4fbe680ef41ed3d38ecc1f49a8b2c32a294c178 tidy up the driver/misc/Makefile, keeps the 9.4 kernel content, just append the new source added by us
6d5d23c54c2198831014b13d2f85f6f353f9e878 merge from 9.1: dimmer & brake gpio initial setting
911b3e5a7aced34f107715ebe4a1c09f8228c683 switch the sdcard init sequence, to make emmc as mmc0 always
a56e643558b691e5db349f0e630acc8e4e768cd4 Merge branch 'rtk' of git://robot/linux-2.6.35.y into rtk
8269efc25f3339750c7c9d05be88bc3232930f05 fix bug: system data abort due to audio-p driver
698afa31b154d7ea6ac6a3ba8664678e14fbbc97 fix bug: system data abort due to audio-p driver

這樣確認是了。

reset --hard 過去:
git reset --hard HEAD@{11}





ref :

2011年4月28日 星期四

show latest tag name

用 git tags 會 show 出所有的 tag,
如果只要列出這個 branch 最新的,要用:
git describe
有時候要加上 --tags




ref: http://stackoverflow.com/questions/1404796/how-to-get-the-latest-tag-name-in-current-branch-in-git

2011年4月26日 星期二

git clean

有時候在切換 branch 的時候,會出現這樣的 Error:
Untracked working tree file blah would be overwritten by merge
這是說,在這個 branch 裡,沒有加入 git track 的 file,在要 checkout 的 branch 中,被加入 track 了,所以 checkout barnch 時會有問題。

解決方法就是用
git clean把所有沒有 track 的file 刪掉,就可以 checkout 到另一個 branch。



ref: http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull 有時候用 git clean -f 強制刪除所有untrack 的 file.
但是這個命令不會刪除 untrack folder.

要的話,就要用: git clean -f -d 這樣就會把 untracked folder 也刪除。

2011年4月14日 星期四

make an existing git branch track a remote branch

如果當初 就是 checkout remote branch 出來,那個 branch 會自動設定 upstream 是 remote branch.
所以 push, pull 都會對應到 這個 upstream (remote branch)

但是如過自己這個 branch 是 local create,之後卻要設定他的 upstream 是某一個 remote branch 的話..
$ git branch --set-upstream localbranchname remotename/branchname
這樣就可以設定自己這邊的 localbranchname 的 upstream 是 remotename/branchname.


ref : http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch

checkout remote branch , merge then update it.

看一下 remote 的 branch
$git branch -r
korg/master
korg/my9.1
korg/release-1.0
korg/rtk

checkout 出 remote 要merge 的 branch
$git checkout rtk
Branch rtk set up to track remote branch rtk from korg.
Switched to a new branch 'rtk'

這個動作很奇怪,如果原來就有一個 branch 叫 rtk 呢?他怎摩區分我是要 checkout remote 端的 rtk branch, 還是要 checkout 原來的 local rtk branch ?

接著 merge 到我的 code:
$git merge mytest
然後就可以 push 回 remote
$git push
都不用指定remote name...

2011年4月13日 星期三

merge -- just use the target file

merge 得時候 會自動修改 conflict 的 file,並且在 file 裡面 conflict 的地方加上:
>>> HEAD
=====
..

的 mark。

要是merge 時,只是要用 另一個 branch 的 code,並不是真的要 加上 local 跟 branch 的 code。
可以在 merge command 完,再用:
$git checkout --theirs .
$git add -u
$git commit

把 "theirs" 的 code 都checkout 出來 (最後的 '.' 代表全部的 code)
把所有conflict 的 file 都mark 為 resolve (-u)
然後 commit (會自動加上 merge 的 comment

如果是要用原來的 (那 merge 幹麼?) , checkout 時用 option --ours


ref :

delete remote branch

$git push korg :branchname

在 branch name 前面加 ":" 就是刪除

config merge tool

$git config --global merge.tool vimtool
這樣開啟 .gitconfig: 會多了:
[merge]
tool = vimdiff

2011年3月23日 星期三

Reference


internal -- staging area

這一篇說得很清楚 (http://ihower.tw/blog/archives/2591 ).

staging area 是一塊 git 用來紀錄 commit command 要"提交的東西" 的區域)。
所以當你commit 過後,staging area 是空的。
用 git add, 就會把 add 的 file 加到 staging area 裡。

平時用的
$git commit -a a option ,意指..將修改過的檔案add 到 staging area,
所以這樣 commit ,會自動把修改過的 file commit 進去。

要是不加 -a 選項,就要在commit 之前,用 git add 把要 commit 的 file 加到 staging area。
然後 commit 才會 commit這些 file 進去。

要把 staging area 裡的 file 拿出來,用:
$git reset HEAD filename
這樣,那個 file 就會從 staging area 移出來,恢復到還沒被加入 staging area 的狀態 (原來是 untracked 就是 untracked, 是unstaging 就是 unstaging),修改的內容不會消失。

因為有 staging area,所以 diff 分為以下三種:
$git diff
比較 working 跟 staging area

$git diff --cached
拿staging area 的file 跟 HEAD 的內容 (上次commit)比較

$git diff HEAD
比較 working 跟 HEAD 的不同。

2011年3月22日 星期二

merge --squash

git merge 會保留分之的 commit log,所以 merge 之後,主線會留下 分支的 commit log。
所以 merge 完後就可以砍掉分支。不用擔心分支的 commit log 會不見。

但是 git merge 會自動 commit,並且在最後加上一個 "merge" 的 commit log。

如果只是要 merge,不要 commit ,就要用 --squash 這個 option
$ git merge --squash branchname
這樣就 code merge 完就不會作 commit,但是 .. branch 的 commit log 也不會被加到主線來。

加上

2011年3月17日 星期四

diff - show brief

只是要看有哪些檔案不一樣,可以加上 --stat 這個選項
$ git diff 201101270-a2 --stat
這樣就會顯示和 20110127-a2 這個 branch 的差異:
Makefile | 1 +
board/freescale/mx51_bbg/flash_header.S | 24 ++++--
board/freescale/mx51_bbg/lowlevel_init.S | 5 +-
board/freescale/mx51_bbg/mx51_bbg.c | 118 +++++++++++++++++++++++++++---
include/configs/mx51_bbg_android.h | 8 ++-
5 files changed, 132 insertions(+), 24 deletions(-)

2011年3月16日 星期三

git remote -- show branch

列出所有remote target 的 branch
$git branch -r
以有加入charles-robot, origin 兩個 remote target 的 repo 為例,會列出:
charles-robot/201101270-a2
charles-robot/20110131-test-a3
charles-robot/20110132-back-to-a1
charles-robot/20110316-a3-emmc-boot
charles-robot/cv7000-A2
charles-robot/imx_r9.1
charles-robot/master
origin/GPL-Cleanup
origin/HEAD -> origin/master
origin/i.MX31
origin/lwmon5
origin/master
origin/next
origin/origin
origin/tx25
origin/u-boot-2009.11.y

git tag

$ git tag A3-factory 7735517
然後用
$ git tag
就會列出所有的 tag

git log

每個 commit 用一行來顯示
$git log --pretty=oneline
會是:
77355173c615b063d06061b698755d56676671e2 loyal 3G, tony break detect,AudioProcessor,Auron pmic_adc
20caf82ec8774c8a8635bf9679dc559f57fa6f30 commit previous patch
1fd44d644180a3a099c2e899aa1135bc80b00104 commit previous patch
8946a0b7e83f818430417a64eaf8bebdcf3ef64a commit pmic_adc.c
de713684abf9725717b070836333f0cc023ad21f 20110121
72443f571e57059fc44d719e4aedbe83c9b78723 20110121_01
...