git rebase -i 로 기존 커밋 수정하기

부가 설명
이전에 작성한 커밋 수정하기 interactive 모드로 rebase 해서 커밋 수정할 수 있음 머지 커밋 취소하기 : rebase -i -r 커밋번호
Tags
commit
edit
 
 

git rebase -i

변경하고자 하는 커밋의 직전 커밋에 대한 해쉬값으로 rebase -i를 명령한다.
$ git rebase -i <변경하고자 하는 커밋의 직전 커밋의 해쉬값>
notion image
위의 예시로 보면 명령어는 다음과 같아야 한다.
$ git rebase -i 6a104306fb22c2e656098a0d02a57a202daf4eb4
 

수정하고 싶은 커밋을 edit으로 수정

rebase 기점 이후의 커밋들이 나열되고 기본값으로 pick으로 되어있는데, 수정하고 싶은 커밋을 edit으로 변경하고 저장한다.
notion image
notion image
 

커밋 변경 방법 ( 선호하는 것 택1 )

방법 1. 기존 커밋에 추가 반영하기

  • 기존의 커밋 내용은 그대로 남겨둔 채로, 추가 작업 후 git commit -—amend 하여 그 커밋에 해당 내용을 추가시킬 수 있다.
  • git rebase --continue를 통해 수정을 마무리한다.

방법 2. 커밋 취소 후 새로운 커밋 만들기

  1. 기존의 커밋사항은 여전히 커밋에 반영된 상태이므로 changes를 보면 아무것도 안나오는 것을 볼 수 있는데, 이것도 보면서 편집하고 싶다면 git reset HEAD^ 해서 커밋을 취소하여 전부 changes에 올려두고 작업하면 된다.
  1. 일반적으로 커밋 하듯 커밋을 새로 만들어주면 된다. git commit -m “fix: 펌웨어 보틀데이터 검증로직 추가된 것 반영”
  1. git rebase --continue 통해 수정을 마무리한다.
💡
reset을 하더라도 VSCode에는 기존 커밋 코멘트가 남아있어서, 코멘트 작성 시 활용하면 된다.
notion image
 

머지 커밋 취소하기

머지 커밋만 보기

# 머지 커밋만 모아서 보는 명령어 $ git log --merges

rebrase -i -r 일 때 커맨드 사용법

# # 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 # . specified); use -c <commit> to reword the commit message # # These lines can be re-ordered; they are executed from top to bottom. # # 여기 줄을 제거하면 해당 커밋을 잃어버립니다! # # However, if you remove everything, the rebase will be aborted. #

특정 머지 커밋 취소하는 예시

-C(default) 부분을 원하는 커맨드 옵션으로 대체시키면 되는데, drop 시키길 원하므로 -d 로 교체 후 저장하면 된다.
notion image
 
git rebase -i -r af8d01ac510c91a424fc0aeaea0776550836a09c # -r은 --rebase-merges와 동일하다
 
notion image