Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Branch Management of Git

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of Git branch management, which is very detailed and has certain reference value. Friends who are interested must finish it!

Branch is a killer application of Git, and unlike other version control tools, git is extremely efficient in creating and switching branches.

Branch introduction

What is a branch? Let's talk about a product that is already online from a scene that we are very familiar with, and now we need to add a new feature. At this time, if we continue to develop on the original branch, it will be very inconvenient, because it is an application that has already been online, and it must be tested before it can be online. In general, our approach is to create a new branch, develop new functions on the new branch, and then merge it into the main branch after testing.

Creation and switching of branches

At present, my version library branch is as follows:

Create a branch

Now let's create a new branch, dev. The commands to create and view branches are as follows:

Git branch Branch name git branch# git branch dev# git branch dev* master

Switch branch

The branch has been established successfully. Now let's switch to the new branch. The command to switch the branch is as follows: git checkout branch name

# git checkout devSwitched to branch 'dev'

Now, let's make some changes in the new branch, then commit, and then switch to the master branch to make some changes and commit. Then, let's look at the status of the branches.

Git vim config.php # modify the config.php file git add. & & git commit-m 'add config.php'git checkout master # switch to the main branch git vim config.phpgit add. & & git commit-m' change config.php'

$git log-oneline-decorate-graph-all* ca4589c (HEAD-> master) add config file | * 43a5a8f (dev) add config.php | / * 19e3186 add index.php* 9cc82f9 first commit

One command to complete the creation of branches and switch branches

Git checkout-b new branch name

Merging of branches

First, introduce a scenario, which is very common:

A system is already online.

The system needs to update a new function, so you create a new branch (dev) and work on it.

At this point, a sudden problem in the system needs to be urgently checked and dealt with.

So, at this point, you need to switch to the online version (master), then create a new branch (fixbug), and correct the error on the new branch.

When the test is complete, switch to the online branch, then merge the fixbug branch, and then push the changes to the online branch.

Finally, we can switch to the dev branch to get back to work.

At present, the status of our version library is as follows:

Now you need to create a new branch and add new functionality to the new branch.

Git checkout-b dev

Then make some changes on the new branch.

At this point, it is found that a serious bug appears online and needs urgent treatment. So, first I need to switch to the master branch. But an error occurred during the switch.

$git checkout mastererror: Your local changes to the following files would be overwritten by checkout: login.phpPlease commit your changes or stash them before you switch branches.Aborting

We often encounter the above mistakes, because when merging branches, workspaces and staging areas must be "clean". There are two ways to achieve the above requirements

Submit changes

Temporary storage

We use the temporary storage method here to demonstrate

$git stash$ git checkout masterSwitched to branch 'master'

When you switch branches, Git will reset your working directory to make it look like the last time you committed on that branch.

Now, let's create a new fixbug branch and fix the bug on this branch.

$git checkout-b fixbug

Merge branches

When the repair is complete and the test passes, you can merge it into master. Merge with git merge branch names

$git checkout master Switched to branch 'master' $git merge fixbug

Delete Branch

At this point, the fixbug function is complete and can be deleted.

$git branch-d fixbug Deleted branch fixbug (was cca73bb).

Now we can continue to work on the dev branch. We need to take out the contents that were temporarily stored before.

$git checkout dev$ git stash popOn branch devChanges not staged for commit: (use "git add..." To update what will be committed) (use "git checkout--..." To discard changes in working directory) modified: login.phpno changes added to commit (use "git add" and/or "git commit-a") Dropped refs/stash@ {0} (2f8476defbaa813e31f3e1b081f5b88416b2ff50)

When the new function is completed, submit it to the version library.

Conflict resolution

Now that our new functionality is complete, we can merge it into the master branch. Now let's demonstrate how to resolve conflicts when you encounter conflicts during a merger.

$git checkout masterSwitched to branch 'master'$ git merge devAuto-merging index.phpCONFLICT (content): Merge conflict in index.phpAutomatic merge failed; fix conflicts and then commit the result.

Remind me that there is a conflict when index.php merges. Let's take a look at this file.

$cat index.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report