Git rebase

preface

I don’t know much about Git rebase, and I want to improve the quality of git submission these days, so I found this useful command. By the way, I will record it to deepen my memory

Post the official documentation so you can learn more about Git

What rebase is

  • rebaseRebase is officially defined as moving your branch root node to maintain a better commit record.rebaseMerging your current branch with another branch puts the other branch’s commit records at the beginning of our current branch timeline. That is, our commit records will be consolidated behind the common branch.
  • In simple terms, merging other local branches can be used when merging records so as not to create redundant forksrebase.
  • So let’s seerebaseWhat are the application scenarios and use skills andmergeThe difference.

Rebase vs. Merge

  • rebaseWill put your current branchcommitPut it at the end of the common branch, that’s why it’s called rebasing. It’s like you’re pulling out the branch from the common branch.
  • mergeWill link the public branch to your currentcommitMerge together to form a new onecommitTo submit.

Application scenarios

  • When you just join the company, the technical leader asks you to pull out a branch based on the master branch to develop requirements. The master branch submits records A and B. You have committed 2 times on the dev branch as e and F, and another colleague has committed 2 times on the master branch as C and D.

  • The current branch status node is shown in the figure below:

Use Git merge to merge

$git log --graph --oneline Pay attention not to tube * * said a commit where a branch line | said forward/said bifurcate \ indicated intoCopy the code

conclusion

    • As shown abovemergeIt merges the two branches together to form a new onecommitSubmit records.
    • We found thatcoomitCommit record is to put the merged branch record into our currentdevAfter the branch record.
    • andcoomitRecord Submission Meetingbifurcation.

Use Git rebase for merge operations

$git rebase --graph --oneline Pay attention not to tube * * said a commit where a branch line | said forward/said bifurcate \ indicated intoCopy the code

conclusion

    • First of all, we found that so fardevThe commit record on the branch isabcdefDid not likemergeGenerates a new commit record as well
    • The secondrebase masterBranch todevBranch,devThe branch history is added to themasterThe back of the branch.
    • As shown in the figure, the history is recorded as a line, which is very neat, and does not end up forking the commit record like the merge.

Application scenarios of Rebase

  • With Rebase in mind, let’s take a look at some of the actual scenarios we can use.

The scene of a

  • Classic scenario, optimizing local commit records to reduce forks.
  • As with the classic case 👆 above, the description is not repeated here

Scenario 2

  • Continuity conflict
  • At this point you pull a dev branch from the master branch to make the required changes to a functionality based on version V1, When the project manager divided the functions, he asked your colleague to also rectify a public page A in the function A of the Master branch. Later your colleague completed the correction, submitted the X version and merged it and pushed it to the master remote branch. At this point, we completed a development in the Dev branch and submitted version V2. The product manager came to us and said, requirements have changed, we need to make changes again. Then we made changes based on v2 and submitted version V3. After a while the test came up with another requirement suggestion. We then made changes based on the current DEV branch V3 version and committed a V4 version. At this point, let’s assume that page A has been modified three times. The colleague modified it once and pushed it to the remote master. So if we merge the master branch it’s going to collide.
  • That’s the short answer.Remote branchThe master commits to file A once, while the other dev commits to file A three times, butThe local branchDev commit = 1 commit = 1 commit
Use Git rebase to resolve conflicts
$git rebase origin/ $git rebase origin/ $git rebase origin/ / At this point we will have the first conflict between page A in the current dev branch version V2 and page A in the remote branch master. $git add. $git rebase continue $git add. $git rebase continue $git add. $git rebase continue $git add. $git rebase continue $git add. $git rebase continue $git rebase --graph - oneline / / / / to check the log some chart symbol explanation: * indicates a commit, don't tube where * is a branch line | said forward/said bifurcate \ indicated intoCopy the code

conclusion

    • Not because like usemergeA new commit record is generated when a synchronization code encounters a conflict
    • You only need to solve one conflict with merge, which is simple and cruderebaseBefore submitting a file, resolve each conflict one by one.
    • userebaseCommit records do not fork; one line is clean and tidy
    • After the conflict is resolved, usegit add To mark that the conflict has been resolved, and finally executegit rebase --continueTo continue. If you encounter a patch that does not need to be applied, run the following command to ignore it:git rebase --skip
    • If you want to go backrebaseState before execution. You can execute:git rebase --abort

Scenario 3

    • You develop a requirement product that changes repeatedly, causing your commit record to repeat function points multiple times
    • In daily development, it is inevitable that there will be duplicate COMMIT records. At this point, we want to optimize the submission record. How do we do that
  • The current branch status node is shown in the figure below:

Use Git rebase to merge commit records
$git rebase -i HEAD~x $git log --graph --oneline Pay attention not to tube * * said a commit where a branch line | said forward/said bifurcate \ indicated intoCopy the code
  • Git rebase -i HEAD ~ 3: git rebase -i HEAD ~ 3

  • At this point we will create a new commit record and select to fill in the commit information and delete the merged commit record

  • Our merger is hereby closed.

conclusion

    • Redundant commit records are merged and a new commit record is generated
    • Make submission records look cleaner and easier for colleagues to check

conclusion

  • We found that sharingrebaseIt’s all aboutOptimize the branch commit recordTo give you an example of this command, I personally think this is the core of this command.
  • In the studyrebaseI used to use it on a daily basismergeLead tocommitThe record is too chaotic, I have to find the submission record of which function node I want for a long time, and I will try to use it in the project gradually in the future. After all, who doesn’t want a neat submission record 😁

reference

  • Git merge and rebase

  • # Common conflicts and solutions from Git rebase