image

Git branch management has been implemented in both online and local merges. After all: talk is a sham. But only practice does not arrange, can only be a silly handle. How does branch management work?

Let’s start with a classic graph from GitLab as a general overview, which is also convenient to understand branch management and direction:

image

Scene preset

image

Consider the company’s development project called Hogwarts_Online2, which includes the live branch master, development branch Develop, test branch Release, and personal development feature branch

Feature branches and Develop branches

Create your own branch locally and pull files from the Develop branch:

image.png

1.2) Create a new file gitflowdemo. TXT in the current branch and enter “study git”; Then add and commit

# change branch
vi gitflowDemo.txt
# commit change
git add gitflowDemo.txt
git commit -m "add demo"
Copy the code

1.3) Use git pull to check whether the remote develop branch conflicts with the current branch:

$ git pull origin develop
Copy the code

Note:

Git push origin gitflowDemo:

image

Merge Request on GitLab and merge on develop branch:

Git merge –abort ‘ ‘is used to abort this merge

create merge request:

image

Select the Develop branch:

image

Merge without conflicts:

image

Finally we can see the successful merge into the Develop branch:

image

We can also see where the branches go in the graph:

image

This completes the pull and merge of code for the properties and Develop branches

Alternatively, the develop branch at work may be open to push, so you can modify the merge locally and push it to remote Develop

Change the gitflowdemo. TXT file to

study git
Copy the code

add,commit,push

git add gitflowDemo.txt
Copy the code

Switch to the local Develop branch, pull the latest code, merge the local gitflowDemo branch, and push into the remote Develop branch

git checkout develop
Copy the code

Check for updates on GitLab:

The release branch

The develop branch changes frequently, and the master branch is the upper limit version, so you need a beta version of the release branch.

hotfixes

Sometimes a very urgent bug occurs and needs to be modified immediately. It is too late to merge tests on all branches. Create a bugfix branch using hotfixes to bypass other branches and merge it into the master.

Note: This kind of untested online situation is very dangerous, I have seen; When I was working in Huawei, one of my team development colleagues modified one or two lines of code. Thinking there would be no problem, he skipped our test and released the code directly through others. At that time, I was in GNSS group. As a result, the positioning function of more than 10 million mobile phones is at risk of failure, which has caused a lot of complaints and a great impact. In the end, the relevant developers had to stop their vacation, and our test team worked overtime for seven days in November. We paid a lot for this small change

3.1) Create bugfix branch and modify file push to remote branch:

git checkout master
Copy the code

3.2) If you check GitLab at this time, you will find an additional branch of bug02 that has been pulled from the master branch:

image.png

3.3) The final master permission owner does the merge.

image.png

3.4) After the bug is fixed on the master branch, it is possible that the changes on the Master branch are already ahead of other branches; At this point, you need to update the other branches and merge the master branch; At the same time, remove the Bugfix branch to keep it as clean as possible.

4, add

git log

git log --graph --all --decorate=short
Copy the code

rebase

Change the base, merge branches can change the base line of the branch direction, when there are many branches, can simplify the display of branches, merge branches to make the process look concise

git checkout feature
Copy the code

image

Comparison with branch direction after merge:

git checkout feature
Copy the code

image

In addition, rebase can modify the history of the commit (not commonly used and not recommended)

git rebase -i HEAD~2
Copy the code

Pay attention to
:



1. Do not rebase on a common branch



2. The main branches are protected

git diff

git diff
Copy the code

Common diff tools:

  • Diff — show only the increase (+) or decrease (-) of a row

  • Vimdiff — More direct than diff seems

  • IDE – powerful tools, clear presentation, easy to use

vimdiff bugfix01.txt bugfix02.txt
Copy the code