[Git] git rebase 사용하기

2022. 2. 18. 16:00·Git & GitHub/Git

 

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

$ 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 댓글기능완료

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

 

$ 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

 

'Git & GitHub/Git' 카테고리의 다른 글
  • [Git] Git 으로 개인 프로젝트 관리하기
  • [Git] git으로 원격저장소 다루기 (push, pull, clone)
  • [Git] git branch 사용하기
  • [Git] Git Branch의 개념 (fast-forward와 3-way merge)
gakko
gakko
좌충우돌 개발기
  • gakko
    MYVELOP 마이벨롭
    gakko
  • 전체
    오늘
    어제
    • 분류 전체보기 (203)
      • Spring (23)
        • Spring (10)
        • Spring Boot (7)
        • Spring Security (1)
        • Hibernate (4)
      • Test (3)
      • 끄적끄적 (6)
      • 활동 (35)
        • 부스트캠프 (23)
        • 동아리 (3)
        • 컨퍼런스 (3)
        • 글또 (5)
        • 오픈소스 컨트리뷰션 (1)
      • 디자인패턴 (0)
      • Git & GitHub (22)
        • Git (13)
        • Github Actions (1)
        • 오류해결 (5)
        • 기타(마크다운 등) (3)
      • 리눅스 (6)
        • 기초 (6)
        • 리눅스 서버 구축하기 (0)
      • Infra (2)
        • Docker (1)
        • Elastic Search (0)
        • Jenkins (1)
        • AWS (1)
      • MySQL (7)
        • 기초 (6)
        • Real MySQL (1)
      • 후기 (3)
        • Udemy 리뷰 (3)
      • CS (26)
        • 웹 기본지식 (0)
        • 자료구조 (13)
        • 운영체제 OS (12)
        • 데이터베이스 (1)
        • 시스템 프로그래밍 (0)
        • 기타 (0)
      • Tools (1)
        • 이클립스 (1)
        • IntelliJ (0)
      • 프로젝트 (1)
        • 모여모여(부스트캠프) (1)
      • JAVA (32)
        • Maven (6)
        • 오류해결 (11)
        • 자바 클래스&메소드 (1)
        • JSP & Servlet (12)
      • Javascript (5)
        • 기초 (3)
        • React (2)
      • Python (28)
        • 파이썬 함수 (9)
        • 알고리즘 문제풀이 (16)
        • 데이터 사이언스 (2)
        • 웹 크롤링 (1)
      • 단순정보전달글 저장소 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 우진님
  • 공지사항

  • 인기 글

  • 태그

    운영체제
    os
    부스트캠프
    자바
    jsp
    스프링
    스프링부트
    부스트캠프 멤버십
    오류해결
    웹개발
    Spring
    파이썬
    자바스크립트
    GitHub
    부스트캠프 7기
    Python
    알고리즘
    Git
    MySQL
    java
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
gakko
[Git] git rebase 사용하기
상단으로

티스토리툴바