스파르타 spring 2기 TIL day8

fart man·2025년 12월 2일

오늘 git에 대해 공부하고 오랜만에 내 개인 git note repo를 업데이트 했다.

그리고 오늘 배운 점은...

git rebase -i는 뭐든지 할 수 있다는 것!!!!!!!

정말 뭐든지 할 수 있다.

git rebase 는 특정 범위에 commit들을 딴 commit위에 apply하는 command인데 git rebase -i는 저 commit들을 편집 할 수 있게 해준다.

그리고 편집이라는게 정말 뭐든지 가능하게 해준다!

commit 메세지를 바꾸거나

commit 순서를 바꾸거나

commit 쪼개거나 없애거나

아님 걍 commit 자체를 수정하거나!

아님 딴 커밋을 merge 하거나

정말 뭐든지 할 수 있다. 걍 commit editor다.

예시

아주 간단한 예시를 보자

C:>dir /w
 Volume in drive C has no label.
 Volume Serial Number is 8029-AC1B

 Directory of C:\Users\Meme\Documents\test\git_squash

[.]    [..]   list   
               1 File(s)             15 bytes
               2 Dir(s)  1,173,742,235,648 bytes free

C:>git log --oneline
43c64e3 (HEAD -> main) commit 5
1e46258 commit4
ba99c2f commit3
b24bcae commit2
1b2dd47 commit1

C:>type list
1
2
3
4
5

걍 각 commit 마다 list에 숫자를 추가한 repo다

요기다

git rebase -i ba99c2f

하면 git이 어떻게 저 commit들을 rebase 할까요?하고 물어봐준다.

pick 1e46258 commit4
pick 43c64e3 commit 5

# Rebase ba99c2f..43c64e3 onto ba99c2f (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# ...

그럼 첨에는 시시하게 commit message 만 수정해 보자.

reword 1e46258 commit4
pick 43c64e3 commit 5
# r, reword <commit> = use commit, but edit the commit message

그럼 git이 commit 수정하라고 editor를 띄어준다. 그럼 eidtor에서 수정하고 다시보면

C:>git log --oneline
482dc43 (HEAD -> main) commit 5
91a4ed2 commit4 -- modified
ba99c2f commit3
b24bcae commit2
1b2dd47 commit1

commit4 메세지가 수정이 잘 된것을 볼 수 있다.

근데... git 이 어떻게 rebase하고 싶냐고 물어봤을 때 이렇게 하면 어떻게 될까?

edit 91a4ed2 commit4 -- modified
pick 482dc43 commit 5
# e, edit <commit> = use commit, but stop for amending
C:>git status
interactive rebase in progress; onto ba99c2f
Last command done (1 command done):
   edit 91a4ed2 commit4 -- modified
Next command to do (1 remaining command):
   pick 482dc43 commit 5
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'main' on 'ba99c2f'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean


C:>git branch WTF


C:>git branch
* (no branch, rebasing main)
  WTF
  main


C:>git checkout WTF
Switched to branch 'WTF'


C:>echo WTF > WTF.txt


C:>git add WTF.txt


C:>git commit -m "LIKE, WTF?"
[WTF 7cd22af] LIKE, WTF?
 1 file changed, 1 insertion(+)
 create mode 100644 WTF.txt


C:>git status
On branch WTF
Last command done (1 command done):
   edit 91a4ed2 commit4 -- modified
Next command to do (1 remaining command):
   pick 482dc43 commit 5
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'main' on 'ba99c2f'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean


C:>git rebase --continue
Successfully rebased and updated refs/heads/main.


C:>git log --oneline --all --graph
* e82593b (HEAD -> main, WTF) commit 5
* 7cd22af LIKE, WTF?
* 91a4ed2 commit4 -- modified
* ba99c2f commit3
* b24bcae commit2
* 1b2dd47 commit1

... rebase하는 와중에 branch 생성이 된다.
정말 모든게 된다!!!

매우 무시무시한 코맨드이다.

0개의 댓글