Git Interactive Rebase 란 무엇입니까?
Il git 대화형 리베이스 (대화형 리베이스)는 가장 강력한 도구 중 하나입니다. Git의 커밋 히스토리를 다시 작성. 재구성, 수정, 작업을 공유하기 전에 커밋을 결합하거나 삭제하여 깨끗한 기록을 만들고 코드 검토 및 디버깅을 용이하게 하는 전문가입니다.
🎯 대화형 리베이스로 할 수 있는 일
- 스쿼시: 여러 커밋을 하나로 결합
- 바꾸어 말하다: 커밋 메시지 편집
- 편집하다: 커밋 내용 수정
- 재정렬: 커밋 재정렬
- 액: 원치 않는 커밋 삭제
- 수정사항: 메시지를 삭제하여 커밋 결합
⚠️ 황금률: 공개 커밋을 절대 리베이스하지 마세요!
Rebase는 Git 기록을 다시 작성합니다. 이미 브랜치에 푸시된 커밋을 리베이스하지 마세요. 공유됨 - 모든 협력자에게 문제를 일으킬 수 있습니다. 커밋에만 리베이스 사용 병합하기 전에 로컬 또는 개인 기능 분기에 있습니다.
대화형 리베이스 시작
대화형 리베이스를 시작하려면 리베이스할 백 커밋 수를 지정하세요.
git rebase -i HEAD~N 여기서 N은 커밋 수입니다.
# Riscrivere gli ultimi 3 commit
git rebase -i HEAD~3
# Rebase tutti i commit dal branch main
git rebase -i main
# Rebase da uno specifico commit (escluso)
git rebase -i abc1234
# Vedere quali commit saranno inclusi prima di rebase
git log --oneline HEAD~5..HEAD
대화형 편집기
대화형 리베이스를 시작하면 Git은 다음을 표시하는 구성된 텍스트 편집기를 엽니다. 커밋 및 사용 가능한 옵션 목록:
pick abc1234 Add user authentication
pick def5678 Fix typo in auth module
pick ghi9012 Add password validation
pick jkl3456 Update tests for auth
# Rebase 0123456..jkl3456 onto 0123456 (4 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
# 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>]
일반적인 대화형 리베이스 작업
1. 스쿼시: 커밋 결합
스쿼싱 여러 커밋을 하나로 결합하여 통합에 적합 WIP 커밋(진행 중인 작업) 또는 논리적이고 완전한 커밋의 작은 수정 사항입니다.
# Storia originale
pick abc1234 Add login form
pick def5678 Fix button styling
pick ghi9012 Add validation
pick jkl3456 Fix validation bug
# Modifica in:
pick abc1234 Add login form
squash def5678 Fix button styling
squash ghi9012 Add validation
squash jkl3456 Fix validation bug
# Risultato: 1 commit che combina tutti e 4
저장한 후 Git은 새 커밋 메시지를 생성하라는 메시지를 표시합니다.
# This is a combination of 4 commits.
# This is the 1st commit message:
Add login form
# This is the commit message #2:
Fix button styling
# This is the commit message #3:
Add validation
# This is the commit message #4:
Fix validation bug
# Puoi modificare in un messaggio unico più chiaro:
feat: implement user login with form validation
- Created login form component
- Added email and password validation
- Styled form buttons
- Fixed validation edge cases
스쿼시 전:
abc1234 Add login form
def5678 Fix button styling
ghi9012 Add validation
jkl3456 Fix validation bug
스쿼시 후:
xyz7890 feat: implement user
login with validation
2. 수정: 메시지 없는 스쿼시
fixup 스쿼시와 비슷하지만 커밋 메시지만 버리고 커밋 메시지만 삭제합니다.
이전 커밋의 메시지입니다. 중간 메시지가 필요하지 않을 때 더 빠릅니다.
pick abc1234 Add user profile page
fixup def5678 Fix typo
fixup ghi9012 Fix linting errors
fixup jkl3456 Remove console.log
# Risultato: 1 commit con messaggio "Add user profile page"
# I messaggi degli altri 3 sono scartati automaticamente
💡 전문가 팁: git commit --fixup
커밋할 때 다음을 사용할 수 있습니다. --fixup 이전 커밋의 수정 사항으로 표시하려면 다음을 수행하세요.
# Durante lo sviluppo
git commit -m "Add feature X"
# ... lavori ...
git commit --fixup abc1234 # abc1234 è l'hash del commit da fixare
# Poi quando fai rebase
git rebase -i --autosquash HEAD~5
# Git automaticamente organizza i fixup commits!
3. 수정: 메시지 편집
변화 pick in reword 커밋 메시지만 편집하려면
내용을 건드리지 않고.
pick abc1234 Add user auth
reword def5678 Fixed bug
pick ghi9012 Update tests
# Quando salvi, Git apre l'editor per def5678
# e puoi cambiare "Fixed bug" in qualcosa di più descrittivo:
fix: resolve authentication timeout issue
Fixed a race condition that caused authentication
to timeout under heavy load.
4. 편집: 커밋 내용 편집
미국 edit 리베이스를 중지하고 커밋을 수정할 수 있도록 허용
(파일 추가/제거, 코드 편집).
pick abc1234 Add payment module
edit def5678 Add validation
pick ghi9012 Add tests
# Quando arrivi a def5678, il rebase si ferma
# Ora puoi modificare file
vim src/validation.ts
# Aggiungi le modifiche al commit
git add src/validation.ts
git commit --amend --no-edit
# Oppure crea nuovi commit
git commit -m "Additional changes"
# Continua il rebase
git rebase --continue
5. 삭제: 커밋 삭제
변화 pick in drop (또는 줄 삭제) 제거
완전히 역사의 커밋입니다.
pick abc1234 Add feature X
drop def5678 Add debug logging
pick ghi9012 Add feature Y
# Oppure semplicemente elimina la riga:
pick abc1234 Add feature X
pick ghi9012 Add feature Y
# Il commit def5678 sparisce dalla storia
6. 재정렬: 커밋 재정렬
커밋 순서를 변경하려면 편집기에서 줄을 이동하기만 하면 됩니다.
# Ordine originale
pick abc1234 Add tests
pick def5678 Add feature
pick ghi9012 Add documentation
# Riordina per logica migliore
pick def5678 Add feature
pick abc1234 Add tests
pick ghi9012 Add documentation
# I commit vengono riapplicati nel nuovo ordine
⚠️ 갈등을 조심하세요
커밋이 서로 의존하는 경우 커밋 순서를 바꾸면 충돌이 발생할 수 있습니다. 커밋이 이후 커밋에 의해 추가된 코드를 수정하는 경우 충돌이 발생합니다.
커밋 분할
가끔은 당신이 필요 대규모 커밋을 분할하다 더 작고 더 집중된 커밋으로.
미국 edit e git reset.
# Avvia rebase e marca il commit da dividere con 'edit'
git rebase -i HEAD~3
# Quando il rebase si ferma al commit da dividere:
# 1. Resetta il commit (mantiene i cambiamenti come unstaged)
git reset HEAD^
# 2. Ora hai tutti i cambiamenti come uncommitted
git status
# 3. Crea commit più piccoli e focalizzati
git add src/user.model.ts
git commit -m "Add user model"
git add src/user.service.ts
git commit -m "Add user service"
git add src/user.component.ts
git commit -m "Add user component"
# 4. Continua il rebase
git rebase --continue
# Risultato: 1 commit diviso in 3 commit logici
오류 관리 및 복구
진행 중인 리베이스 취소
# Se qualcosa va storto durante il rebase
git rebase --abort
# Torna allo stato precedente al rebase
리베이스 중 충돌 해결
# Se incontri conflitti durante rebase
# Git si ferma e mostra i file in conflitto
# 1. Risolvi i conflitti nei file
vim conflicted-file.ts
# 2. Aggiungi i file risolti
git add conflicted-file.ts
# 3. Continua il rebase
git rebase --continue
# Se il conflitto è su un commit che vuoi skippare
git rebase --skip
잘못된 리베이스 복구
# Git salva tutto nel reflog - puoi sempre recuperare
# Vedi la storia completa delle operazioni
git reflog
# Output esempio:
# abc1234 HEAD@{{ '{' }}0{{ '}' }}: rebase finished
# def5678 HEAD@{{ '{' }}1{{ '}' }}: rebase: checkout main
# ghi9012 HEAD@{{ '{' }}2{{ '}' }}: commit: Add feature
# Torna allo stato prima del rebase
git reset --hard HEAD@{{ '{' }}2{{ '}' }}
# Oppure crea un branch di backup
git branch backup-before-rebase HEAD@{{ '{' }}2{{ '}' }}
대화형 리베이스를 사용한 전문적인 워크플로우
📋 모범 사례 워크플로
1. 개발 중: 빈번한 커밋
# Commit spesso con messaggi WIP
git commit -m "WIP: add login form"
git commit -m "WIP: fix styling"
git commit -m "WIP: add validation"
git commit -m "WIP: fix bug"
2. 푸시 전: 기록 정리
# Combina i WIP in commit logici
git rebase -i HEAD~10
# Squash tutti i WIP in:
# - feat: implement login with validation
# - test: add login form tests
# - docs: update authentication docs
3. Pull Request 전: 최종 검토
# Assicurati di avere base branch aggiornato
git fetch origin
git rebase -i origin/main
# Riordina se necessario
# Migliora messaggi di commit
# Rimuovi commit di debug/test
고급 명령 및 요령
--autosquash를 사용한 자동 스쿼시
# Durante sviluppo, marca i fixup
git commit --fixup abc1234
git commit --fixup def5678
# Al rebase, Git organizza automaticamente
git rebase -i --autosquash HEAD~10
# I commit fixup vengono automaticamente
# posizionati dopo il commit che fixano
Exec: 리베이스 중 명령 실행
pick abc1234 Add feature A
exec npm test
pick def5678 Add feature B
exec npm test
pick ghi9012 Add feature C
exec npm test
# Git esegue i test dopo ogni commit
# Se i test falliscono, il rebase si ferma
루트 리베이스: 첫 번째 커밋에서 리베이스
# Rebase dall'inizio della storia
git rebase -i --root
# Utile per pulizia completa del repository
Interactive Rebase를 사용하지 말아야 할 경우
❌ 피해야 할 상황
- 공공 지점: 메인, 개발 또는 기타 공유 브랜치를 리베이스하지 마세요.
- 이미 푸시된 커밋: 다른 사람들이 당신의 커밋에 기반한 작업을 한 경우
- 복잡한 충돌 중: 병합이 더 쉬울 수 있습니다
- 중요한 이야기: 감사에 정확한 이력이 중요한 경우
결론
Il git 대화형 리베이스 스토리를 유지하는 데 매우 강력한 도구입니다. 깨끗하고 전문적인 Git. 이를 사용하여 작업 커밋을 논리적이고 잘 설명된 커밋으로 통합합니다. 코드를 공유하기 전에. 항상 기억하세요: 로컬 커밋만 리베이스하고 공개 커밋은 절대 하지 마세요!
🎯 대화형 리베이스 체크리스트
- ✅ 푸시되지 않은 로컬 커밋에만 사용하세요.
- ✅ 복잡한 리베이스 전 분기 백업:
git branch backup - ✅ 푸시 전 스쿼시 커밋 WIP/디버깅
- ✅ 스쿼시 후에 명확한 커밋 메시지 작성
- ✅ 리베이스 후 코드 테스트
- ✅ 사용
git reflog필요한 경우 복구를 위해
📚 Git 시리즈의 다음 작품
다음 기사에서는 살펴보겠습니다. Git 리베이스와 병합, 통합할 전략과 모범 사례를 언제 사용해야 하는지 이해 프로 팀의 변화.







