[Git] rebase는 무엇이고 주의점과 사용방법

Git 2023. 6. 25.

  목 차

  • rebase 사용방법
  • rebase 주의점
  • 원하는 Commit만 Merge 하기
  • 특정 commit 삭제

 

 


 

rebase는 내가 작업하는 branch가 파생된 master branch(또는 main)를 업데이트 하는 것이라고 생각하면 된다.

 

주의점이 있는데, 같은 branch에서 다른 개발자와 함께 작업하고 있거나, 서버에 history가 올라가 있다면 사용에 주의가 필요하다.

주의점은 여기서 따로 정리하지는 않겠다.

 

 

  rebase 사용방법

 

먼저 내가 작업하고 있는 branch로 이동하고, 다음 명령어를 실행해준다.

git rebase 브랜치_이름

 

 

rebase --onto

 

이 옵션은 브랜치가 체이닝으로 파생되었을 때, 특정 브랜치를 다른 브랜치로 재연결 싶을 때 사용할 수 있다.

예를 들면, master branch에서 파생된 submit branch가 있고 또 여기서 파생된 submit-ui branch가 있다고 가정하자.

이게 submit-ui branch를 master branch로 옮기고 싶다면 다음과 같이 명령어를 작성하면 된다.

git rebase --onto master submit submit-ui

 

 

  rebase 주의점

 

⚠️ 또 주의점이 존재하는데, 이 rebase는 그저 commit들을 이동시키는 것이 아닌, 똑같은 것을 생성하는 개념이다. 이 것이 위에서 언급한 주의점의 이유이기도 하다. 그러니 공유된 브랜치에서의 rebase는 문제를 일으킬 수 있다는 것을 생각하자.

 

 


  원하는 Commit만 Merge 하기

 

특정 기능의 개발이 길어지는 경우, 특정 Commit만 먼저 필요한 경우가 생길 수 있다. 그런 경우 cherry-pick 이라는 명령어를 사용하게 되는데, 사용법은 간단하다.

먼저 필요한 commit의 해시코드를 복사한다. 그리고 다음 명령어를 작성하면 된다.

git cherry-pick 해시코드

 

 


  특정 Commit 삭제

 

제거하려는 Commit의 하나 이전의 Commit 해시코드를 복사해준다.

 

그리고 다음 명령어를 작성하여 VScode(다를 수 있음)를 열어준다.

git rebase -i 해시코드

 

더보기: 다음은 rebase의 옵션들이니 참고하자

더보기
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
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

 

삭제하려는 commit을 drop으로 바꾸어 주고 저장한다.