기술나눔

[Git 연구 노트] 4장 git rebase rebase 작업 및 관련 예제(1부)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Chapter 4 리베이스 작업 및 관련 사례

【관련 주제】

  • 할 것이다 commit 버전을 다른 분기로 리베이스
  • 버전 충돌 시 리베이스 수행
  • 지정된 버전에서 대화형 리베이스 수행
  • 대화형 리베이스를 사용하여 버전 집계
  • 대화형 리베이스를 사용하여 커미터 변경
  • 자동 집계 버전

4.0 리베이스 소개

리베이스(Rebasing )은 Git의 매우 강력한 기능입니다.리베이스는 일종의 작업입니다.commit 가장 초기에는 다음을 기반으로했습니다.commit 그럼; 다음으로 리베이스, 즉, 그럴 것이다 기반이 되다 작업.

다음 데모 사례에서는 리베이스가 보기만큼 쉽지 않은 경우가 많다는 것을 알게 될 것입니다.

4.1은 commit 버전을 다른 분기로 리베이스

먼저 가장 간단한 유형의 리베이스 작업을 살펴보겠습니다. 관련 준비작업 : 새로운 파일을 소개하고 제출하고 내용을 변경한 후 다시 제출합니다.이런 식으로 로컬이 2배로 늘어납니다.commit 버전.

아직도 jgit 예를 들어 라이브러리:

# Clone jgit repo into chapter4
$ git clone https://git.eclipse.org/r/jgit/jgit chapter4
$ cd chapter4
# Checkout a new branch
$ git checkout -b rebaseExample --track origin/stable-3.1
# Create the 1st commit
$ echo "My Fishtank
    
Gravel, water, plants
Fish, pump, skeleton" > fishtank.txt
$ git add fishtank.txt
$ git commit -m "My brand new fishtank"
# Create the 2nd commit
$ echo "mosquitos" >> fishtank.txt
$ git add fishtank.txt
$ git commit -m "Feeding my fish"
# Rabase to stable-3.2
$ git rebase origin/stable-3.2
Successfully rebased and updated refs/heads/rebaseExample.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

리베이스하기 전:

여기에 이미지 설명을 삽입하세요.

리베이스한 후:

여기에 이미지 설명을 삽입하세요.

git rebase 실행 과정:

  1. 나타나다 HEAD 리베이스가 가리키는 대상 브랜치 간의 공통 버전(merge-base);
  2. 기반으로 merge-base, 대상 브랜치에서 누락된 버전을 모두 찾습니다.
  3. 누락된 버전을 대상 브랜치에 하나씩 적용해 보세요.

4.2 버전 충돌 시 리베이스 수행

만약 commit 버전 또는branch 분기를 다른 분기로 리베이스HEAD , 버전 충돌이 발생할 가능성이 높습니다.이 시점에서 충돌을 해결하고 명령을 실행해야 합니다.git rebase --continue, 리베이스가 완료될 수 있습니다.

이 섹션의 예제에서는 충돌이 있을 때 리베이스를 수행하는 방법을 보여줍니다.4.1절에서 시연된 최종 결과에 이어 이번에는rebaseExample 지점이 다음으로 리베이스되었습니다.stable-3.2 나뭇가지.이 예에서는 stable-3.1의 새 분기를 다시 확인하고 및rebaseExample 분기 이름은 동일하지만 내용이 호환되지 않는 텍스트 파일fishtank.txt

# Checkout rebaseExample2
$ git checkout -b rebaseExample2 --track origin/stable-3.1
# Add a new commit
$ echo "My Fishtank
Pirateship, Oister shell
Coconut shell
">fishtank.txt
$ git add fishtank.txt
$ git commit -m "My brand new fishtank"
# Rebase conflicting branches
$ git rebase rebaseExample
Auto-merging fishtank.txt
CONFLICT (add/add): Merge conflict in fishtank.txt
error: could not apply 24f9bf1ef... My brand new fishtank2
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 24f9bf1ef... My brand new fishtank2
$ subl fishtank.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

갈등 상황은 다음과 같습니다.

여기에 이미지 설명을 삽입하세요.

다음 내용으로 변경하고 저장한 후 닫습니다.

여기에 이미지 설명을 삽입하세요.

병합된 파일을 추가하고 리베이스를 계속합니다.

$ git add fishtank.txt
$ git rebase --continue
hint: Waiting for your editor to close the file...
  • 1
  • 2
  • 3

열리는 편집기는 그림에 표시되어 있습니다.

여기에 이미지 설명을 삽입하세요.

확인하고 저장한 후 닫으면 리베이스가 성공했다는 메시지가 표시됩니다.

$ git rebase --continue
[detached HEAD 9911772b3] My brand new fishtank2
 1 file changed, 2 insertions(+), 3 deletions(-)
Successfully rebased and updated refs/heads/rebaseExample2.
# Check new status via gitk
$ gitk
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

세부사항은 다음과 같습니다:

여기에 이미지 설명을 삽입하세요.

이 리베이스에서는 이전 브랜치에는 있지만 새 브랜치에는 없는 버전만 리베이스되는 것을 볼 수 있습니다(즉, 새 버전이 추가됩니다). commit)。

첫 번째 리베이스 중단에 대한 git 프롬프트 정보에서 다음 두 가지 대안도 볼 수 있습니다.

  1. git rebase --abort: 문자 그대로의 의미로 리베이스를 중단합니다.

  2. git rebase --skip: 충돌을 건너뛰고 직접 리베이스하면 rebaseExample2 새로운 것을 버리세요commit, 직접 병합됨 rebaseExample, 리베이스 후 부모가 가리키는 rebaseExample 일관된:

    여기에 이미지 설명을 삽입하세요.


4.3 지정된 버전에서 대화형 리베이스 수행

이 예에서는 rebaseExample 지점을 기반으로 사용 방법 시연--interactive 마크, 4.1에서 두 가지를 리베이스하세요commit 원격 추적 분기로 리베이스stable-3.1 우수한:

$ git checkout rebaseExample
Switched to branch 'rebaseExample'
Your branch is ahead of 'origin/stable-3.1' by 109 commits.
  (use "git push" to publish your local commits)
$ git rebase origin/stable-3.1
  • 1
  • 2
  • 3
  • 4
  • 5

이때 새로 팝업 편집기에 커밋 목록이 표시되며 범위는 다음과 같습니다. rebaseExample 그리고stable-3.1 그 사이에 모두 리베이스할 수 있습니다.stable-3.1 ~의commit 기록.예제에서 두 개의 새로운 항목을 유지하십시오.commit, 나머지 내용을 모두 삭제합니다(즉, 89행 이전의 내용).

여기에 이미지 설명을 삽입하세요.

저장 후 편집기를 종료하면 다음 출력이 표시됩니다.

$ git rebase --interactive origin/stable-3.1
Successfully rebased and updated refs/heads/rebaseExample.
# Check in gitk view:
  • 1
  • 2
  • 3

결과는 다음과 같습니다.

여기에 이미지 설명을 삽입하세요.

확장 예

이 예에서는 다음 명령을 통해 원하는 효과를 빠르게 얻을 수도 있습니다.

$ git rebase --onto origin/stable-3.1 origin/stable-3.2 rebaseExample
Successfully rebased and updated refs/heads/rebaseExample.
  • 1
  • 2

리베이스 전후의 다이어그램은 다음과 같습니다.

여기에 이미지 설명을 삽입하세요.