In development, what happens when you encounter such a situation?

The website already has alipay online payment function, to add “wechat pay”, modified two files, weik.php, pay.php.

Halfway through, there was an urgent bug: Alipay could not modify the order status after payment. You need to fix this bug right away. The files you need to fix are ali.php, pay.php.

The problem is that the pay.php file has already been modified by you, and it is not finished yet. There must be something wrong with it. Go back to pay.php? Then all my work was for nothing.

At this point you will certainly think: when doing “wechat payment”, can the warehouse copy a copy, does not affect the content of the original warehouse, after modification, and then the copy of the modification merged in the past.

Ok, so now you have branching ideas.

The master, seen earlier, is the trunk branch of code.

In fact, in actual development, instead of making changes and committing directly to the master branch, a dev branch is created, where the tests are modified, and the dev branch is merged into the Master branch.

If we had branches, we would have solved the problem.

When we do “wechat pay”, we create a wechat branch and commit the wechat branch. At this point, the content of the Master branch will not change, because the branches are different.

When an urgent bug is encountered, create an AliBug branch, fix the bug, and merge the AliBug branch to the Master branch. Then we will develop the function of “wechat payment”. After the development, we will merge the wechat branch into the Master branch.

Let’s talk about it in detail.

View all branches

git branch
Copy the code

There are only master branches, and you are currently on the master branch. The asterisk (*) indicates the current location. Create a branch

git brand wechat
Copy the code

At this point we look at the branchesNow we have two branches, but we’re still on the master branch. Next we switch branches.

git checkout wechat
Copy the code

Now we are on the wechat branch. Next, we will make some modifications on the wechat branch.Now the config.txt file has been modified, but there is an urgent bug on the master branch that you need to deal with. In this case, you need to save your work and deal with the bug. We enter

git add .
git commit -m "wechat todo"
Copy the code

In this way, we have saved our work on the wechat branch. Next, let’s switch back to the master branch.

git checkout master
Copy the code

At this point, something magical happens. Look at your working directory and the content you added is now missing. That’s what we want. Next, we’ll create another branch to fix the bug.

git branch ali
Copy the code

Then switch to the ALI branch

git checkout ali
Copy the code

Under this branch, we do something, create a file in our working directory, and then write something arbitrary.Assuming we have fixed the bug at this point, we commit the changes to the branch, and then merge the branch to the master branch. Let’s switch to the master branch and see that the file we just created disappears in the working directory, and then type

git merge ali
Copy the code

At this point, file changes made on the ALI branch can be found in the working directory. At this point, we can go back to the wechat branch and finish the unfinished work. When this is done, we can merge this branch into the master branch as well.

Delete the branch

git branch -d wechat
Copy the code

Note that when you make changes to the same content on both branches, merging on the master branch will cause conflicts because Git can’t determine which version you want.TXT, conflict (contents): merge conflict in config.txt, automatic merge failure; Fix the conflict, and then commit the result. Let’s look at the conflicting files.So let’s resolve the conflict, if we need all three values, we just delete the tags, and if we don’t need any of them, we delete the ones we don’t need, and I’m just going to keep all of them.After the modification is complete, we can submit it again.