본문으로 바로가기

[GIT] Commit 합치기 - rebase

category Helloworld!/GIT 2016. 6. 23. 15:58
다음과 같이 현재까지 git commit log 상태가 총 5개가 있다고 가정하자.
modified file - 3
modified file - 2
modified file - 1
added file
init project

이 중에 modified file 1, 2, 3 이라고 입력한 commit 들을 "modified file" 이라 하고 한개의 commit 으로 합치고자 한다.
우선 현재 commit 히스토리를 볼때는 다음과 같이 git log를 사용하면 된다.
$git log
또는, 좀 더 이쁘게 한줄로 보고 싶다면 다음 명령어를 사용하면 된다.
$git log --pretty=oneline
69678ae modified file - 3
1299021 modified file - 2
26bf369e modified file - 1
1b89eaf added file
28889ea0 init project 

commit 내용이 너무 많으면 Enter로 다음 commit 내용을 확인 할 수 있으며,
그만보고 싶을 때는 q 를 누르면된다.

commit 을 합치기 위해서는 rebase라는 명령어를 사용 후 자동으로 실행되는 편집기에서 commit 히스토리를 수정한다.
우선, 최상단에 있는 3개의 commit (modified file 1, 2, 3) 을 하나의 commit 으로 merge 시키는 것이므로 다음과같이 명령어를 입력한다
$git rebase -i HEAD~3
이처럼 HEAD~뒤에 숫자만큼 최상단으로 부터 몇번째 commit 을 합치겠다고 입력을 하면 된다.
그럼 다음과 같이 편집기가 켜진다. 편집기의 내용은 다음과 같다.
pick 69678ae modified file - 3
pick 1299021 modified file - 2
pick 26bf369e modified file - 1
# Rebase 0fab167..f329ec3 onto 0fab167
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
여기서 제일 상단에 있는 commit 빼고 pick 부분을 squash 로 변경을 한다.
pick 69678ae modified file - 3
squash 1299021 modified file - 2
squash 26bf369e modified file - 1
# Rebase 0fab167..f329ec3 onto 0fab167
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
변경이 다됐으면 저장을 한다.
입력 내용은 수정은 i 또는 Insert, 저장은 w 종료는 q 이다. (vi 와 같다)
저장하고 빠져나오면 새로은 편집창이 출력된다.
# This is a combination of 3 commits.
# The first commit's message is:
modified file - 3

# This is the 2nd commit message:
modified file - 2

# This is the 3rd commit message:
modified file - 1

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   modified:   hello.html
#
여기서 2 commit message 1, 2를 삭제하고 3부분에 수정하고자 하는 commit 내용으로 수정을 해준다
# This is a combination of 3 commits.
# The first commit's message is:
modified file

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   modified:   hello.html
#
그리고 다시 저장해서 나오면 정상적으로 rebase되었음을 확인할 수 있다. 
정상적으로 됐을 때 내용은 다음과 같다.
modified : modified file
 Date: Tue Jun 21 18:47:08 2016 +0900
 8 files changed, 100 insertions(+), 150 deletions(-)
 delete mode 100644 file1.php
 rewrite file.php (95%)
 rewrite file1.php (81%)
Successfully rebased and updated refs/heads/feature/testFile.

참고사이트 

http://mohwaproject.tistory.com/entry/Git-rebase-%EB%AA%85%EB%A0%B9-%ED%85%8C%EC%8A%A4%ED%8A%B8

http://jybaek.tistory.com/378