DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on • Updated on

[git] history reading

👍 Basic

just log

git log --name-status
git log -p -S xxx # see commits that include changes with xxx
git log -p -S xxx --pickaxe-all # with all files


git log --pretty=format:"%p => %h %s %an"
# 010ae6aba4 => 0f96ad607d :shirt: rubocop
# 9bc53da9c3 => 010ae6aba4 add facker lorem
# 0fd91176dc => 9bc53da9c3 remove unused
# f44be56a4b => 0fd91176dc ControllerSpec

git log --pretty=format:"%ar [%an] %s %h"
# 47 minutes ago [n350071] :shirt: rubocop  0f96ad607d
# 53 minutes ago [n350071] add facker lorem 010ae6aba4
# 2 days ago [n350071] remove unused 9bc53da9c3
# 5 days ago [n350071] ControllerSpec 0fd91176dc
option 内容
%ad Authorの日付 ( --date= オプションの形式 )
%ar Authorの相対日付
%h commitのハッシュ(短縮形)
%p 親commitのハッシュ(短縮形)
%an Authorの名前
%s コミットメッセージ
--date= 内容 見た目
iso ISO8601 2019-05-23 18:34:42 +0900
rfc FRC2822 Thu, 23 May 2019 18:34:42 +0900
raw UNIX時間 1558400483 +0900
local 端末のタイムゾーン Thu May 23 18:34:42 2019
relative 相対時間 4 weeks ago

なお、Authorは作業を行った人, Commiterはその作業を適用した人という意味らしいです。%adを%cdに変更すると対象がAuthorからComitterに変わります。

Filter

git log -- path/to/*.rb
git log --grep [commit-message]
git log --since="4 days ago" --until="2015/01/22"
git log --author='name'

# list commits that modified 'config/routes.rb'
git log --oneline  --diff-filter=M -- config/routes.rb

📚 reference: --diff-filter

This Branch history

#  --first-parent: Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch
git log -n 3 --oneline --first-parent --no-merges
git log -n 3 --oneline --graph

Find the commit which removes the file

$ git log --oneline | cut -d' ' -f1 | xargs git show --name-only | grep -B100 path/to/file.rb

🦄Advanced

👍 Search/Find the log

Search all remote branches by the specific word

# git grep 'search-string' $(git ls-remote . 'refs/remotes/*' | cut -f 2)
git grep 'search-string' $(git ls-remote . 'refs/remotes/*' | grep -v HEAD | cut -f 2)
git grep 'search-string' $(git rev-list --all)

Find the commit that the branch made from

# list the checkout reflog informations, the last one is the base commit of the branch
git reflog |grep checkout |grep [branch-name]

Find the commit that the branch made from 2

Faster, but not accurate. This actually shows how much the two branches merged?

git show-branch branchA branchB

#  * [feature/KX-155^2^^2^2] Fix something
#  -- [develop~23] Merge branch 'feature/jsx-1902' into 'develop'

Find the merged branch from the commit message

(git コミットメッセージからマージ元ブランチを特定する(原始的バージョン))

# 1. --more= の数字を適当に切り替える
git show-branch --more=400 master | grep 'hoge'

# 2. [master~70^2~3] hoge というような行がみつかるので、、その数字でもういちど実行してスクロールすると、見つけられる
git show-branch --more=400 master

# [master~70] Merge pull request #306 from organize/branch-name
# [master~70^2] buzz
# [master~70^2^] fizz
# [master~70^2~2] fuga
# [master~70^2~3] hoge

Search the Commits by the specific word

git blame TargetFile | grep TargetWord | cut -d" " -f1 | xargs git show
git blame TargetFile | peco | cut -d" " -f1 | xargs git show

👍 log history reading

make sure which lines modified in the specific Tickets(commit messages)

# Basic
git diff --name-only aj194z^ aj194z         # see the diff between one commit
git diff aj194z^ aj194z -- config/routes.rb # see the diff of the commit

# list the commits of the specifig tickets
git log --first-parent --oneline | egrep TICKET-18[1234]

# Find the files named 'controller' which is modified in the specific tickets in this branch history
git log --first-parent --oneline | egrep TICKET-18[1234] | cut -d' ' -f1 | xargs -I{} git diff --name-only  {}^ {} | grep controller | grep -v spec
git log --first-parent --oneline | egrep TICKET-18[1234] | cut -d' ' -f1 | xargs -I{} git diff  {}^ {} -- config/routes.rb | grep +

$ git log --first-parent --oneline | grep TICKET-18[1234] | cut -d' ' -f1 | xargs -I{} git diff --name-only {}^ {} | grep view | xargs -I{} echo '{}' | sort | uniq

🔗 Parent Note

Top comments (0)