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

Git branch management-create, merge, delete branches

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Foreword:

Almost all version control supports branching in some form. Using branches means that you can separate your work from the development mainline so as not to affect the development mainline.

The branch model of Git is called its "must-kill feature", and it is precisely because of this feature that Git stands out from many version control systems. The way Git handles branches is incredibly lightweight, creating new branches is done in seconds, and switching between branches is just as easy.

A branch of Git is essentially just a variable pointer to a commit object. The default branch of Git is master. After several commit operations, we already have a master branch that points to the last commit object. He automatically moves forward during each commit operation.

In actual work, we may encounter the following situation:

Develop a website. To implement a new requirement, create a branch. Work on this branch. Just then, you suddenly received a phone call saying that there was a serious problem that needed urgent repair. You will do this as follows: switch to your online branch (production branch). Create a new branch for this urgent task and fix it in it. After the test passes, switch back to the online branch, then merge the patch branch, and finally push the changes to the online branch. After modification, switch back to the branch where you originally worked and continue to work.

For more information about git, move on to its official documentation.

Blog outline:

1. Initialize a directory and declare the user and email address

2. Create, quickly merge and delete branches

3. Solve the problem of branch conflict

4. Turn off Quick merge

5. Bug branch

6. Git branch management related commands

1. Initialize a directory and declare the user and email address [root@git ll] # git init [root@git ll] # git config-global user.name admin [root@git ll] # git config-global user.email admin@admin.com2, create, Quickly merge and delete branches [root@git ll] # echo "aaaa" > branch.txt [root@git ll] # git add branch.txt [root@git ll] # git commit-m "submit From master for the first time" # create a branch and enter the new branch [root@git ll] # git checkout-b dev [root@git ll] # git branch # to view the current branch * dev # the column with asterisk is The current branch master# updates the file in the dev branch and submits [root@git ll] # echo "bbbb" > > branch.txt [root@git ll] # git add * [root@git ll] # git commit-m "commit From dev branch" [root@git ll] # cat branch.txt # confirm that the current content aaaabbbb [root@git ll] # git checkout master# switches to master branch [root@git ll] # cat branch.txt # Confirm the current content aaaa [root@git ll] # git merge dev # merge dev branch [root@git ll] # cat branch.txt # View its content again aaaabbbb [root@git ll] # git log-- graph-- pretty=oneline-- abbrev-commit # View the submission log # you can see that the asterisk in front of the submission log is in the same column This is because the fast merge method (default) # will be written later to close the fast merge, and then you can compare the results viewed by this command * bc8bd7b commit From dev branch* 8bb6874 first submitted From master [root@git ll] # git branch-d dev # Delete dev branch 3, resolve branch conflict

In our actual work, we will encounter a problem of branch conflict, that is, when you modify the content of the file under the work branch dev, and then before you submit it to the version library, the content under the master branch has changed. At this time, the content under your dev branch is older than that under the master. In this case, if you merge branches, there will be a concept of branch conflict. Chestnut is as follows:

[root@git ll] # cat branch.txt # View the file content of the master branch aaaa [root@git ll] # git checkout-b dev # create and switch to your own working branch # modify the file content and submit it to the version library [root@git ll] # echo "bbbb" > > branch.txt [root@git ll] # git add branch.txt [root@git ll] # git commit-m "alter from dev" [ Root@git ll] # cat branch.txt # View file content aaaabbbb [root@git ll] # git checkout master # switch to the file content under master [root@git ll] # cat branch.txt # master or the original aaaa# modify the file content under master and submit [root@git ll] # echo "cccc" > > branch.txt [root@git ll] # git add branch.txt [root@git ll] # git commit-m "alter from master "[root@git ll] # cat branch.txt # the contents of the file under master are as follows: aaaacccc# then merge the dev branches: [root@git ll] # git merge dev # returns the following error message Said that there is a conflict automatic merge branch.txt conflict (content): merge conflict in branch.txt automatic merge failure, correct the conflict and then submit the result of the correction. # resolve merge conflict # in fact, after the above error report, the content under the dev branch already exists in the file in the master directory, but it has not been submitted. It can be submitted # but direct submission is not recommended in the work. Because there is something special about the content [root@git ll] # vim branch.txt # at this time, the contents of the file are as follows: aaaa > dev [root@git ll] # cat branch.txt # delete the special symbols caused by the conflict error and submit aaaccccbbb [root @ git ll] # git add branch.txt [root@git ll] # git commit-m "conflict resolved" [root@git ll] # git log-- graph-- pretty=oneline-- abbrev-commit # Check branch merge status * da2bcdb conflict resolved |\ | * 6abac82 alter from dev* | 2b5d2f0 alter from master | / * ef014ec alter from master4, Turn off Quick merge

As mentioned above, when looking at the submission history of the git version, the branch structure is not so intuitive because the option of fast merge is turned on by default, and here is how to turn off fast merge.

# enter the branch, modify the contents of the file, and submit [root@git ll] # git checkout-b dev [root@git ll] # echo "ffff" > > branch.txt [root@git ll] # git add branch.txt [root@git ll] # git commit-m "close Quick merge" # switch to master branch Merge [root@git ll] # git checkout master [root@git ll] # git merge-- no-ff-m "Branch merge description" dev # option "--no--ff" is to turn off fast merge [root@git ll] # git log-- graph-- pretty=oneline-- abbrev-commit # to view the submission log again * 38c4fad Merge branch 'dev' |\ | * 9233297 close Fast merge | / * 7e0ea1b conflict resolved # look up here, you can see that it was submitted after a branch |\ | * f9180c9 alter from dev* | 107081a alter from branch/bug | / # the branch merge operation for which fast merge is not initially turned off, you can see that there is only one asterisk. Does not show branch * bc8bd7b commit From dev branch * 8bb6874 first submission From master [root@git ll] # git branch-d dev # Delete dev branch 5, Bug branch

Developers in the development process, bug as usual, with bug to repair, in git, because the branch is powerful, so you can use a new temporary branch to repair bug, repair, branches merge, and then delete the temporary branch.

When we received a task to modify the bug, we naturally wanted to create a branch to repair it, but in the middle of the ongoing work, it could not be submitted, but we needed to repair the bug immediately. At this time, we can "store" the current workspace through the stash function provided by git, and continue to work after the site is restored later.

Halfway through the work, the status of the workspace is as follows: [root@git ll] # cat branch.txt aaaaccccbbbb [root@git ll] # echo "dddd" > > branch.txt [root@git ll] # git status # prompts for modification but has not yet been submitted # changes that have not been temporarily saved for submission in the branch dev#: # (use "git add." Update the content to be submitted) # (use "git checkout--..." Discarding workspace changes) # # modifications: branch.txt# modifications have not been added to the submission (using "git add" and / or "git commit-a") to hide the workspace at this time [root@git ll] # git stash # is this command, you can hide the current workspace [root@git ll] # git status # to view the current workspace again It's clean # there are no files to be submitted in the branch dev Clean workspace [root@git ll] # cat branch.txt # file does not have our new content aaaaccccbbbb suppose repair bug# on master branch switch to master branch and enter bug branch modify [root@git ll] # git checkout master [root@git ll] # git checkout-b bug [root@git ll] # echo "eeee" > branch.txt [root@git ll] # git add branch.txt [root@git ll] # git commit-m " Alter from bug "# switch to master branch merge modified bug branch [root@git ll] # git checkout master [root@git ll] # git merge bug [root@git ll] # cat branch.txt # the contents of the merged file are as follows: aaaccccbbbbeeee [root @ git ll] # git branch-d bug # Delete bug branch # go back to the content modified before dev branch recovery continue your work # there are two recovery methods: # one is to use git stash apply to restore However, after recovery, the stash content is not deleted and needs to be deleted with git stash drop. # another way is to use git stash pop to delete the stash content when restoring. Here I use the second method [root@git ll] # git stash pop # to recover the contents of the store [root@git ll] # cat branch.txt # our previous content is back to aaaaccccbbbbdddd# and the final work is completed. When merging dev branches, there will be branch conflicts. You can refer to the previous method of resolving branch conflicts. 6. Git branch management related commands [root@git ll] # git checkout-b ops # create ops branch and switch to ops branch [root@git ll] # git checkout master # switch to master branch [root@git ll] # git merge dev # quickly merge dev partition to the current branch [root@git ll] # git branch-d ui # Delete ui branch [root@git ll] # git branch # View where it belongs Branch (denoted by an asterisk) [root@git ll] # git log-- graph-- pretty=oneline-- abbrev-commit # View the branch merge diagram [root@git ll] # git merge-- no-ff-m "merge submission information" dev # does not use the fast merge branch [root@git ll] # git stash # to temporarily store the status of the current version library [root@git ll] # git stash pop # restore and delete In addition to temporary storage information [root@git ll] # git stash apply # restore temporary storage information But do not delete information [root@git ll] # git stash drop # Delete information in temporary storage [root@git ll] # git stash show # View temporary storage information [root@git ll] # git branch-D dev # Force deletion of a branch [root@git ll] # git remote # check whether the current version library belongs to a remote version library [root@git ll] # git remote-v # View Details of the remote version library [root@git ll] # git push origin dev # push the local dev branch to the remote repository [root@git ll] # git checkout-b dev origin/dev # create a local dev branch and associate it to the remote repository dev branch [root@git ll] # git pull # grab the remote branch Commonly used to resolve conflicts [root@git ll] # git branch-- set-upstream-to=origin/dev dev # dev branch that associates a local branch dev to a remote repository

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

Servers

Wechat

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

12
Report