2018年7月18日 星期三

checkout svn repo and convert to git

用 git svn 來checkout svn repo,會convert 成 git repo,當然 svn 的 commit log 都會保存下來。
git svn clone https://subversion.my.com/svn/mysvnproj mygitproj

然後以後要從 svn update,對應的是:
git svn fetch


fetch 完,自己工作的 branch 要 update,還要下一次:
git svn rebase

2017年2月16日 星期四

2014年11月17日 星期一

hard reset to --- 放棄掉.....

ref: http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit

如果是自己的 git,沒有跟人家 sync..(或是還沒sync).
那就可以用 ...
git reset --hard ed2b7f3501f6
這樣,用 git log 看,最後會在 commit ed2b7f3501f6 的地方。
這個 commit 後面的都不見了,,

2014年5月6日 星期二

commit and checkpatch.pl and hooks

checkpatch.pl 是一個 perl script。
越來檢查有沒有符合該專案的 coding style.

所以可以叫 git commit 時,自動 run 這個 script。
防止不合style 的 code 被 commit 進 source code

這個動作通常是放在 git 的 hooks 裡:
 .git/hooks/prepare-commit-msg

這個 hooks 是一個 shell script,在 git commit 時,書入完 commit 時會被執行。

所以可以這樣寫:
#!/bin/sh
if test -r "checkpatch.pl"; then
  git diff --cached | ./checkpatch.pl --no-signoff --no-tree -
fi
檢查project 目錄是不是有 checkpatch.pl 好決定是不是要 執行

這樣改完後,如果 checkpatch.pl 結果是 warning 或 error, commit 就不會進行 commit

這個 script 也可以吃參數,$1 就是 commit comment。
所以可以利用 commit comment 來控制他的動作。
例如:
 ! grep -qs "\[NO_STYLE_CHECK\]" "$1" || exit 0
就可以在 commit 時,加入 "[NO_STYLE_CHECK]" bypass 掉 checkpatch:
$ git commit -am "[NO_STYLE_CHECK] try commit dirty codes"


ref:
  • http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/
  • https://answers.atlassian.com/questions/169399/sourcetree-and-git-prepare-commit-msg
  • http://www.fluff.org/ben/linux/githooks/pre-commit

#!/bin/sh
#
# pre-commit hook to run check-patch on the output and stop any commits
# that do not pass. Note, only for git-commit, and not for any of the
# other scenarios
#
# Copyright 2010 Ben Dooks, 

if git rev-parse --verify HEAD 2>/dev/null >/dev/null
then
 against=HEAD
else
 # Initial commit: diff against an empty tree object
 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

git diff --cached $against -- | ./scripts/checkpatch.pl --no-signoff -

2014年2月20日 星期四

revert 某個 commit..

有時候想把某個 commit 消除。
要作 :
$ git revert COMMIT-SHA

然後就會出現 comment editor 要你confirm,

這個動作不會把那個 commit 從 commit log 中拿掉,
而是產生一個相反動作的 commit, 把那個 commit 消除。

所以 revert 完是可以 push 到 remote server 的。



所以要把最新的 3 個 commit 都 revert 的話:
git log
commit A
 ...
commit B
 ...
commit C
..
就要反過來:
 git revert A
 git revert B
 git revert C

2013年11月5日 星期二

看某一個 commit 的 diff

只要看某一個 commit 的 diff,就用
$ git show 2e6f1155dd56512d5f848f4b7e6198c38aa256f2

2013年8月12日 星期一

git with https source.. SSL error

最近越來越多 git source 採用 https,

有兩個麻煩的地方:
  • username, password
  • SSL certification
usename, password 就用 ~/.netrc 就可以,
把 machine, login, password 寫進去:
machine my.git.com
login checko
password mygitpassword
.. 對,是明文 ..

site SSL certification fail 就會出現...
Initialized empty Git repository in /xxxx/xxxx/.git/
Password: 
error: server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt 
CRLfile: none while accessing https://xxxx@xxxx.xxxx/xxxx/xxxx.git/info/refs

fatal: HTTP request failed
這時候就加上這個還就可以:
export GIT_SSL_NO_VERIFY=1


2013年7月22日 星期一

git difftool readonly

某一天突然 run git difftool 變成以 readonly 開啟。
把另一個系統的 git (1.7.5) copy 到 local 的 ~/bin/ 下,結果一樣。

所以只好用 apt-get purge git 和 apt-get autoremove 把 git 清掉。
然後 再 apt-get install git

把 git 裝回來(結果是 1.7.1) 。
就 OK 了。

結果 android repo 需要 1.7.2...

設法 安裝 1.7.2 以上...(結果裝到 1.8.3.4)
git difftool 又是 read-only...

後來發現vim 打開雖然是 RO,但是寫入時加上強制 '!' 符號,還是可以寫入


其實只要在 .vimrc 下,增加一個option:
:set noro
就可以了..

2012年8月19日 星期日

看branch 的 remote track

看目前所有branch 的 track 狀態: $ git branch -vv 其中,有track 的,會顯示在 [ ] 中。

checkout & track remote branch

一般只要沒有混淆的地方(重複名稱,已經checkout.. etc),直接 checkou remote branch 會自動設定 track。
但是有混淆的時候,就要手動了。

要 checkout remote branch ,並且設好 track (push, pull target),可以用: $ git checkout --track -b merge10.4 aosp/merge10.4

2012年6月25日 星期一

new project, clone from remote repo with specific name

如果要create 一個 new git,但是要從 remote 拿code,又要指定 remote 的name
(例如 android framework project 有自己 create 的新 project folder,就會需要這樣).

用 git clone 也可以,但是用這樣好像比較合理:

git init git remote add myserver git://..... git pull myserver


ref: http://blog.opsb.co.uk/instant-remote-git-repository-using-ssh

2012年5月20日 星期日

rebase

rebase 和 merge 一樣,都是要整合另一個 branch 的內容,
rebase 有一點 "管理 commit " 的意味。


如果把git 的每一次 commit 都當作一個 patch。

merge 會依照每個 patch 的時間,依序把merge 的兩個 branch 的 patch 一一上到source。
所以merge 完,兩個 branch 的 commit 有可能會穿插.


rebase 的話,就一律先上另一個branch 的 commit ,然後再上自己的 commit。

這在系統廠的工程師比較合用。

因為系統廠都是拿 vendor 的 bsp 來改。
所以當 vendor 有新版bsp 時,如果作 merge,
自己的修改和 vendor 的修改就會混在一起,
merge 時的 conflict 也會比較多(?),因為有可能是 vendor 的commit 的 conflict,所以會比較不好 resolve。

如果是用 rebase 的話,至少第一步,vendor 的 new bsp 的 commit一定都上得去。
在自己的commit 才會發生 conflict,
這樣因為 conflict 是在自己的 修改,會比較好 resolve。

2012年3月19日 星期一

use ssh login with username

語法就是: $ git clone ssh://charles@servername/~/mysourcecode .. 可以用 ~ 就不用寫一堆 /home/charles/...

另外也可以指定 port,一樣,寫 sername:port

ref:
http://stackoverflow.com/questions/2433049/git-clone-using-ssh-cant-find-repository

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 也刪除。