Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The environment

  • Ubuntu 18.04 64 – bit
  • github

preface

Generally speaking, a Git source repository has only one master branch, namely the master branch, on which are relatively stable performance and relatively complete functions of the code. At the same time, a number of other branches will be created, which correspond to some new functions respectively. Usually, when we develop a new function, we always carry out on the corresponding branch. After the function development is completed and the test is stable, the code of this branch will be merged into the main branch.

Clone remote warehouse

Take Github as an example. First of all, clone the project from Github. Here, I recently did a face recognition project as an example, the project address is github.com/xugaoxiang/…

git clone https://github.com/xugaoxiang/FaceRecognition.git
Copy the code

Viewing the Current Status

Use git status to check the status

See the branch

There is currently only one branch in the project, master, which is the default branch

Create a branch

Create a branch called face_recognition

Git branch view

The result shows that face_recognition has been created for the branch. Note that there is an asterisk in front of the master, indicating that the branch is still the master

Switch branch

Once the branch is created, switch to the new branch and use git Checkout branch name

Now let’s look at the current branching situation

Note that the asterisk in front of the branch has been moved to face_recognition, indicating that the switch is successful

Git status can also be used to view the current branch

Commit code to the new branch

Run git diff to check the code changes in this branch

git commit

Commit to the target branch

git push

On Linux, press TAB to display all branches

Branch merge

Run the git branch command to switch to the target branch name. Then run the git merge command to merge the branch name. If a conflict occurs, run the git status command to check and resolve the conflict

Delete the branch

You can perform

Git branch -d Specifies the branch nameCopy the code