IaC/Configuration Management

Git Rebase vs Merge

Somaz 2024. 6. 3. 13:18
728x90
반응형

Overview

Git Rebase와 Merge에 대해서 알아보자.

 


Git Rebase

Rebase는 Feature Branch에서 Another Branch(typically the main branch)의 베이스로 Commit을 이동하거나 "replaying"함으로써 한 Branch에서 다른 Branch로 변경 사항을 통합하는 데 사용되는 강력한 Git 기능이다. Rebase의 주요 목표는 선형 프로젝트 히스토리(linear project history)를 만드는 것이다.

Example Git Rebase

 

 

Functions in Rebase

대화형 Rebase(`git rebase -i`)를 수행하는 동안 사용할 수 있는 몇 가지 일반적인 기능은 다음과 같다.

  • pick: 수정 없이 Commit을 그대로 사용
  • reword: Commit을 사용하되 Commit 메시지를 변경
  • edit: 어떤 방식으로든 Commit을 수정할 수 있도록 이 Commit에서 Rebase를 일시 중지(code changes, splitting the commit, further amends).
  • squash: 이 Commit을 이전 Commit과 결합하고 새 커밋 메시지를 제공하라는 메시지를 표시
  • fixup: squash와 유사하지만 이전 Commit의 메시지를 유지하고 이 Commit의 로그 메시지를 삭제
  • drop: 기록에서 Commit을 완전히 제거

 

 

Git Merge

Merge는 Merge된 Branch의 변경 사항을 결합한 새로운 "Merge Commit"을 생성하여 한 Branch의 변경 사항을 다른 Branch로 통합하는 데 사용된다. (non-linear history)

 

 

Understanding `ours` and `theirs` in Git

Git의 충돌을 해결할 때, 특히 Merge 및 Rebase 작업 중에 ours와 theirs 서로 다른 지점의 변경 사항을 참조할 수 있는 방법을 제공한다.

 

Operation ours theirs Description
Merge Changes in the target branch. Changes in the branch being merged. During a merge, ours refers to the branch you are on (the one that is receiving the merge), and theirs refers to the branch being merged into your current branch. This helps decide which changes to prioritize if conflicts arise.
Rebase Changes in the branch being rebased. Changes in the branch onto which you are rebasing. In a rebase, ours and theirs flip roles compared to a merge. Here, ours refers to changes in the branch that you are attempting to rebase (originally), and theirs refers to the branch providing the new base for the commits. This switch is often a source of confusion and requires careful attention during conflict resolution.

 

 

In Merge

  • ours: 현재 있는 Branch를 말하며, 이는 다른 Branch가 Merge되는 대상 Branch이다. feature를 main에 Merge하고 main Branch에 있는 경우 ours는 main Branch의 변경 사항을 나타낸다.
  • theirs: 대상 Branch에 Merge되는 Branch를 나타낸다. 위의 예를 들어 게속 설명하면 theirs는 feature Branch의 변경 사항을 나타낸다.

출처 : https://nitaym.github.io/ourstheirs/

 

출처 : https://nitaym.github.io/ourstheirs/

 

 

In Rebase

  • ours: 상황에 따라 변경된다. Rebase 중에 ours는 다른 Branch로 Rebase되는 Branch의 변경 사항을 나타낸다. feature를 main으로 Rebase하는 경우 충돌이 발생하면 ours는 원래 feature에 있던 것을 참조한다.
  • theirs: Rebase의 맥락에서 theirs는 Rebase하는 Branch에 대한 새 기반을 제공하는 Branch를 나타낸다. feature를 main으로 Rebase하는 경우 theirs는 Rebase가 적용되는 지점에서 main Branch의 변경 사항을 나타낸다.

출처 : https://nitaym.github.io/ourstheirs/

 

출처 : https://nitaym.github.io/ourstheirs/

 

 

Practical Usage

  • Merge 시나리오: 두 개의 Branch를 Merge하고 충돌이 발생하면 우선순위를 지정하려는 Branch의 관점(target or incoming)에 따라 ours 또는 theirs 해결 방법을 선택할 수 있다.
  • Rebase 시나리오: Rebase 중에 ours 와 theirs 중에서 선택하는 것은 변경 사항 및 기본 컨텍스트에 따라 역할이 전환되기 때문에 좀 더 혼란스러울 수 있다. ours는 (현재) Rebase되는 Branch이고, theirs는 (충돌시킨) Branch이다.

 

 

Popular Commands in Merge vs. Rebase

  • Merge는 빨리 fast-forward merge이 가능한 경우에도 Merge Commit을 생성하기 위해 --no-ff(no fast forward)와 같은 옵션과 함께 자주 사용되어 기능 분기의 과거 존재에 대한 정보를 보존한다.
  • Rebase는 일련의 변경 사항을 기본 프로젝트 분기에 병합하기 전에 정리하기 위해 --interactive와 함께 자주 사용된다.

 

 

 

 


Reference

https://git-scm.com/book/ko/v2/Git-브랜치-Rebase-하기

https://seosh817.tistory.com/240

https://nitaym.github.io/ourstheirs/

https://wonyong-jang.github.io/git/2021/02/05/Github-Rebase.html

728x90
반응형