기본 Git 명령어만으로는 복잡한 협업 상황을 처리하기 어렵습니다. Interactive Rebase, Cherry-Pick, Stash를 활용하면 깔끔한 커밋 히스토리를 유지하며 효율적으로 작업할 수 있습니다.
Basic Git commands alone are not enough for complex collaboration scenarios. Using Interactive Rebase, Cherry-Pick, and Stash, you can work efficiently while maintaining clean commit history.
Interactive Rebase로 커밋 정리하기 Cleaning Up Commits with Interactive Rebase
PR을 올리기 전에 "WIP", "fix typo" 같은 중간 커밋들을 정리하면 리뷰어가 변경사항을 이해하기 쉬워집니다. git rebase -i를 사용하면 커밋을 합치고, 순서를 바꾸고, 메시지를 수정할 수 있습니다.
Before creating a PR, cleaning up intermediate commits like "WIP" or "fix typo" makes it easier for reviewers to understand changes. git rebase -i allows you to squash, reorder, and edit commit messages.
$ git rebase -i HEAD~3
# 에디터가 열리면 다음과 같은 내용이 보임
pick abc1234 기능 A 구현
pick def5678 typo 수정
pick ghi9012 기능 A 테스트 추가
# 커밋을 합치려면 pick을 squash(또는 s)로 변경
pick abc1234 기능 A 구현
squash def5678 typo 수정
squash ghi9012 기능 A 테스트 추가
$ git rebase -i HEAD~3
# Editor opens with content like this
pick abc1234 Implement feature A
pick def5678 Fix typo
pick ghi9012 Add tests for feature A
# To squash commits, change pick to squash (or s)
pick abc1234 Implement feature A
squash def5678 Fix typo
squash ghi9012 Add tests for feature A
🔧 Rebase 명령어 옵션 🔧 Rebase Command Options
- pick: 커밋 그대로 사용
- pick: Use commit as-is
- reword: 커밋 메시지만 수정
- reword: Edit commit message only
- squash: 이전 커밋과 합치기 (메시지 수정 가능)
- squash: Meld into previous commit (can edit message)
- fixup: 이전 커밋과 합치기 (메시지 버림)
- fixup: Meld into previous commit (discard message)
- drop: 커밋 삭제
- drop: Remove commit
⚠️ Rebase 주의사항 ⚠️ Rebase Caution
이미 원격에 push한 커밋은 rebase하지 마세요. 다른 팀원의 작업에 영향을 줍니다. 로컬에서만 작업한 커밋에 대해서만 rebase를 사용하세요.
Never rebase commits that have been pushed to remote. It affects other team members' work. Only use rebase for commits that are local-only.
Cherry-Pick으로 특정 커밋만 가져오기 Picking Specific Commits with Cherry-Pick
다른 브랜치의 특정 커밋만 현재 브랜치에 적용하고 싶을 때 사용합니다. 핫픽스를 여러 브랜치에 적용하거나, 실수로 잘못된 브랜치에 커밋했을 때 유용합니다.
Use when you want to apply specific commits from another branch to the current branch. Useful for applying hotfixes to multiple branches or when you accidentally committed to the wrong branch.
$ git cherry-pick abc1234
# 여러 커밋을 연속으로 적용
$ git cherry-pick abc1234 def5678 ghi9012
# 범위로 적용 (abc1234는 포함 안됨, def5678부터 ghi9012까지)
$ git cherry-pick abc1234..ghi9012
# 커밋하지 않고 스테이징만 (여러 커밋을 하나로 합칠 때)
$ git cherry-pick -n abc1234
$ git cherry-pick abc1234
# Apply multiple commits in sequence
$ git cherry-pick abc1234 def5678 ghi9012
# Apply range (abc1234 not included, from def5678 to ghi9012)
$ git cherry-pick abc1234..ghi9012
# Stage only without committing (when combining multiple commits)
$ git cherry-pick -n abc1234
Stash로 임시 작업 저장하기 Saving Work Temporarily with Stash
현재 작업 중인 변경사항을 임시로 저장하고 깨끗한 상태로 되돌립니다. 급하게 다른 브랜치로 전환해야 할 때, 아직 커밋할 준비가 안 된 작업을 보관할 수 있습니다.
Temporarily save current changes and return to a clean state. When you need to quickly switch to another branch, you can store work that's not ready to commit.
$ git stash
# 메시지와 함께 저장 (나중에 구분하기 쉬움)
$ git stash push -m "WIP: 로그인 기능"
# 저장된 stash 목록 보기
$ git stash list
stash@{0}: On main: WIP: 로그인 기능
stash@{1}: WIP on feature: abc1234 이전 작업
# 가장 최근 stash 복원 (stash에서 제거)
$ git stash pop
# 특정 stash 복원 (stash에서 제거하지 않음)
$ git stash apply stash@{1}
# stash 내용 미리보기
$ git stash show -p stash@{0}
$ git stash
# Save with message (easier to identify later)
$ git stash push -m "WIP: login feature"
# List saved stashes
$ git stash list
stash@{0}: On main: WIP: login feature
stash@{1}: WIP on feature: abc1234 previous work
# Restore most recent stash (removes from stash)
$ git stash pop
# Restore specific stash (keeps in stash)
$ git stash apply stash@{1}
# Preview stash contents
$ git stash show -p stash@{0}
💡 Stash 활용 팁 💡 Stash Usage Tips
git stash push -u를 사용하면 untracked 파일도 함께 저장됩니다.
새로 생성한 파일까지 모두 임시 저장하고 싶을 때 유용합니다.
Using git stash push -u also saves untracked files.
Useful when you want to stash everything including newly created files.
복잡한 충돌 해결 전략 Strategies for Complex Conflict Resolution
$ git status
# 충돌 파일의 세 가지 버전 확인
$ git show :1:파일명 # 공통 조상
$ git show :2:파일명 # 현재 브랜치 (ours)
$ git show :3:파일명 # 병합 대상 (theirs)
# 한쪽을 선택하여 해결
$ git checkout --ours 파일명 # 현재 브랜치 버전 사용
$ git checkout --theirs 파일명 # 병합 대상 버전 사용
# 충돌 해결 후 계속 진행
$ git add 파일명
$ git rebase --continue
$ git status
# View three versions of conflicting file
$ git show :1:filename # Common ancestor
$ git show :2:filename # Current branch (ours)
$ git show :3:filename # Merge target (theirs)
# Resolve by choosing one side
$ git checkout --ours filename # Use current branch version
$ git checkout --theirs filename # Use merge target version
# Continue after resolving conflict
$ git add filename
$ git rebase --continue
Git Hooks 활용하기 Using Git Hooks
Git Hooks는 커밋, 푸시 등의 이벤트 전후에 자동으로 스크립트를 실행합니다. 코드 품질 검사, 테스트 실행, 커밋 메시지 검증 등에 활용할 수 있습니다.
Git Hooks automatically run scripts before or after events like commit and push. Can be used for code quality checks, running tests, validating commit messages, and more.
# 커밋 전에 lint 검사 실행
#!/bin/sh
npm run lint
if [ $? -ne 0 ]; then
echo "Lint 검사 실패! 커밋이 취소됩니다."
exit 1
fi
# Run lint check before commit
#!/bin/sh
npm run lint
if [ $? -ne 0 ]; then
echo "Lint check failed! Commit cancelled."
exit 1
fi
💡 Husky로 Git Hooks 관리하기 💡 Managing Git Hooks with Husky
Husky를 사용하면 Git Hooks를 팀 전체와 공유하기 쉽습니다.
npx husky-init && npm install로 시작하세요.
Using Husky makes it easy to share Git Hooks with your team.
Start with npx husky-init && npm install.
