This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

preface

How can you be a coder and not master Git? However, xiao Yue, a new employee, often uses Git incorrectly, especially Git rebase recommended by colleagues, so she comes to learn!! Here is xiao Yue’s study notes, goal: master Git, unlimited power!

Git rebase (rebase)

Base base

background

Tsuki created her own development branch feat/ Test based on the Master branch and submitted a new update C4. When she tried to merge her branch into the Master branch, she found that the new update C3 was also submitted on the Master branch. The merger failed.

plan

Git rebase can move the commit point of the target base branch to the current branch, as if the current branch had been developed on the base branch with the latest commit point.

git checkout feat/test
git rebase master
Copy the code

The principle is to first find the C2 of the two branches (feat/test and master of the target base), then compare the previous commits of the current branch to this ancestor, extract the corresponding changes and store them as temporary files, and then update C3 by pointing the current branch to the target base. Finally, the commit points of the previously stored temporary files are applied in sequence.

Now you can successfully merge the branches.

git checkout master
git merge feat/test
Copy the code

Although the actual development work is parallel, with the Rebase operation, the commit record for the master after merging branches is a neat straight line without forks. This is one of the advantages of rebase operations: it ensures that commit records are kept clean when pushed to remote branches.

Multi-topic branch base

background

Satsuki created her own development branch feat/ Server based on the Master branch, submitted a new update C4, and then created her own development branch feat/ Client based on her development branch Feat/Server. New updates C5 and C7 have also been committed. At the same time, a new update C3 has been committed on the Master branch and a new update C6 has been committed on the Feat/Server branch. Satsuki wants to merge his branch with the master branch, but satsuki wants to merge his branch with the master branch. How to use rebase in this case?

plan

You can use the –onto option of the git rebase command to select changes that are in the feat/ Client branch but not in the feat/ Server branch.

git checkout feat/client
git rebase --onto master feat/server feat/client
Copy the code

What happens if you don’t use the –onto option?

git checkout feat/client
git rebase master
Copy the code

A change has been made in the feat/ Server branch.

You can merge feat/ Client branches into the Master branch by using the –onto command to update the base branch of feat/ Client to the latest branch of Master.

git checkout master
git merge feat/client
Copy the code

At this point, Satsuki found that the code on his branch feat/ Server had passed the test and wanted to merge into the Master branch as well. She can base the master branch as the target base branch and then merge it with the Master branch.

git checkout feat/server
git rebase master
git checkout master
git merge feat/server
Copy the code

Let’s take a look at the final commit record on the Master branch!

Perfect! Nice!

Considering the actual situation, this kind of rebasing is usually not recommended and is prone to conflict! Unless it is confirmed that the new commit points C5 and C7 are not related to the changes in commit point C4 or are less related.

The risk of becoming homogenous

The essence of the rebasing operation is to discard some existing commits and create some new commits that are the same but actually different. If you’ve pushed a commit to a repository and someone else has pulled a commit from that repository and done subsequent work, if you reorganize your commit with git rebase and push it again, your peers will therefore have to integrate their work with your commit again. If you then have to pull and integrate their modified commits, things get messy.

As a general rule, only base cleanup history on local changes that haven’t been pushed or shared with others, and never base commits that have been pushed elsewhere, so you can get the benefit of both approaches.

The code pulls the base

background

When Xiao Yue thought he had mastered git rebase, there was an unexpected problem: Xiao Yue based on the remote branch Origin /master to generate its own local branch local/master for development. When she tried to push her commit point to the remote branch, git push failed. The remote branch has a new commit point that needs to be synchronized to the local branch. Xiao Yue uses Git pull to get the latest commit point from the remote branch. However, when you use git log to check the commit record, you find that there is another commit point of merge. Xiao Yue does not want this commit point of merge.

plan

All you need to do is add the rebase parameter to git pull.

git pull --rebase
Copy the code

Interactive base change

background

When Xiao Yue thought she had mastered the skill of Git rebase, there was another unexpected problem: Xiao Yue’s teacher X put forward some suggestions when reviewing her code, requiring Xiao Yue to merge multiple submission points C1 and C4 into one submission point C1′, so that the submission record could be clearer and cleaner. Xiao Yue continues to learn!

plan

In this case, multiple commit points can be combined into a single commit point using git Rebase’s interactive mode. There are two cases: merge multiple commit points from the last commit point; Merges the commit points for the specified interval.

If you want to merge commit point C1 to C4 as the new commit point C1′, there are two ways:

Git rebase -i HEAD~4 git rebase -i HEAD~4Copy the code
Git rebase -i C0_ID git rebase -i C0_ID git rebase -i C0_IDCopy the code

If you want to merge commit points C1 to C3 as the new commit point C1′, you can do this by specifying an interval:

Git rebase -i C0_ID C3_ID git rebase -i C0_ID git rebase -i C0_IDCopy the code

-i stands for –interactive. The following operations can be performed on an operation window: Pick, reword, Edit, Squash, fixup, exec, and drop.

In general, the fixup operation is recommended.

conclusion

After a series of learning, Xiao Yue thinks she is comfortable with git Rebase. Specific is not, let us look forward to it ~