编程知识 cdmana.com

Git (4) -- merge and rebase

After the study of the previous theory , Believe in git We have a more in-depth understanding of the model of , Before explaining the common development commands , Let's take a look at git The overall operation process

git technological process

We follow a normal development process to learn , We're going to start developing , First of all, we need to synchronize the remote warehouse to the local

git clone https://github.com/generalthink/git_learn.git

Run the command above , There will be one in our current working directory git_learn Folder , There is .git Catalog (git Maintain the basics of the warehouse ) And the same files as the remote repository , Now our code environment is consistent with the remote repository , We can start our development process .

Development process

Generally speaking , Whether it's developing new features (feature) Or modify bug(issue) None of us will develop directly on the backbone , It's going to be developed on branches , After verification, it is synchronized to master.
Now suddenly there's a bug We need to open a new branch to solve this problem bug, So what should we do ?

First , Create a new one bugFix Branch , And switch to this branch

git branch bugFix
git checkout bugFix
 perhaps 
git checkout -b bugFix

git checkout

Create a new branch , It's just a pointer to the latest submission , All subsequent commits are based on the current branch , The asterisk represents the current branch .

then , find bug Why , Modify the corresponding file , Now we've modified a few files , Need to submit it to our local warehouse , Synchronization to remote warehouse will be explained in the following article .

  1. Add the modified file to the temporary storage area
git add *.java

The above operation is actually to make the file working directory and the version of the staging area consistent , But now it is not consistent with the version of the warehouse . If you want to see what documents exist that need to be submitted, you can use git status see .

  1. Submit documents to local warehouse
git commit -m "fix bug"

commit The order is actually to make index The version of the file in the temporary storage area is consistent with that of the warehouse , Go through this step , working directory , The versions of the staging area and the warehouse are the same , The following figure shows before and after submission commit objects( Check out the data model article ) The change of .

git commit

You are modifying bug At the same time , Someone else has submitted code for other functions to the main branch, or you already have two different branches locally , So fixed this bug after , Our warehouse looks like this

 chart

Merging branches

This bug After repair , It has been tested and verified that it has passed , And then you want to merge the code from this branch into the main branch ( Or merge two different branches ), The common ways to merge are 2 Kind of

git merge

take bugFix Merge into master On the branch

// Switch to master Branch 
git checkout master

git merge bugFix

git-merge-bugFix

See that? ,master Points to a commit record with two parent nodes , If from master Start looking up the arrow , On the way to the starting point, we will pass all the submission records , It means master Contains all changes to the code base .

At this time, if you still want to put master Branch into bugFix Branching is also possible

git checkout bugFix
git merge master

git-merge-master

because master Inherited from bugFix,Git Nothing to do , Just simply bugFix Move to master The submission record pointed to .
Now all submitted records are the same color , This means that each branch contains all the changes to the code base !

git rebase

The second way to merge branches is git rebase.Rebase It's actually taking out a series of submission records ,“ Copy ” they , And then put them in another place one by one .
Rebase The advantage is that you can create a more linear submission history .

git rebase master

git-rebase-bugFix

Now? bugFix The work on the branch is master Top of , At the same time, we also get a more linear submission sequence .
Be careful , Submit the record C3 There is still a ( The dotted node on the tree ), and C3' It is our Rebase To master On the branch C3 Copy of .
Now the only problem is master Not updated yet , Let's update it

git checkout master
git rebase bugFix

git-rebase-master

because bugFix Inherited from master, therefore Git It's just simple master The reference to the branch just moved forward .

版权声明
本文为[think123]所创,转载请带上原文链接,感谢

Scroll to Top