0 foreword


When I first started working with Git, I knew just the usual commands: pull, add, commit, push. After graduation and working in the large-scale cooperation of the company, it is urgent to have a further understanding of Git. Then I systematically studied Git. Finally to these commonly used commands know it, know its why, at the same time, also learned to master some senior commands. Among them, the rebase command makes me feel that I have discovered the new world of Git. This is to record the relevant knowledge, in order to make records, but also can learn from each other with everyone.

1 what is rebase


Rebase, which translates directly to rebase, is exactly what the command is called. With the rebase command, we can change the focal point of a sequence of commits (parent COMMIT). First let’s go through this command and see what it does.

Suppose our Git structure looks like this. We opened a branch branch1 on Commit3, then developed and committed two Commits (6 and 7) on Branch1. At the same time, the merging of other branches into the Master branch resulted in two more commits for the Master branch (4 and 5).

We execute the following command: (Note: the active branch is branch1)

(branch1) git rebase master

The git structure is as follows:

As you can see, before using the rebase command, branch1 and master are commit3, which is the “basis point” for branch1. By executing git rebase, branch1 has a “base point” of 5. This changes the branch/master crossover point, now commit5.

Here’s how rebase works: Git causes the commit sequences we want to move (Commit6 and COMMIT7) to be replayed in the same order on the target branch (Master). This is equivalent to making a copy of each commit with the same set of changes, author, date, and comment information. Commit6 (Branch1) and COMMIT7 (Branch1) are historical versions of commit6 and COMMIT7 (Branch1). We can find them by the SHA-1 sequence number of the commit. Only after garbage collection is performed with the GC command (which Git does automatically to clean up the repository) do they truly disappear from the repository.

The steps for rebase are as follows:

  1. Determine which commits are involved: Identify the active branch (Branch1) and which commits are not on the target branch (master). Commit6 and COMMIT7 are the commits to be refocused.
  2. Identify the new base point: The new base point is the latest commit of the target branch, commit5.
  3. Copy Commit: Copy the commits that need to be refocused one at a time and create new commits (COMMIT copy 6 and Commit copy 7), in this order
  4. Reset the active branch: The active branch is moved to the top of the above replication commit, in this case commit 7.

From the above demonstration, you should have a basic understanding of Rebase. Here’s when to use it.

2 When to use Rebase


  1. This is one of the most important reasons to use Rebase, rebase and merge code, so that our master branch does not cross, just a line down, so it is easy to maintain and manage.
  2. Migrate branches. You can migrate some commits on one branch to another branch
  3. With the advanced use of Rebase, interactively rebase, you can modify, merge, and so on branches that need to be moved (such as COMMIT6 and COMMIT7 above).

3 How to use Rebase


  • Merge development branches into Master, purify history

When we finish developing a feature on the feature branch, we need to merge it into the master branch, continuing with the example above:

(branch1) git checkout master

(master) git merge branch1

After executing the above commands, git has the following structure:

After the merge, a new commit node (COMMIT7) is created. So you have the crossover, you have the diamond chain. Makes the Master branch look messy and difficult to maintain and manage.

Let’s look at the effect of using the rebase command. Run the following command:

(branch1) git rebase master

Note: The rebase command executes on the active branch, whereas the merge command switches to the branch you want to merge. So the rebase command is executed under branch1, while the merge command is executed under master. That’s one of the differences

After the preceding command is executed, the effect is as follows

This is branch1 not yet merged into master. You also need to switch to the master branch and run the merge command. Run the following command:

(branch1) git checkout master

(master) git merge branch1

Now that the merge operation is complete, look at the result:

The merge above is a fast-forward merge and does not result in a new commit. If you need to generate a new commit (for better record tracking), you can use the –no-ff option, which is supported in newer git versions but not necessarily older ones: git merge –no-ff branch1

Commit6 and COMMIT7 will be cleared later. The master branch is thus a single line, clean and easy to maintain and manage.

Advanced use of rebase


The rebase command also has some options that allow us to do more complex things. Here are two –onto and — Interactive.

Use — Onto port submission

In the following Git structure, we want to merge both branch1 and Branch2 branches into master. We can merge commit6 and COMMIT7 from branch1 to Branch2, and then merge Branch2 to Master. This is called a migration commit. You can merge COMMIT6 and COMMIT7 into BRANch2.

  1. First cut the active branch to branch1

git checkout branch1

  1. Perform migration

Git rebase master — branch2 copy commit from branch1 to branch2

This allows the commit from Branch1 to be migrated to Branch2.

Use –interactive to do more

Adding the –interactive option to rebase makes it interactive rebase, which stops in the middle of the rebase (copy commit) process and gives us a selection of commands to do different things.

For example, we develop a feature in the feature branch that makes multiple commits, resulting in multiple commits. When the branch is merged into the master, only a COMMIT is generated, so use the –interactive option. Let’s simulate this:

Note: Before performing a local rebase, it is recommended to pull the remote and local code to be consistent. After rebase, the local commit structure will change. If you want to synchronize the remote, you need to force the pull, then rebase. Then use git push –force-with-lease to force local overwrites on the remote. If you don’t understand it here, look down.

  1. First, open a new feature branch from the master branch

git checkout -b feature

  1. Mock development, multiple commits, three of which were simulated here

    Let’s look at the feature branch log situation

Three branches were committed. Let’s look at the master branch:

Master update commit “Merge branch ‘test1′”

Next, let’s do the merge:

  1. Switch the active branch to feature

git checkout feature

  1. Perform interactive rebase -i is short for –interactive

git rebase master -i

After executing this command, the base change operation enters the interactive state:

As you can see, all commits that need to be regrounded are listed above, from top to bottom, and also from front to back, with the first commit shown above. Next, there are seven commands for interactive rebasing:

  • Pick is also the default, so use the commit, copy it over, and base it normally
  • Reword uses the submission, but can modify the submission information of the submission, when using this command is, it will then go to a page that lets us write the submission information. We can use this method if we want to modify the commit information for that commit
  • editWith the commit, and the base change is interrupted, we can modify the workspace filegit add . => git commit --amendResubmit. Use after submissiongit rebase --continueLet’s keep going. You can use this method if you want to modify a commit
  • Squash merges commit, it merges into the previous commit, and then lets us reedit the commit information, which is what we’re going to do.
  • Fixup also merges the submissions, but instead of reediting the submissions, we discard the submitted submissions
  • Drop discards the commit, that is, the commit is not copied over.
  • Exec executes shell commands

Now that we know about these commands, let’s move on to the task we just did, as mentioned above, where we used the sqash command to merge the commit and reedit the commit information.

Git add. => git rebase –continue

The rebasing is done and not merged into the master.

Note: The local repository of the feature branch is changed after the base change, and the three COMMITS are merged into one. If you have committed the local branch to the remote, the remote feature branch still has three commits. If you commit a local branch to a remote branch and merge it into the master branch via the Open Merge Request, you should be careful. The git pull command cannot be executed. Use git push –force-with-lease. The local is forced to overwrite the remote, so that the local and remote branches are synchronized.

The forced commit here takes two parameters, –force and –force-with-lease. These two specific use and introduction, I suggest you Google, after a detailed understanding of the use. The comparison function is more powerful.

Portal (why not use –force-with-lease instead of –force)

In real development, the same Create Merge Request is usually merged into the master for code review. In this case, we need to push the local branch to the remote side to initiate the merge. Once this step is complete, as mentioned above, the remote repository is different from the local repository, which requires you to perform the above operations, overwrite the remote, and then initiate the merge.

git push –force-with-lease

If you can merge to master locally, it’s much easier.

  1. Switch to the master

git checkout master

  1. Merge operation

git merge feature

At this point, the task is complete, but it is necessary to remind you that if you have committed to the remote feature branch, the remote feature branch will be different from the local feature branch. If you want to synchronize the remote feature branch, you must force push. Next, let’s look at the master log

git log –graph

There is only one submission. If the master has a new submission during feature development. There’s no crossover, so I’m not going to show you.

5 concludes


Rebase is also a powerful command, and some people are afraid to use it because it’s too powerful, for fear of something going wrong, but when you really understand how these commands work, there’s nothing to be afraid of because it’s all under your control. Master the command rebase -i, I believe that the understanding and use of Git will be a ladder, really more powerful and easy to use, master it, use it well, will help us improve work efficiency.

That’s it! Welcome to leave a message.