Home Page Ah File Ah sundry Ah Git knowledge overview (three) branch creation, deletion, switching, merging and conflict resolution. -ios learning from entry to mastery is done by Ji Chang Xin.

Git knowledge overview (three) branch creation, deletion, switching, merging and conflict resolution. -ios learning from entry to mastery is done by Ji Chang Xin.

share Hottest Of information

First two chapters Blog Concentrated on some of the common GIT. command For details, please refer to " Git knowledge overview (1) from git clone and git status "," Overview of Git knowledge (two) git commonly used commands " This article Blog is mainly related to Git Edition In management branch Of Establish , switch as well as merge And lists the branches that happen in merge. conflict Hour Solve mode At the same time, it also introduces how to do this. delete Local branches and remote branches. This blog is a reference to ProGit. content For reference. Learngitbranching this website The contents of the GIT branch. Study And sort out. Underneath Example Some give Out of Based on LearningBranching, there are Yes. True branch operation An example. This blog is only part of GIT branch management. knowledge The next blog will continue to summarize the GIT branch. Relevant Operation.

First, branch creation and handover.

1. Create new branches and switch to the branch. Submission

First of all, let's start with the most simple Here, the bottom of the GIT branch is introduced, and the most basic operation is to create and switch. step As follows:

  1. Git branch < branch name > First, use git branch bugfix01 in the current branch. Also on A new branch named bugfix01 is created on the master branch.

  2. Git checkout < branch name > Then, use the GIT checkout bugfix01 command to switch to the new branch of our newly created bugfix01.

  3. Git commit Finally, use the GIT commit command to proceed on the new branch. Code Submission.

It is easy to see from the above operation that the asterisk * represents the HEAD of GIT. Pointer Point to the current branch. At the beginning, the HEAD pointer points to the master branch, that is, the branch that the user currently operates is master. Using git branch Create a After the new branch, the direction of HEAD has not changed, and it still points to the master branch. When we use the GIT checkout command, the HEAD pointer shifts from the master branch to the bugfix01 branch. Can Bugfix01 has been operated.

When you need to pay attention to it, if the branch is not submitted on the new branch after the branch is created, then the bugfix01 and master branches point to the same number of submissions. C 1. This C1 represents the hash value submitted every time, that is, the submit number. When using git commit, it is indicated that there are new submissions on the branch. Will On the basis of previous submission, a submission is extended, that is, C2. Post submitted C2 There will be A pointer points to the previous submission of C1.

2. The above operations are terminal Performance

Next we will take a look at the concrete operation of the terminal. Below is the initial state, and the branch now is master branch. From below you can see the Tag number on the branch and the master pointer to the HEAD pointer.

Then we use git branch bugfix01 to create a new branch. From the bottom we can see that the commit number of the pointer to the bugfix01 branch coincides with the commit number of master, because the branch we have derived from master. However, the branches currently operated are still master (HEAD > Master).

Because we switched from the master branch to the bugfix01 branch, so the HEAD pointer at the moment is pointing to bugfix01.

Then we made some code on bugfix01, and now we see bugfix01 pointing to the latest commit, but HEAD has been pointing to the current branch bugfix.

The commands used in the above operations are as follows:

3. Create and switch branches

We can use a simplified command to create and switch to the branch, and demonstrate it below. This one Operation:

  1. Git checkout -b < branch name > First, use the GIT checkout -b bugfix02 command to create a new branch on the current branch bugfix01 and switch to the newly created bugfix02.

  2. Git commit: then you can use git commit to commit on the new branch bugfix02.

The above command is not overstated in the execution of the terminal. Please refer to the second part.

4, switch to the previous branch and submit it.

The use scenario below is to switch to a previously created branch and submit it on the branch after the handover. After the operation, the fork will form.

  1. Git checkout < branch name > The current branch is bugfix02, then the GIT checkout bugfix01 command is used to switch branches to bugfix01.

  2. Git commit: after switching, you can commit operation on bugfix01.

In the last blog, we also talked about it. In the terminal, we can use git log --oneline --graph --all to view all branches. The following are as follows:

Two, merge and delete branches and conflict resolution

The last part is how to create branches and switch between branches. Next we will look at the merging and deletion of branches.

1. Merging of branches -merge

We also take a look at the example above to see the merging of branches. The operation below is mainly the combination of branches, when on the two branches bugfix01 and bugfix02. modify Related bug, and need to merge the modified code into the master branch. Below is this series of operations:

  • First, use the GIT checkout master command to switch to the master branch.

  • Then the GIT merge bugfix01 command is used to fit the bugfix01 branch changes to the master branch, and the new file will be submitted after the closure is successful, and now there will be a new commit number, which corresponds to the C9 below.

  • Then use it
    Git merge bugfix02 When the changes on the bugfix02 are incorporated into the master branch, the corresponding commit number of the merge is C10.

  • In the end, it is possible to make normal submissions on the master branch.


Below is the branch merge operation that we have done in the real branch. We have branched the bugfix01 branch merge to the master branch. From below you can see that bugfix02 has not yet joined the Master branch. Later, we will incorporate the bugfix02 branch into the master branch in an example of handling conflicts.


2. Deletion of branches

As you can see above, although the branches of bugfix01 and bugfix02 have been integrated into the master branch, These two items. Branches still exist. If we don't need this Two The branch pointer can delete two branch pointers:

  • First use git branch -d bugfix01 to delete branch bugfix01.

  • Then use the GIT branch -d bugfix02 to delete the branch bugfix02.

From the bottom of the operation, the deletion of the branch is only a pointer to the commit number deleted. It does not delete its related submission number. It can still find the previous commit record in the log, and it can still create a new branch on the commit. If you want to delete the remote branch, you have to use the $git push origin --delete < branch name >.

Or to attach to the instance, underneath the last part of the master branch has been incorporated into the bugfix01 branch of the implementation of the delete operation, delete the success will prompt "Delete branch bugfix01 (was 223aefb)", this is the deletion of the corresponding commit number of the first 7 branches.

From below screenshot You can see that the branch of bugfix01 is deleted, but only a pointer or alias to the commit number is deleted, and nothing else has changed.

3, conflict resolution

The above is the normal process without conflict. If the branch is merged, the two branches modify the same one at the same time. file The same place. At the moment, when a branch is merged, it will conflict, and the code to solve the conflict needs manual intervention. Above we say that it is useful to keep the bugfix02 branch. Now let's look at the use of bugfix02. It is easy to see from the log below that both the bugfix02 branch and the master branch modify the second row data of the file. Next we will integrate bugfix02 into the master branch.

The content of the screenshot below is the clash reported when we merge the bugfix02 branch into the master branch. The general meaning of conflict is that there is a conflict when merging files, and automatic merge fails. repair This conflict is committed after repair. We can use git status to check the current state (configured alias git st). You can also see from git status's tips that you can repair the conflict, then use git commit to commit, or use git merge --abort command to give up. This time Amalgamation. If we give up the merger, we will return to the state before the merger. Of course, this is not what we want. We will solve the conflict and submit it at the bottom.

Next we will take a look at the specific content of the conflict, from the beginning to the end of the conflict is part of the contents of the two branches from = = = = = = = = division. The above is the content of the current branch. We need to merge the contents of the conflict, analyze the situation according to the specific circumstances, and see those needs. Retain Those do not need to be preserved. They all need to be preserved.

Below is the content that we modify after conflict. strategy It retained the modification of master and deleted the modification of bugfix02 branch. Then you can submit the modified files, so you won't overdo it here.

4, use tool Conflict resolution

input Git mergetool Then enter opendiff according to the prompt and open it under Mac. Xcode The built-in FileMerge tool.

Below is the startup FileMerge tool, which can choose several file merge strategies when using the tool to file merge. For example, with the left side as the criterion and the right as the criterion.

The above tool is located in the development tool of Xcode, as shown below:

Three. Branch view and mandatory deletion

First, we use the GIT log --oneline --graph --all command to visualize the current branch state. From the screenshot below, we can see that there are three branches master, bugfix02 and bugfix03. And we can see that bugfix02 has been integrated into the master branch, and bugfix03 has not yet been integrated.

Below are a series of ways to view branches.

  • Git branch: look at all branches, where the asterisk is the current branch and the master branch below.

  • Git branch -v: see the last submission of all branches and branches.

  • Git branch --merged: see all branches that have been integrated into the current branch.

  • Git branch --no-merged: view is not clad into branch.

Today's blog about git branch management content first come here, the next blog will detail rebase and cherry-pick and other related content.

IOS Learn from Introduction reach Master As far as possible Woman Long letter

Fabulous ( Zero )

This article is composed of Ji Chang Xin Creation, article address: Https://
Use Knowledge sharing signature 4 The international license agreement is licensed. In addition to the reprint / provenance, all originals or translations of this site must be signed before retransmission. The final edit time is March, 22, 2018 at 11:07 PM.

Hot articles




Please wait three seconds after submission, so as not to cause unsuccessful successes and duplication.