Technology Sharing

[Git Learning Notes] Chapter 4 git rebase rebase operation and related examples (Part 1)

2024-07-12

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

Chapter 4 Rebase Operations and Related Cases

【related topic】

  • Will commit Rebase the version to another branch
  • Rebase in case of version conflicts
  • Perform an interactive rebase on the specified version
  • Aggregate versions using interactive rebase
  • Changing committers with interactive rebase
  • Automatically aggregate versions

4.0 Introduction to rebasing

RebaseRebasing) is an extremely powerful feature of Git. Rebasing is a type of operation: if a commit A The earliest was based oncommit B ; thenA Rebase toC, that is, A Become based onC operation.

In the following demonstration cases, you will find that rebasing operations are often not as easy as they seem.

4.1 commit Rebase the version to another branch

Let's first look at the simplest type of rebase operation. The relevant preparation work: introduce a new file, commit, change the content, and commit again. In this way, there are two local commit Version.

Or jgit Library as an example:

# 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

Before rebasing:

insert image description here

After rebasing:

insert image description here

git rebase The execution process:

  1. turn up HEAD The common version between the target branch pointed to by the rebase (merge-base);
  2. based on merge-base, find all missing versions on the target branch;
  3. Try to apply the missing versions one by one to the target branch.

4.2 Rebasing in case of version conflicts

If you put a commit version or onebranch Rebase a branch to a differentHEAD If you have a version conflict, you may encounter a version conflict. You must resolve the conflict and run the commandgit rebase --continue, the rebase can be completed.

This section will demonstrate how to complete the rebase in the case of conflicts. rebaseExample The branch has been rebased tostable-3.2 The example will recheck out a new branch from stable-3.1 and add arebaseExample Text files with the same branch name but different contentfishtank.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

The conflicts are as follows:

insert image description here

Change to the following content, save and close:

insert image description here

Add the merged files and continue rebasing:

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

The editor that opens is shown below:

insert image description here

Confirm, save and close, and you will see a prompt that the rebase is successful:

$ 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

Details are as follows:

insert image description here

It can be seen that this rebase only rebases the versions that the old branch has but the new branch does not have (that is, the new commit)。

In the git prompt for the first rebase interrupt, you can see two more options:

  1. git rebase --abort: As the name implies, interrupt rebase

  2. git rebase --skip: Skip the conflict and rebase directly, which will result in rebaseExample2 Abandon the newcommit, directly incorporated rebaseExample, the parent to which it points after rebasing, and rebaseExample Consistent:

    insert image description here


4.3 Interactive rebase of a specific version

This example uses the rebaseExample Based on branches, demonstrate how to use--interactive Mark, rebase the two in 4.1commit Rebase onto the remote tracking branchstable-3.1 superior:

$ 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

At this time, a commit list will be displayed in the new pop-up editor, ranging from rebaseExample andstable-3.1 All between can be rebased tostable-3.1 ofcommit Record. Keep the two new ones in the examplecommit, delete all the rest (i.e. lines 89 and before):

insert image description here

After saving, exit the editor and you can see the following output:

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

The results are as follows:

insert image description here

Example Extension

In this example, you can also quickly achieve the desired effect with one command:

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

The schematic diagram before and after the rebase is as follows:

insert image description here