Skip to content

Instantly share code, notes, and snippets.

@browny
Last active October 13, 2016 02:50
Show Gist options
  • Save browny/7078500 to your computer and use it in GitHub Desktop.
Save browny/7078500 to your computer and use it in GitHub Desktop.

Git Diff

git diff                 // Look diff from previous commited version

git diff > temp.patch    // Output diff to patch file
git apply temp.patch     // Apply patch 'pk_xxx'

git reset --hard 6a137   // Force reset to version '6a137' will lost new commit
git format-patch HEAD^^^ // Patch to nth(^) previos version from HEAD, output multiple files

git diff --u=10          // Show 10 lines context of diff

Git Branch

git checkout .				// Discard local changes of all files
git checkout <somefile>		// Discard local changes of some file

git branch -av    // detail branch info

git branch -u origin/<remote-branch-name> // setup tracking remote branch

// Stash
git stash save "work in progress for foo feature"
git stash list
git stash apply stash@{1}

// List which branch (remote & local) contains some commit
git branch -a --contains <commit_id>

// Merge multiple commits to one commit
// (ex: ABCDE), you want to ABE'(E'=CDE)
git rebase -i HEAD~2	// rebase to C
s e97a17b E
s asd314f D
p asd314f C

// 如何分辨 local branch 追蹤哪一個 remote branch
git remote show origin

// Create remote branch
git checkout -b your_branch
git push -u origin your_branch

// Delete remote branch
git push origin :your_branch

// 遠端分支被刪除要用 -p 更新 branch 資訊
git fetch -p

Git Log

git log --pretty=short           // 簡短的log
git log --oneline                // 超短的log
git log --author="Jon"           // Show Jon's commits
git log /drivers/video           // 只看drivers/video資料夾下更動的log
git log ./                       // 只看當前資料夾下更動的log

git blame sha1_file.c            // 查看sha1_file.c 的修改
git blame -L 160,+10 sha1_file.c // 查看sha1_file.c 從第160行開始往下10行,是誰修改的

git log --grep=word              // 搜尋 commit message 包含 'word' 的 commit
git log -Sword                   // 搜尋 commit 修改內容包含 'word' 的 commit

git log -5 --name-only           // 查最近 5 筆 commit 有修改哪些檔案

// 搜尋哪一筆 commit 在 SurfaceFlinger.cpp 中曾經修改過 surface_flinger_cblk_t 這個關鍵字
git log -S"surface_flinger_cblk_t" -p services/surfaceflinger/SurfaceFlinger.cpp

// 樹狀圖看log merge path
git log --graph --pretty=format:'%Cred%h%Creset:%s  %Cgreen(%cr)%Creset %an' --abbrev-commit --date=relative

Git Patch

git format-patch -1 7ef30fa // 取出單一commit為patch

git am                      // apply format-patch patch
git apply                   // apply diff > patch

git apply --reject          // 會把可以上的部份上去,衝突部份他會寫到 xxx.c.rej 檔案中

git reset                   // 把 changes to be commited 的部份拉到 changes but not commit
git ch .                    // 把 changes but not commit 變成 no changes

// Single file checkout
git checkout v1.2.3 -- filename         // tag v1.2.3
git checkout stable -- filename         // stable branch
git checkout origin/master -- filename  // upstream master
git checkout HEAD -- filename           // the version from the most recent commit
git checkout HEAD^ -- filename          // the version before the most recent commit

// git revert
git revert 2cded15f64b2eb6f8acc   // revert a previous commit, and the revert action will become a new commit

// git bisect
git bisect start
git bisect bad 2ac4169f24e91e10d683cbf4676d
git bisect good a49b61dbb440fa48f78adece5e5980

// cherry-pick partial commit
git cherry-pick -n <commit>   # get your patch, but don't commit (-n = --no-commit)
git reset                     # unstage the changes from the cherry-picked commit
git add -p                    # make all your choices (add the changes you do want)
git commit                    # make the commit!

// Keep commit and remove some content
git checkout -b test
git reset --hard HEAD^
git cherry-pick -n master
git reset .
git add -p
git diff master > patch
git checkout -f master
git apply patch
git cm --amend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment