2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
【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
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.
commit
Rebase the version to another branchLet'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.
Before rebasing:
After rebasing:
git rebase
The execution process:
HEAD
The common version between the target branch pointed to by the rebase (merge-base
);merge-base
, find all missing versions on the target branch;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
The conflicts are as follows:
Change to the following content, save and close:
Add the merged files and continue rebasing:
$ git add fishtank.txt
$ git rebase --continue
hint: Waiting for your editor to close the file...
The editor that opens is shown below:
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
Details are as follows:
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:
git rebase --abort
: As the name implies, interrupt rebase
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:
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
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):
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:
The results are as follows:
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.
The schematic diagram before and after the rebase is as follows: