Use of git branches, create branches, merge branches, delete branches, merge conflicts, branch management strategies, bug branches, forced deletion of branches

GIT | branch

Create a branch

Check which branches are currently in the local repository

git branch

The branch pointed to by HEAD is the branch currently being worked on.

cat .git/HEAD

Insert image description here

Create a branch

git branch dev

Created, but currently still pointing tomaster

Insert image description here
You can also use the tree command to see that the branch has been created successfully.

Insert image description here

The branch created is the same as the latest record of the main branch

Insert image description here

Switching branches means letting HEAD point to our dev branch.

git checkout dev

Insert image description here

We made changes to the ReadMe file on the dev branch

Insert image description here

Submit again

Insert image description here

Switch back to the master branch at this time
View files

Insert image description here

You can see that the newly added line of file is missing~~

Then let’s switch back to the dev branch and take a look.

Insert image description here
I found that the newly added line is still there

We looked here and found that it has changed
Insert image description here

Let’s check the records
The dev branch is the latest record, and the master branch is the second one

Insert image description here

Our final effect is to see our effect on the master branch. How do we operate?

merge branches

This requires us to merge branches. Before merging branches, we need to switch to our master branch.

git merge dev

Insert image description here

delete branch

This branch can only be deleted on other branches

git branch -d dev

Insert image description here

merge conflicts

When merging branches, we have modified the files on the master branch, and I have also modified the files on the dev branch. Then an error will appear when merging. Let's demonstrate it.

Quickly create branches and enter branches

git checkout -b dev1

Insert image description here

We changed the original aaa to bbb

Insert image description here

Then submit it

Insert image description here

We switch to the master branch to check the file content and find that it is still aaa because it has not been merged yet.

Insert image description here
Insert image description here

Next we will continue to change this aaa to ccc, and then merge the branches

Insert image description here

Insert image description here
If you merge again at this time, you will be prompted for merge conflicts.

git merge dev1

Insert image description here

Let’s open the ReadMe file and check it out

Insert image description here

At this time, you need to manually choose which codes to keep

Suppose we just keep this code

Insert image description here

and then submit

Insert image description here
Check if it is the latest commit

Insert image description here

Visual viewing method

git log --graph --abbrev-commit

Insert image description here

Branch management strategy

Not usedFast forwardMode

Create a new branch

git checkout -b dev2

Insert image description here

Modify the ReadMe file and submit it

Insert image description here

Insert image description here

Merge after switching back to the master branch

Not usedFast forwardMode

git merge --no-ff -m "merge with no-ff" dev2

Insert image description here

bug branch

Suppose we are currently developing on the dev2 branch. Halfway through the development, we suddenly find a bug on the master branch, which needs to
be resolved. In Git, every bug can be fixed by creating a new temporary branch. After fixing, merge the branches and then delete the temporary branch

Insert image description here

  • At this time, a bug appears in the master branch. At this time, it is necessary to switch to the master branch.

Insert image description here

Then we don’t want this, we can do this

Save the contents of the workspace

git stash

Insert image description here

After fixing the bug, we need to return to the dev2 branch to continue development.

git stash pop

Insert image description here
Now that we are on the dev2 branch, we continue development

Insert image description here
Then commit, a new commit was made on the dev branch

Insert image description here

At this time, it is necessary to merge, but conflicts will occur during the merge. The master has just fixed the bug, and this time we need to merge the branches again. We need to solve the error.

We need not to merge branches on master, but merge master branches on dev, and solve the problem locally before taking the next step.

We merge on the dev2 branch
Insert image description here

Manually modify conflicts
Insert image description here

Then you can merge~~

Insert image description here

Finally, don’t forget to delete the temporary branch and development branch you just created~~

Insert image description here

Force deletion of branch

If during development, if the code has been developed on a branch and the code is submitted, it cannot be deleted using the traditional method at this time. We need to use -D Delete~~

git branch -D dev3
  • What is the practical use of branches? Suppose you plan to develop a new feature, but it takes two weeks to complete. You wrote 50% of the code in the first week. If you submit it immediately, because the code has not been written yet, the incomplete code base will prevent others from working. If you wait until all the code is written before submitting it, there is a huge risk of losing daily progress.
  • Now that there are branches, there is no need to be afraid. You create a branch of your own. Others cannot see it. You continue to work normally on the original branch. You work on your own branch and submit it when you want. Until the development is completed, merge it into On the original support, it is safe and does not affect other people's work.
  • And whether Git creates, switches, or deletes branches, Git can complete it within 1 second! Whether your repository is 1 file or 10,000 files.

Guess you like

Origin blog.csdn.net/2201_76004325/article/details/134616269