今天看到朋友講了一個 git 指令,想說順便分享一下我自己常用的指令,算是野人獻曝。
config
首先是 ~/.gitconfig,放一些 global 的 git 環境變數,可以在裡面設定自己常用的名稱,以及 alias
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   |  [color] 	ui = auto [user] 	name = YOUR_COMMIT_AUTHOR_NAME 	email = someone@foo.bar [alias] 	lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative 	ld = log --decorate --graph 	lf = log --pretty=fuller --decorate --graph 	s  = status 	amd = commit -a --amend 	cm = commit --amend 	co = commit 	ca = commit -a 	ck = checkout 	ri = rebase -i 	ri5 = rebase -i HEAD~5 	ri10 = rebase -i HEAD~10 	ra = rebase --abort 	rc = rebase --continue 	cp = cherry-pick 	rb = rebase [diff] 	algorithm = patience [core] 	editor = vim 	autocrlf = input 	quotepath = false [push] 	default = simple [url "git@github.com:"] 	insteadOf = https://github.com/ [tig "bind"] 	# use 'Z' to see commit via vimdiff 	generic=Z !sh -c 'git difftool %(commit)^ %(commit)'
 
  | 
 
從這些 command 就可以輕易看出我最常做的事,不外乎就是對 commit 修修剪剪,git rebase -i 真是宅宅的好朋友。
- 在專案裡面新增 
.gitignore 可以叫 git 忽略、不去管理部份檔案,通常用在編譯出來的檔案 
- 如果是一些自己手動的東西需要忽略,不適合放進 .gitignore 給專案的其他人知道,可以寫進 
.git/info/exclude 
patch
只有某幾個修改要給別人的時候,可以用 diff 或是 format-patch 來產生 patch,然後用 apply / am 來打上 patch
format-patch 與 am 是成對的,可以透過 e-mail 來更方便傳遞,但我沒試過。喜歡用 format-patch/am 是因為這樣的 patch 看起來資訊含量比較多。
1 2 3 4 5
   | $ git diff HEAD^..HEAD > patch_for_apply.patch $ git format-patch HEAD~5..HEAD
  $ git apply patch_for_apply.patch $ git am 01-Foobar.patch
   | 
 
手動新增 object
以前還要用 repo sync 奮鬥的時候,常常因為網路或是其他問題,搞到自己的 repository 裡面少了幾個 git object,那時候 kanru 教過這招可以把檔案塞進去
1 2
   |  $ cat foobar | base64 -d | git unpack-objects
 
  | 
 
其他指令
一些偶爾會用到,但是很容易忘記的指令。其實原本是持續更新在我自己的 local wiki 筆記裡面,直接複製貼上到這邊。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
   |  $ git reflog
 
  $ git log -p <MD5>..HEAD --reverse
 
  $ git log --pretty=fuller
 
  $ git diff orig/some_branch..<MD5>
 
 
  $ git diff origin/master..HEAD --name-only     $ git diff origin/master..HEAD --name-status  
 
  $ git -p show-branch
 
  $ git ls-tree <MD5> $ git ls-tree <MD5> path/to/somewhere
 
  $ git show <MD5>:path/to/file
 
  $ git cherry-pick -s -x <MD5>
 
  $ git push <Repo> :heads/branch
 
  $ git push <Repo> Newbranch
 
  $ git archive -o output.zip <MD5> $ git archive -o output.zip <MD5> the/sub/dir
 
  |