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
rebase
Rebase is officially defined as moving your branch root node to maintain a better commit record.rebase
Merging 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 forks
rebase
. - So let’s see
rebase
What are the application scenarios and use skills andmerge
The difference.
Rebase vs. Merge
rebase
Will put your current branchcommit
Put 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.merge
Will link the public branch to your currentcommit
Merge together to form a new onecommit
To 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 above
merge
It merges the two branches together to form a new onecommit
Submit records.
- As shown above
-
- We found that
coomit
Commit record is to put the merged branch record into our currentdev
After the branch record.
- We found that
-
- and
coomit
Record Submission Meetingbifurcation
.
- and
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 far
dev
The commit record on the branch isabcdef
Did not likemerge
Generates a new commit record as well
- First of all, we found that so far
-
- The second
rebase
master
Branch todev
Branch,dev
The branch history is added to themaster
The back of the branch.
- The second
-
-
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 use
merge
A new commit record is generated when a synchronization code encounters a conflict
- Not because like use
-
- You only need to solve one conflict with merge, which is simple and crude
rebase
Before submitting a file, resolve each conflict one by one.
- You only need to solve one conflict with merge, which is simple and crude
-
- use
rebase
Commit records do not fork; one line is clean and tidy
- use
-
- After the conflict is resolved, use
git add
To mark that the conflict has been resolved, and finally executegit rebase --continue
To continue. If you encounter a patch that does not need to be applied, run the following command to ignore it:git rebase --skip
- After the conflict is resolved, use
-
- If you want to go back
rebase
State before execution. You can execute:git rebase --abort
- If you want to go back
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 sharing
rebase
It’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 study
rebase
I used to use it on a daily basismerge
Lead tocommit
The 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