Git 재활 훈련 11일차 - git interactive rebase

0

Git

목록 보기
11/14

Interactive rebase

interactive rebase로 git history를 정리할 수 있다. 과거에 올린 commit msg도 바꿀 수 있고, 제거할 수도 있다. 이는 매우 유용한 기능으로 실제 개발에 있어서 많이 사용된다.

Rewriting History

개발을 하다보면 commit을 재작성하고 싶거나, 삭제, 수정, 심지어는 순서를 바꾸고 싶을 때가 있다. 이러한 모든 것들을 git rebase를 통해서 할 수 있다. 가령, 이전 commit에 bug가 있었는데, bug가 있는 내용들을 수정해서 올리고 싶을 때나, msg 내용들을 바꾸고 싶을 때나 git rebase를 통해서 commit들을 수정할 수 있다. 이는 내 commit들을 다른 사람들에게 공유하기 전에 하는 하나의 정리라고 보면 된다.

git rebase -i HEAD~4

현재의 branch에서 HEAD 기준으로 4개의 commit 이전에 되돌아가겠다는 것이다. -i가 바로 interactive 모드이다.

실습을 위해 해당 github repo에 접속하고 clone하기로 하자.

https://github.com/Colt/interactive-rebase-demo

해당 project를 clone해보도록 하자.

git clone https://github.com/Colt/interactive-rebase-demo
cd interactive-rebase-demo

git log --oneline 
3b23c17 (HEAD -> main, origin/main, origin/HEAD) Create README.md
2029e20 my cat made this commit
655204d fix another navbar typo
2a45e71 fix navbar typos
4ff2290 add top navbar
6e39a76 whoops forgot to add bootstrap js script
240827f add bootstrap
519aab6 add basic HTML boilerplate
cbee26b I added project files
0e19c7a initial commit

commit들을 보면 지저분한 commit들이 몇몇개 있다. 가령 fix navbar typos, fix another navbar typo, my cat made this commit와 같은 것들이 있다. 이러한 commit들을 정리하고, 하나로 합치도록 해보자.

먼저 my-feat branch를 만들고, 거기서 작업을 하도록 하자.

git switch -c my-feat
git rebase -i HEAD~9

text editor가 실행되는데, 다음과 같이 써있을 것이다.

pick cbee26b I added project files
pick 519aab6 add basic HTML boilerplate
pick 240827f add bootstrap
pick 6e39a76 whoops forgot to add bootstrap js script
pick 4ff2290 add top navbar
pick 2a45e71 fix navbar typos
pick 655204d fix another navbar typo
pick 2029e20 my cat made this commit
pick 3b23c17 Create README.md

# Rebase 0e19c7a..3b23c17 onto 2029e20 (9 commands)

헷갈리지 말아야할 것이 위부터 예전 것이고 아래가 최신 것이다.

잘보면 해당 commit에 pick이라고 표현이 되어 있는데, 이러한 command들을 통해서 commit들을 선택, 수정, 삭제할 수 있다.
1. pick: commit을 사용
2. reword: commit을 사용, commit msg를 수정
3. edit: commit을 사용, commit content를 수정
4. fixup: commit content들을 사용하지만, commit을 이전 commit과 합치거나 commit msg를 버린다.
5. drop: commit을 버린다.

우선 reword만 보도록 하자.

I added project files commit msg를 수정하기 위해서 pickreword로 수정하도록 하자.

rework cbee26b I added project files
pick 519aab6 add basic HTML boilerplate
...
pick 3b23c17 Create README.md

:wq를 눌러서 저장하면 해당 commit msg를 수정할 수 있는 editor가 나온다. 해당 editor를 통해서 정보를 수정하기로 하자.

add project files

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

:wq를 눌러서 commit msg의 변동을 저장하도록 하자.

git log로 잘 적용되었는 지 확인해보도록 하자.

git log --oneline 
f26915f (HEAD -> my-feat) Create README.md
f91b097 my cat made this commit
f748fa9 fix another navbar typo
25aaed4 fix navbar typos
37ea494 add top navbar
a171ee4 whoops forgot to add bootstrap js script
549bf8a add bootstrap
fa9beb5 add basic HTML boilerplate
c5fcdc9 add project files
0e19c7a initial commit

add project files로 수정된 것을 볼 수 있다. 재미난 점은 commit hash값이 바뀌었다는 점이다.

원본은 cbee26b이지만, 바뀐 commit hash는 c5fcdc9이다.

만약, 가장 최근의 commit에 대해서 바꾸고 싶다면 다음과 같이 할 수 있다.

git commit --amend

다음과 같은 화면이 나올 것이다.

Create README.md

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#

Createcreate로 수정하고 저장하도록 하자.

아래와 같이 잘 수정된 것을 볼 수 있다.

git log --oneline 
a73415d (HEAD -> my-feat) create README.md

commit 합치기

이번에는 commit을 합쳐보도록 하자.

git log --oneline 
f26915f (HEAD -> my-feat) Create README.md
...
a171ee4 whoops forgot to add bootstrap js script
549bf8a add bootstrap
...

a171ee4 commit은 549bf8a commit이 발생한 후로 잊어버린 js script code를 넣은 commit이다. 코드를 확인하면 다음과 같다.

  • a171ee4 commit
<body>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
        crossorigin="anonymous"></script>
</body>

</html>

git rebase를 통해서 a171ee4549bf8a를 합쳐보도록 하자.

git rebase -i HEAD~9

pick c5fcdc9 add project files
pick fa9beb5 add basic HTML boilerplate
pick 549bf8a add bootstrap
pick a171ee4 whoops forgot to add bootstrap js script
pick 37ea494 add top navbar
pick 25aaed4 fix navbar typos
pick f748fa9 fix another navbar typo
pick f91b097 my cat made this commit
pick f26915f Create README.md

# Rebase 0e19c7a..f26915f onto f91b097 (9 commands)

해당 명령어에서 a171ee4 commit에 pick이 아니라 fixup을 써주면 된다. fixup은 해당 commit을 이전 commit과 합쳐지도록 하고, commit안에 있는 commit content들은 남겨놓는 특징을 가진다.

git rebase -i HEAD~9

pick c5fcdc9 add project files
pick fa9beb5 add basic HTML boilerplate
pick 549bf8a add bootstrap
fixup a171ee4 whoops forgot to add bootstrap js script
pick 37ea494 add top navbar
pick 25aaed4 fix navbar typos
pick f748fa9 fix another navbar typo
pick f91b097 my cat made this commit
pick f26915f Create README.md

# Rebase 0e19c7a..f26915f onto f91b097 (9 commands)

:wq를 누르면 Successfully rebased and updated refs/heads/my-feat.라는 로그가 발생할 것이다.

git log를 통해서 commit이 정말 합쳐졌는 지 확인해보도록 하자.

git log --oneline 
21a409c (HEAD -> my-feat) Create README.md
333b72d my cat made this commit
7379d1b fix another navbar typo
766bb25 fix navbar typos
0ede780 add top navbar
a4b12ad add bootstrap
fa9beb5 add basic HTML boilerplate
c5fcdc9 add project files
0e19c7a initial commit

a171ee4 commit이 사라진 것을 볼 수 있다. 다음으로 이전 a171ee4 commit의 content가 정말로 남아있는 지 확인해보도록 하자.

  • index.html
...
<body>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
        crossorigin="anonymous"></script>
</body>

</html>

남아있는 것을 확인할 수 있다.

추가로 아래의 log에서 navbar 부분 commit을 하나로 만들어보자.

git log --oneline 
...
7379d1b fix another navbar typo
766bb25 fix navbar typos
0ede780 add top navbar
...

7379d1b766bb250ede780 commit 하나로 바꾸는 것이다.

git -i rebase HEAD~8

pick c5fcdc9 add project files
pick fa9beb5 add basic HTML boilerplate
pick a4b12ad add bootstrap
pick 0ede780 add top navbar
fixup 766bb25 fix navbar typos
fixup 7379d1b fix another navbar typo
pick 333b72d my cat made this commit
pick 21a409c Create README.md

766bb257379d1b commit을 fixup을 통해서 하나로 만드는 것이다.

완료된 이후의 commit들을 확인해보도록 하자.

git log --oneline
4025cc8 (HEAD -> my-feat) Create README.md
819a751 my cat made this commit
721dfe7 add top navbar
a4b12ad add bootstrap
fa9beb5 add basic HTML boilerplate
c5fcdc9 add project files
0e19c7a initial commit

fix navbar typos, fix another navbar typo commit들이 사라졌지만, commit content들인 type 수정 code는 그대로 남아있는 것을 볼 수 있을 것이다.

commit 삭제하기

drop은 메세지 제거를 넘어서 commit의 contetn 자체를 지운다.

git log --oneline
4025cc8 (HEAD -> my-feat) Create README.md
819a751 my cat made this commit
721dfe7 add top navbar
a4b12ad add bootstrap
fa9beb5 add basic HTML boilerplate
c5fcdc9 add project files
0e19c7a initial commit

my cat made this commit commit의 content를 보면 다음과 같다.

  • my cat made this commit commit의 app.js
alskj
askdj;) (; a
sdkljasdkljas
aslkdja)

이상한 내용들이 들어있는 것을 볼 수 있다. 이를 해결하는 가장 쉬운 방법은 해당 commit을 삭제하는 것이다.

git rebase -i HEAD~2

다음의 화면이 나올 것이다.

pick 819a751 my cat made this commit
pick 4025cc8 Create README.md

# Rebase 721dfe7..4025cc8 onto 721dfe7 (2 commands)

819a751drop으로 바꾸고 :wq로 저장해보도록 하자.

drop 819a751 my cat made this commit
pick 4025cc8 Create README.md

# Rebase 721dfe7..4025cc8 onto 721dfe7 (2 commands)

이제 결과를 확인해보도록 하자.

git log --oneline
330f9d3 (HEAD -> my-feat) Create README.md
721dfe7 add top navbar
a4b12ad add bootstrap
fa9beb5 add basic HTML boilerplate
c5fcdc9 add project files
0e19c7a initial commit

my cat made this commit commit이 삭제된 것을 볼 수 있다. 다음으로 해당 commit content들도 삭제되었는 지 확인해보도록 하자.

cat ./app.js

아무것도 없는 것을 볼 수 있다.

0개의 댓글