Git
Git Rebase - 커밋 히스토리를 깔금하게
강태종
2022. 2. 8. 21:01
브랜치를 합치는 방법에는 크게 merge와 rebase가 있습니다. 두 방법의 차이는 크게 커밋 히스토리가 달라집니다.
merge
- 커밋 순서를 바꾸지 않습니다.
- 존재하는 브랜치가 변경되지 않습니다.
- 새로운 merge commit이 생깁니다.
- 변경 내용의 이력이 모두 그대로 남아 있기 때문에 이력이 복잡해집니다.
rebase
- 커밋 메시지가 시간 순서대로 합쳐집니다.
- 히스토리가 좀 더 깔끔해집니다.
- 원래의 커밋 이력이 변경되기 때문에 정확한 이력을 남겨야 할 필요가 있을 경우 사용하면 안됩니다.
rebase는 base를 새롭게 설정한다는 의미이고, 커밋 메시지가 좀 더 깔끔해지는 특징이 있습니다.
rebase명령어를 통해 커밋 히스토리를 깔금하게 브랜치를 병합할 수 있습니다.
user@AL01724100 Git-Study % ls
aa bb cc
aa, bb, cc 3개의 파일이 있고, master에서 aa, bb를 작업하고 cc브랜치에서 cc를 작업하고 merge를 진행한다고 가정하자.
* commit a9d0be9cc7e6abd426c722958795719da56ab7b0 (HEAD -> cc)
|\ Merge: 80687b0 6aeab4f
| | Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| | Date: Tue Feb 8 17:04:07 2022 +0900
| |
| | Merge branch 'master' into cc
| |
| * commit 6aeab4f4795b2e7b838394a4e6580bbede1a6370 (master)
| | Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| | Date: Tue Feb 8 17:03:16 2022 +0900
| |
| | update bb
| |
| * commit 50609c2cd8a093b314131979165abe97246237d8
| | Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| | Date: Tue Feb 8 17:03:05 2022 +0900
| |
| | update aa
master와 cc 브랜치가 유지되고 merge 커밋 내역이 생기면서 서로 합쳐진 그래프를 볼 수 있습니다.
* commit 09002cec14d342ac31ed72c9c7941e7fb5851657 (HEAD -> cc)
| Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| Date: Tue Feb 8 17:34:39 2022 +0900
|
| update cc
|
* commit 9240159219da6cab9de4000ff73ae2a3f031abd7 (master)
| Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| Date: Tue Feb 8 17:34:15 2022 +0900
|
| update aa, cc
|
만약 merge가 아닌 rebase로 진행한다면 master와 cc의 브랜치가 합쳐지며 cc의 base가 재설정된 것을 볼 수 있습니다. 여기서 추가적으로 master와 cc를 merge를 진행하고 cc의 브랜치를 삭제하면 깔끔하게 커밋 내용을 관리할 수 있습니다.
rebase 명령어를 통해 커밋된 내용을 합칠 수 있습니다.
user@AL01724100 Git-Study % git add *
user@AL01724100 Git-Study % git commit -m "c1"
[cc 4c5bcc9] c1
1 file changed, 1 insertion(+), 1 deletion(-)
user@AL01724100 Git-Study % git add *
user@AL01724100 Git-Study % git commit -m "c2"
[cc d080cd9] c2
1 file changed, 1 insertion(+), 1 deletion(-)
user@AL01724100 Git-Study % git add *
user@AL01724100 Git-Study % git commit -m "c3"
[cc b289937] c3
1 file changed, 1 insertion(+), 1 deletion(-)
작업을 하면서 작은 단위로 커밋을 했다고 가정합시다.
* commit b28993700b3f74ad5e30cea910410d7df05a32ef (HEAD -> cc)
| Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| Date: Tue Feb 8 17:49:15 2022 +0900
|
| c3
|
* commit d080cd91d3cf8d356adefd26d773365f08019d4a
| Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| Date: Tue Feb 8 17:49:05 2022 +0900
|
| c2
|
* commit 4c5bcc9c18fae52ebc89bdfa8dfc2a59dc1c9cd4
| Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| Date: Tue Feb 8 17:48:56 2022 +0900
|
| c1
|
그렇다면 이렇게 많은 커밋 히스토리가 쌓일 것 입니다.
git rebase -i head~3
rebase 명령어에 -i head~3을 추가하면 head부터 3개의 commit을 rebase할 수 있습니다.
pick 4c5bcc9 c1
pick d080cd9 c2
pick b289937 c3
# Rebase 55831da..b289937 onto 55831da (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
# . message (or the oneline, if no original merge commit was
3개의 커밋 내역을 불러오고 pick을 squash로 바꾸어 squash 기능을 통해 커밋 내용을 합칠 수 있습니다.
* commit 55831da9a5614539e12a4fd0c5be45bb092dd777
| Author: rkdxowhd98 <rkdxowhd98@webtoonscorp.com>
| Date: Tue Feb 8 17:44:48 2022 +0900
|
| * c1
|
| * c2
|
| * c3
|
하나의 커밋으로 합쳐지며 커밋 메시지를 수정할 수 있습니다.