Eliminate merge records in the same branch (GIT pull — rebase)

Time:2021-5-9

Question:

When multiple people work on the same development branch, there will be some redundant and unacceptable history

 Merge branch 'feature/x' of github.com:xxx/learn-git into feature/x

The translation is to branch the remote warehousefeature/xMerge to local branchfeature/x

emmm…

We are all in the same branch. What’s good for merging our own branch and our own branch (remote branch and local branch)?

Let’s take a look at what operation caused this situation

The problem recurred

One day the company needs to develop a new function

The developers are Xiao Ming and Xiao Hong

Xiao MingcongmasterPull a branch out, the branch name isfeature/x

Since then, Xiaoming and Xiaohong have developed happily in this branch.

If there is a small change, it will be passedgit commitSubmit

Xiao Ming’s log before pushing is like this

*2a68d81 (head - > feature / x) submitted by Xiaoming for the third time
*53b261a Xiaoming's second submission
*71a5262 submitted by Xiao Ming for the first time
* 3021c2e (origin/master, origin/feature/x, master) first commit

The log before Xiaohong push is like this

*0266f4f (head - > feature / x) Xiaohong submit for the second time
*260fa38 Xiaohong submitted for the first time
* 3021c2e (origin/master, origin/feature/x, origin/HEAD, master) first commit

Xiaoming took the lead in pushing

Xiaohong wants to push after the change

At this time, the remote end has been updated (Xiao Ming’s push), so Xiao Hong must first pull the code (GIT pull) to push

*   17771aa (HEAD -> feature/x) Merge branch 'feature/x' of github.com:zq741235/learn-git into feature/x
|\  
|* 2a68d81 (origin / feature / x) submitted by Xiaoming for the third time
|* 53b261a submitted by Xiaoming for the second time
|* 71a5262 submitted by Xiaoming for the first time
*| 0266f4f Xiaohong's second submission
*| Xiaohong submitted for the first time
|/  
* 3021c2e (origin/master, origin/HEAD, master) first commit

(here are some questions)How to avoid such merging?

If you use rebase mode operation at this time, it will be another scene:

$ git pull --rebase

The results are as follows

*9cd6d58 (head - > feature / x) Xiaohong submitted for the second time
*01cd7cf Xiaohong submitted for the first time
*2a68d81 (origin / feature / x) submitted by Xiaoming for the third time
*53b261a Xiaoming's second submission
*71a5262 submitted by Xiao Ming for the first time
* 3021c2e (origin/master, origin/HEAD, master) first commit

What’s the difference between the two ways?

In addition to the fact that the history has become a straight line, the visible difference is that the hash value of Xiaohong’s submission record has changed.

Let’s take a lookgit pullandgit pull --rebaseWhat’s the difference?

git pull

  1. git fetch
  2. git merge FETCH_HEAD

git pull –rebase

  1. git fetch
  2. git rebase FETCH_HEAD

Just use rebase instead of merging

other

Everybody’s rightrebaseMost of the reasons are that hearsay has side effects. And then I hold it. I don’t need it anywayrebaseWe should continue to avoid the policy of living well.

Let’s analyze the above situation

Little red hash value changed, what will be the impact, first to a soul torture

  1. How many times does the hash value of Xiaohong change?
  2. Has it been pushed to the remote warehouse?

As shown above, the hash value of Xiaohong changed to its own local 2-time submission, and was not pushed.

Are there any side effects? can’t.

Under what circumstances will there be side effects?

Take chestnuts

Er…

In short, changes that do not affect others (do not change the submission that has been pushed to the remote repository) have no side effects.

The side effects will be updated in the following series.