Git & GitHub/Git

[Git] git rebase 사용하기

gakko 2022. 2. 18. 16:00

 

아래와 같은 로그가 있다고 해보자.

$ git log
commit 4b38800ffd950b6d9ee7d9e498cb813c4bb6196c (HEAD -> master)
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:45:52 2022 +0900

    댓글기능완료

commit fbf1cb943bc5f651af389935089fd34d0ff2927f
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:45:26 2022 +0900

    댓글기능2

commit 90dc548ae0bfaa3328fca93bad686fa37045f23d
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:44:34 2022 +0900

    댓글기능1

commit 34e07046d9e9e3d11278effcd9201792a7bab652
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:44:01 2022 +0900

    홈페이지

 

 

댓글기능1, 댓글기능2, 댓글기능완료라는 로그를 깔끔하게 정리하고 싶다.

이 때 사용하는 기능이 git rebase다. 명령어를 사용해보자.

$ git rebase -i HEAD~3

HEAD에서부터 3개의 로그를 합칠 것이기 때문에  HEAD~3 이라고 해주면 된다. 그러면 vi 에디터가 열리게 된다.

pick 90dc548 댓글기능1
pick fbf1cb9 댓글기능2
pick 4b38800 댓글기능완료

# Rebase 34e0704..4b38800 onto 34e0704 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's

 

 

여기서 Squash라는 개념이 필요하다. Squash란 찌그러트리고, 압축하는 걸 의미한다. Squash는 방향이 특히 방향이 중요하다. 방향은 현재에서 과거로 찌그러트려야 한다.

 

아래 내용을 참고하여 rebase의 기능을 실습을 해보자.

- 원래 편집기 내용

pick 90dc548 댓글기능1
pick fbf1cb9 댓글기능2
pick 4b38800 댓글기능완료

 

- 편집기를 사용하는 법

  1.  i  누르면 편집 가능
  2.  esc  누르고  :wq  누르면 종료됨

 

 

 

1. Drop 사용하기 (로그 지우기)


pick 90dc548 댓글기능1
d fbf1cb9 댓글기능2
pick 4b38800 댓글기능완료

위 내용처럼 편집기에 있는 내용 중 하나의 pickd로 바꾸고 편집기를 종료해보겠다.

 

$ git log
commit 4b38800ffd950b6d9ee7d9e498cb813c4bb6196c (HEAD -> master)
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:45:52 2022 +0900

    댓글기능완료

commit 90dc548ae0bfaa3328fca93bad686fa37045f23d
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:44:34 2022 +0900

    댓글기능1

commit 34e07046d9e9e3d11278effcd9201792a7bab652
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:44:01 2022 +0900

    홈페이지

 

 

보너스: 복구하는 방법

$ git reflog
6985833 (HEAD -> master) HEAD@{0}: rebase (finish): returning to refs/heads/master
6985833 (HEAD -> master) HEAD@{1}: rebase (pick): 댓글기능완료
90dc548 HEAD@{2}: rebase (start): checkout HEAD~3
4b38800 HEAD@{3}: commit: 댓글기능완료
fbf1cb9 HEAD@{4}: commit: 댓글기능2
90dc548 HEAD@{5}: commit: 댓글기능1
34e0704 HEAD@{6}: commit (initial): 홈페이지


$ git reset --hard 4b388
HEAD is now at 4b38800 댓글기능완료

git reflog를 통해 해쉬코드를 참고하고, 댓글기능완료 쪽으로 돌아가면 복구가 가능하다.

 

 

 

 

2. Reword 사용하기 (로그 수정하기)


pick 90dc548 댓글기능1
r fbf1cb9 댓글기능2
pick 4b38800 댓글기능완료

이번엔 Reword를 의미하는 r을 적은 후 편집기를 종료해보겠다.그러면 아래와 같은 편집기가 다시 뜨게 된다.

여기서 댓글기능2라고 적혀있는 부분을 수정하면 헤더영역의 커밋메시지를 바꿀 수 있다.

내용을 수정한 후 편집기를 종료하면 된다.

댓글기능2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Feb 12 14:45:26 2022 +0900
#
# interactive rebase in progress; onto 34e0704
# Last commands done (2 commands done):
#    pick 90dc548 댓글기능1
#    reword fbf1cb9 댓글기능2
# Next command to do (1 remaining command):
#    pick 4b38800 댓글기능완료
# You are currently editing a commit while rebasing branch 'master' on '34e0704'.
#
# Changes to be committed:
#       new file:   "\353\214\223\352\270\200\352\270\260\353\212\2452.txt"
#

 

 

 

 

3. Squash 사용하기 (로그 합치기)


- 수정한 편집기 내용

pick 90dc548 댓글기능1
s fbf1cb9 댓글기능2
s 4b38800 댓글기능완료

이렇게 적으면 s가 적힌 부분을 pick으로 찌그러트리게 된다. 현재부분에서 과거쪽으로 찌그러트려야하기 때문에 맨 위의 부분을 pick으로해주고, 나머지 아래부분은 s로 해주면 된다. 

또 아래와 같은 편집기가 뜬다.

# This is a combination of 3 commits.
# This is the 1st commit message:

댓글기능1

# This is the commit message #2:

댓글기능2_수정

# This is the commit message #3:

댓글기능완료

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Feb 12 14:44:34 2022 +0900
#
# interactive rebase in progress; onto 34e0704
# Last commands done (3 commands done):

 

 

이제 편집기를 수정해보자. 아래와 같은 원본에 필요없는 부분을 지워 수정본처럼 만들어주자.

- 원본

# This is the 1st commit message:

댓글기능1

# This is the commit message #2:

댓글기능2_수정

# This is the commit message #3:

댓글기능완료

 

- 수정본

# This is the 1st commit message:


# This is the commit message #2:


# This is the commit message #3:

댓글기능완료

 

이제 git log를 통해 확인해보면 로그가 굉장히 깔끔해진 것을 확인할 수 있다.

$ git log
commit cde93aaea06a14cd8b66a1ddd87ef5404642af87 (HEAD -> master)
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:44:34 2022 +0900

    댓글기능완료

commit 34e07046d9e9e3d11278effcd9201792a7bab652
Author: BigBell <pythonstrup@gmail.com>
Date:   Sat Feb 12 14:44:01 2022 +0900

    홈페이지

 

 

 

 

git-Rebase에 대한 자세한 설명 참고

깃 홈페이지: https://git-scm.com/book/ko/v2/Git-%EB%B8%8C%EB%9E%9C%EC%B9%98-Rebase-%ED%95%98%EA%B8%B0

 

Git - Rebase 하기

Git에서 한 브랜치에서 다른 브랜치로 합치는 방법으로는 두 가지가 있다. 하나는 Merge 이고 다른 하나는 Rebase 다. 이 절에서는 Rebase가 무엇인지, 어떻게 사용하는지, 좋은 점은 뭐고, 어떤 상황에

git-scm.com