Command Format
Let's take a look at the Command Format of Git-rebase:
Git rebase [-I | -- Interactive] [Options] [-- onto <newbase>]<Upstream> [<branch>]
Git rebase [-I | -- Interactive] [Options]-onto <newbase>-Root [<branch>]
Git rebase-continue |-Skip |-Abort
From the command format, you can see the git-rebae commandAt leastYou need a parameter <upstream>, which can be a branch name or a valid commit.
A small place
When you decide to learn this command, you should first note that if a parameter is added after Git-rebase <Branch>, Then Git-rebase will execute git checkout <branch> before any other action. If the parameter <branch> is not added, Git-rebase will act on the current branch.
Command usage
The git-rebase command is used to obtain the latest commit information from the upstream branch and organically merge the current Branch and the upstream branch.
This is a simple introduction. Readers may not be able to understand its purpose and benefits.
We still need to use examples to talk about it. Assume that the master branch is the master, and a new branch topic is generated during development. The master node is called the upstream branch of the topic.
Example:
[Rocrocket @ ABC Git-study] $ CD rebase
[Rocrocket @ ABC rebase] $ ls
[Rocrocket @ ABC rebase] $ VI Roc. c
[Rocrocket @ ABC rebase] $ cat Roc. c
Int main ()
{
Printf ("MASTER: 001 ″);
Return 0;
}
[Rocrocket @ ABC rebase] $ git init
Initialized empty git repository in/rocrocket/career/programming/Git-study/rebase/. Git/
[Rocrocket @ ABC rebase] $ git add.
[Rocrocket @ ABC rebase] $ git commit-M "master: 001 ″
Created initial commit 2d89602: MASTER: 001
1 files changed, 5 insertions (+), 0 deletions (-)
Create mode 100644 Roc. c
[Rocrocket @ ABC rebase] $ git log
Commit 2d89602d0c99551_df0d2c023e447f5d98d863a
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:26:40 2008 + 0800
MASTER: 001
[Rocrocket @ ABC rebase] $
At this point, we have completed a commit in the master branch.
[Rocrocket @ ABC rebase] $ VI Roc. c
[Rocrocket @ ABC rebase] $ git commit-a-m "master: 002 ″
Created commit 41b3d1c: MASTER: 002
1 files changed, 1 insertions (+), 0 deletions (-)
[Rocrocket @ ABC rebase] $ cat Roc. c
Int main ()
{
Printf ("MASTER: 001 ″);
Printf ("MASTER: 002 ″);
Return 0;
}
[Rocrocket @ ABC rebase] $ git log
Commit 41b3d1cfaae0184bb8e5f27a165d51cc23867413
Author: rocrocket <wupengchong@gmail.com>
Date: Mon Nov 17 15:28:01 2008 + 0800
MASTER: 002
Commit 2d89602d0c99551_df0d2c023e447f5d98d863a
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:26:40 2008 + 0800
MASTER: 001
[Rocrocket @ ABC rebase] $
So far, we have completed two commit commits in the master branch.
The current branch structure is as follows:
MASTER: 001-MASTER: 002 (MASTER)
Okay. What we need to do is to create a new branch topic.
[Rocrocket @ ABC rebase] $ git Branch
* Master
[Rocrocket @ ABC rebase] $ git branch topic
[Rocrocket @ ABC rebase] $ git Branch
* Master
Topic
[Rocrocket @ ABC rebase] $ git checkout topic
Switched to branch "topic"
[Rocrocket @ ABC rebase] $ git Branch
Master
* Topic
[Rocrocket @ ABC rebase] $
Now, we have successfully created a topic branch and transferred it to the topic branch.
Next, the development of the topic is as follows:
[Rocrocket @ ABC rebase] $ VI Roc. c
[Rocrocket @ ABC rebase] $ git commit-a-m "Topic: 001 ″
Created commit d599b54: Topic: 001
1 files changed, 1 insertions (+), 0 deletions (-)
[Rocrocket @ ABC rebase] $ VI Roc. c
[Rocrocket @ ABC rebase] $ git commit-a-m "Topic: 002 ″
Created commit 3f4b17f: Topic: 002
1 files changed, 1 insertions (+), 0 deletions (-)
[Rocrocket @ ABC rebase] $ cat Roc. c
Int main ()
{
Printf ("topic: 002 ″);
Printf ("topic: 001 ″);
Printf ("MASTER: 001 ″);
Printf ("MASTER: 002 ″);
Return 0;
}
[Rocrocket @ ABC rebase] $ git log
Commit 3f4b17fe3b5d277771770c0515e75f04e783ad14
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:49:24 2008 + 0800
Topic: 002
Commit d599b54336ad96b8e09ef92e371a09a25e6d0c11
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:48:58 2008 + 0800
Topic: 001
Commit 41b3d1cfaae0184bb8e5f27a165d51cc23867413
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:28:01 2008 + 0800
MASTER: 002
Commit 2d89602d0c99551_df0d2c023e447f5d98d863a
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:26:40 2008 + 0800
MASTER: 001
[Rocrocket @ ABC rebase] $
We can see that since the topic branch is established and switched to, the topic has been committed twice and added a line of code each time.
In this case, the branch structure should be as follows:
Topic: 001 --- topic: 002 (topic)/master: 001 --- master: 002 (MASTER)
The figure shows the direction of the branch.
Next, the master Branch also has its own progress, as shown below:
[Rocrocket @ ABC rebase] $ git checkout master
Switched to branch "master"
[Rocrocket @ ABC rebase] $ git Branch
* Master
Topic
[Rocrocket @ ABC rebase] $ VI Roc. c
[Rocrocket @ ABC rebase] $ git commit-a-m "master: 003 ″
Created commit 91a7ffc: MASTER: 003
1 files changed, 1 insertions (+), 0 deletions (-)
[Rocrocket @ ABC rebase] $ VI Roc. c
[Rocrocket @ ABC rebase] $ git commit-a-m "master: 004 ″
Created commit b81fbc3: MASTER: 004
1 files changed, 1 insertions (+), 0 deletions (-)
[Rocrocket @ ABC rebase] $ cat Roc. c
Int main ()
{
Printf ("MASTER: 001 ″);
Printf ("MASTER: 002 ″);
Printf ("master: 003 ″);
Printf ("MASTER: 004 ″);
Return 0;
}
[Rocrocket @ ABC rebase] $ git log
Commit b81fbc3be5c7bd1fdef72820C29e2c67590f4b03
Author: rocrocket <wupengchong@gmail.com>
Date: Mon Nov 17 15:55:23 2008 + 0800
MASTER: 004
Commit 91a7ffc73e6320a86b10849061efd672f47fd5bd
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:55:06 2008 + 0800
MASTER: 003
Commit 41b3d1cfaae0184bb8e5f27a165d51cc23867413
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:28:01 2008 + 0800
MASTER: 002
Commit 2d89602d0c99551_df0d2c023e447f5d98d863a
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:26:40 2008 + 0800
MASTER: 001
[Rocrocket @ ABC rebase] $
As you can see, the master Branch has also completed two commit submissions and added a line of code each time.
By now, the branch structure is:
Topic: 001 --- topic: 002 (topic)/master: 001 --- master: 002 --- master: 003 --- master: 004 (master)
At this time, our experiment sample has been basically set up, and git-rebase will be useful!
Assume that topic and master are two branches of a project, Master is the master branch, and topic is the bypass branch. In most cases of software development, the bypass branch must follow the master branch. Therefore, the topic Branch wants to import the latest code developed by the master branch to the topic branch, and this action is required not to affect the development of the master branch, that is, it must be done secretly. Git-rebase is on stage:
[Rocrocket @ ABC rebase] $ git checkout topic
Switched to branch "topic"
[Rocrocket @ ABC rebase] $ git Branch
Master
* Topic
[Rocrocket @ ABC rebase] $ git rebase master
First, rewinding head to replay your work on top of it...
Applying topic: 001
Error: patch failed: Roc. C: 1
Error: Roc. C: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merged Roc. c
Applying topic: 002
[Rocrocket @ ABC rebase] $
We use git rebase master to fulfill our needs. If you are careful, you will see that it outputs some errors, which means "patch failure". This error does not affect the normal operation of Git-rebase.
Let's take a look at the magic of Git-rebase:
[Rocrocket @ ABC rebase] $ git Branch
Master
* Topic
[Rocrocket @ ABC rebase] $ git log
Commit 05de9849078541c86cf5182cD8c15fa22bd00f77
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:49:24 2008 + 0800
Topic: 002
Commit 7e5a744ef9e0740b4a091e9c8baa859b14800b0b
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:48:58 2008 + 0800
Topic: 001
Commit b81fbc3be5c7bd1fdef72820C29e2c67590f4b03
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:55:23 2008 + 0800
MASTER: 004
Commit 91a7ffc73e6320a86b10849061efd672f47fd5bd
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:55:06 2008 + 0800
MASTER: 003
Commit 41b3d1cfaae0184bb8e5f27a165d51cc23867413
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:28:01 2008 + 0800
MASTER: 002
Commit 2d89602d0c99551_df0d2c023e447f5d98d863a
Author: rocrocket <abc@gmail.com>
Date: Mon Nov 17 15:26:40 2008 + 0800
MASTER: 001
[Rocrocket @ ABC rebase] $ cat Roc. c
Int main ()
{
Printf ("topic: 002 ″);
Printf ("topic: 001 ″);
Printf ("MASTER: 001 ″);
Printf ("MASTER: 002 ″);
Printf ("master: 003 ″);
Printf ("MASTER: 004 ″);
Return 0;
}
[Rocrocket @ ABC rebase] $
See it! The master: 003 and master: 004 developed by the master Branch have quietly entered the log of the topic branch. The ROC. c file also has the corresponding development code.
Now, do you have a sense of openness? Let's take a look at the branch structure:
MASTER: 001 --- master: 002 --- master: 003 --- master: 004 --- topic: 001 --- topic: 002
(Topic)
MASTER: 001 --- master: 002 --- master: 003 --- master: 004 (master)
This is the magic of Git-rebase! What's amazing?
If you forget it, compare it. Here is the branch structure before executing Git-rebase:
Topic: 001 --- topic: 002 (topic)/master: 001 --- master: 002 --- master: 003 --- master: 004 (master)