Creation and merging of branches

Time:2020-11-5

Creation and merging of branches

Now let’s take a simple example of branching and merging. This workflow is also used in actual work

1. Develop a website.
2. To implement a new requirement, create a branch.
3. Work on this branch.
Suppose you suddenly receive a call saying that there is a very serious problem that needs urgent repair, you can deal with it in the following ways:

1. Return to the branch that was previously published to the production server.
2. Create a new branch for this emergency fix and fix the problem in it.
3. After passing the test, go back to the branch where the production server is located, merge the repair branch, and then push it to the production server.
4. Switch to the branch that implemented the new requirement before and continue to work.

Branch creation and switch

First, let’s assume that you are working happily on a project and have submitted several updates (see Figure 3-10).

Git checkout - B iss53 // create and switch

These two commands are equivalent to the following:

git branch iss53
git checkout iss53

Before you do, however, pay attention to changes in your staging area or working directory that have not yet been committed. It will conflict with the branch you are about to check out, preventing git from switching branches for you. When switching branches, it is best to keep a clean working area. A few ways to circumvent this problem are described later (called staging and commit signing, respectively).
Git restores the contents of the working directory to a snapshot of the commit object it pointed to when a branch was checked out. It automatically adds, removes, and modifies files to ensure that the contents of the directory are exactly the same as when you submitted it.

It’s necessary to do some testing to make sure the fix is successful, then go back to the master branch and merge it in, and then publish it to the production server.
Git merge merge branch

git merge hotfix

Note that the prompt “fast forward” appears when merging. Since the commit object of the current master branch is directly upstream of the hotfix branch to be merged, GIT only needs to move the master branch pointer to the right directly. In other words, if you can go down one branch and reach another branch, GIT will simply move the pointer to the right when merging the two. Because there are no differences to be solved in the historical branch of this single line, this merging process can be called fast forward.

Use the – D option of GIT branch to delete

git branch -d hotfix

Merging of branches

git merge iss53

Git does a simple three-way merge with the ends of the two branches (C4 and C5) and their common ancestor (C2).
Instead of simply moving the branch pointer to the right, GIT takes a new snapshot of the merged results of the three parties and automatically creates a commit object (C6) pointing to it (see Figure 3-17). This submission object is special. It has two ancestors (C4 and C5).
It is worth mentioning that git can decide which common ancestor is the best combination basis;

Branch merge in case of conflict

If the same part of the same file is modified in different branches, GIT can’t put the two together cleanly
Git merges, but does not commit, and it will stop waiting for you to resolve the conflict. To see which files conflict when merging, you can use git status to check:

git status

Any files that contain unresolved conflicts are listed as unmerged. Git will add standard conflict resolution tags to conflicting files, which can be used to manually locate and resolve these conflicts.
After all conflicts in all files have been resolved, running git add marks them as resolved
If you are satisfied and confirm that all conflicts have been resolved, that is, entering the staging area, you can use git commit to complete the merge commit.
If you want to make it convenient for those who will come to see the merger, you can modify this information to provide more details of the merge. For example, what changes have you made and why. Sometimes the reasons for the conflict of rulings are not direct or obvious, so it is necessary to add some notes.