Git 시작하기 3

커밋 히스토리 조회하기

저장소의 히스토리를 보고 싶은 경우가 생기는데,

Git에는 히스토리를 조회하는 명령어인 git log가 있다.

git log 저장소의 커밋 히스토리를 시간순으로 보여준다. 즉, 가장 최근 커밋이 가장 먼저 나온다.

1. git log 명령어의 다양한 옵션

  • git log -p -2 : -p는 각 커밋의 diff 명령어 결과를 보여준다. -2는 최근 두 개의 결과만 보여주는 옵션이다.

  • git log --word-diff : 줄 단위 대신 단어 단위로 변경사항을 보여준다.

  • git log --stat : 각 커밋의 통계 정보를 조회할 수 있다. 어떤 파일이 수정됐는지, 얼마나 많은 파일이 변경됐는지, 또 얼마나 많은 줄을 추가하거나 삭제했는지 보여준다.

  • git log --pretty : online - 정보를 한줄씩 보여준다.
  • short, full, fuller - 정보를 조금씩 가감해서 보여준다.
# --pretty=online
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
  • git log --pretty=format : 나만의 포맷으로 결과를 출력하고 싶을 때 사용한다
# --pretty=format : 나만의 포맷으로 결과를 출력하고 싶을 때 사용한다
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 11 months ago : changed the version number
085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code
a11bef0 - Scott Chacon, 11 months ago : first commit

format 형식에서 사용하는 유용한 옵션들

Option Description of Output
%H Commit hash
%h Abbreviated commit hash
%T Tree hash
%t Abbreviated tree hash
%P Parent hashes
%p Abbreviated parent hashes
%an Author name
%ae Author e-mail
%ad Author date (format respects the –date= option)
%ar Author date, relative
%cn Committer name
%ce Committer email
%cd Committer date
%cr Committer date, relative
%s Subject
  • git log --pretty=format: "%h %s" --praph : branch와 merge 히스토리를 보여 주는 아스키 그래프를 출력한다.
$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 ignore errors from SIGCHLD on trap
*  5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
|\
| * 420eac9 Added a method for getting the current branch.
* | 30e367c timeout code and tests
* | 5a09431 add timeout protection to grit
* | e1193f8 support for heads with slashes in them
|/
* d6016bc require time for xmlschema
*  11d191e Merge branch 'defunkt' into local

2. git log에 유용한 옵션들

옵션 설명
-p 각 커밋에 적용된 패치를 보여준다.
--word-diff diff 결과를 단어 단위로 보여준다.
--stat 각 커밋에서 수정된 파일의 통계정보를 보여준다.
--shortstat --stat 명령의 결과 중에서 수정한 파일, 추가된 줄, 삭제된 줄만 보여준다.
--name-only 커밋 정보중에서 수정된 파일의 목록만 보여준다.
--name-status 수정된 파일의 목록을 보여줄 뿐만 아니라 파일을 추가한 것인지,
수정한 것인지, 삭제한 것인지도 보여준다.
--abbrev-commit 40자 짜리 SHA-1 체크섬을 전부 보여주는 것이 아니라 처음 몇 자만 보여준다.
--relative-date 정확한 시간을 보여주는 것이 아니라 2 주전처럼 상대적인 형식으로 보여준다.
--graph 브랜치와 머지 히스토리 정보까지 아스키 그래프로 보여준다.
--pretty 지정한 형식으로 보여준다. 이 옵션에는 oneline, short, full, fuller, format이 있다.
format은 원하는 형식으로 출력하고자 할 때 사용한다.
--oneline --pretty=oneline --abbrev-commit 옵션을 함께 사용한 것과 동일하다.

3. 조회 제한조건

  • --since, --until : 시간을 기준으로 조회하는 옵션
# 지난 2주 동안 만들어진 커밋 조회
$ git log --since=2.weeks
  • --author : 저자를 지정해 검색

  • --grep : 커밋메세지에서 키워드를 검색

  • --all-match : 모두 만족하는 커밋

  • git log -- path1 path2 : 파일경로 검색하는 옵션

옵션 설명
-(n) 최근 n 개의 커밋만 조회한다.
--since, --after 명시한 날짜 이후의 커밋만 검색한다.
--until, --before 명시한 날짜 이전의 커밋만 조회한다.
--author 입력한 저자의 커밋만 보여준다.
--committer 입력한 커미터의 커밋만 보여준다.
# 2008년 10월에 Junio Hamano가 커밋한 히스토리를 조회하는 것
# 그 중에서 테스트 파일을 수정한 커밋 중에서 머지 커밋이 아닌 것들만 조회한다


$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attribute
acd3b9e - Enhance hold_lock_file_for_{update,append}()
f563754 - demonstrate breakage of detached checkout wi
d1a43f2 - reset --hard/read-tree --reset -u: remove un
51a94af - Fix "checkout --track -b newbranch" on detac
b0ad11e - pull: allow "git pull origin $something:$cur