Git Branch Management - Create, Merge, Delete Branches

Keywords: Linux git Asterisk vim

Preface:

Almost all version control supports branching in some way.Using branching means that you can separate your work from the development main line, so as not to affect it.

Git's branching model is known as its "must-kill" feature, which makes Git stand out from many version control systems.Git handles branches incredibly lightweight, creating new branches is done in seconds, and switching between different branches is as convenient.

The branch of Git is essentially just a variable pointer to the submitting object.The default branch of Git is master.After several submissions, we actually have a master branch that points to the last submission object.It will automatically move forward in each submission.

In practice, we may encounter one of the following situations:

  • Develop a website.
  • To achieve a new need, create a branch.
  • Work on this branch.
  • At this point, you suddenly get a call saying that there is a serious problem that needs urgent repair.You will proceed as follows:
  • Switch to your 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, merge the patch branch, and push the changes to the online branch.
  • Modify and switch back to the branch where you originally worked and continue working.

More about git, or walk through it Official Documents Come on!

Blog Outline:
1. Initialize a directory and declare user and mailbox addresses
2. Create, quickly merge, and delete branches
3. Resolving Branch Conflicts
4. Close Quick Merge
5. Bug Branch
6. Git Branch Management related commands

1. Initialize a directory and declare user and mailbox addresses

[root@git ll]# git init
[root@git ll]# git config --global user.name admin
[root@git ll]# git config --global user.email admin@admin.com

2. 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 "First Submission From master"
#Create a branch and enter the new branch
[root@git ll]# git checkout -b dev
[root@git ll]# git branch       #View the current branch
* dev           #The column with the asterisk is the current branch
  master
#Update files in dev branch and submit
[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 current content
aaaa
bbbb
[root@git ll]# git checkout master       #Switch to master branch
[root@git ll]# cat branch.txt       #Confirm current content
aaaa
[root@git ll]# git merge dev        #Merge dev branches
[root@git ll]# cat branch.txt    #View its contents again
aaaa
bbbb
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit   #View submission log
#You can see that the asterisk before its submission log is in the same column because of the fast merge method (default)
#You'll write later to turn off quick merge, and you can then compare the results you see with this command
* bc8bd7b commit From dev branch
* 8bb6874 First submission From master
[root@git ll]# git branch -d dev       #Delete dev branch

3. Resolving Branch Conflicts

In our practical work, we will encounter a branch conflict problem, that is, when you make changes to the file contents under the working branch dev, and then before you submit to the version library, the contents under the master branch have changed. At this time, the contents under your dev branch are older than those under the master. In this case, submitting, there will be a concept of branch conflict, such as chestnut.Next:

[root@git ll]# cat branch.txt      #View the contents of this file for the master branch
aaaa
[root@git ll]# git checkout -b dev        #Create and switch to your own branch of work
 #Modify the contents of the file 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 contents
aaaa
bbbb
[root@git ll]# git checkout master     #Switch to master
[root@git ll]# cat branch.txt      #The contents of the file under master are the same as before
aaaa
#Modify file contents 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
aaaa
cccc
#Next, merge the dev branches:
[root@git ll]# git merge dev        #Return the following error message saying there is a conflict
//Auto Merge branch.txt
//Conflict (content): merge conflicts in branch.txt
//The automatic merge failed, the conflict was corrected, and the result of the correction was submitted.

#Resolve merge conflicts
#In fact, after the above errors, the contents under dev branch already exist in the files under master directory, but they are not submitted, just submit.
#However, direct submission is not recommended because there are special aspects to the content.
[root@git ll]# vim branch.txt       #At this point the contents of the file are as follows

aaaa
<<<<<<< HEAD
cccc
=======
bbbb
>>>>>>> dev
[root@git ll]# cat branch.txt   #Delete special symbols from conflict errors before submitting
aaaa
cccc
bbbb
[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   #View branch merge
*   da2bcdb Conflict resolved
|\  
| * 6abac82 alter from dev
* | 2b5d2f0 alter from master
|/  
* ef014ec alter from master

4. Close Quick Merge

As mentioned above, when looking at the submission history of the git version, its branching structure doesn't behave as intuitively as it does because the option to fast merge is turned on by default, and here's how to turn off fast merge.

#Enter branch, modify file contents, 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 "Turn off fast merge"
#Switch to master branch for merge
[root@git ll]# git checkout master 
[root@git ll]# git merge --no-ff -m "Branch merge description" dev    #Option'--no--ff'turns off fast merge
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit 
#Review the submission log again,
*   38c4fad Merge branch 'dev'
|\  
| * 9233297 Close Quick Merge
|/  
*   7e0ea1b Conflict resolved    #Looking up from here, you can see that it was submitted through a branch
|\  
| * f9180c9 alter from dev
* | 107081a alter from branch/bug
|/  
#Following is the branch merge operation that did not turn off fast merge initially. You can see that there is only one column of asterisks without showing branches
* bc8bd7b commit From dev branch          
* 8bb6874 First submission From master
[root@git ll]# git branch -d dev       #Delete dev branch

5. Bug Branch

In the development process, bugs are just like everyday snacks, bugs need to be fixed. In git, since branches are powerful, bugs can be fixed by a new temporary branch. After fixing, branches merge, and temporary branches are deleted.

When we get a bug fix task, it's natural to create a branch to fix it, but half of the work we're doing is still uncommitted, but we need to fix the bug right away. At this point, you can "store" the current workspace using the stash function provided by git, and then resume working when you return to the field later.

Half way through the work, the status of the workspace is as follows:

[root@git ll]# cat branch.txt 
aaaa
cccc
bbbb
[root@git ll]# echo "dddd" >> branch.txt 
[root@git ll]# git status     #Prompt for modification but not yet submitted
# In branch dev
# Changes that have not yet been provisioned for submission:
#   (Use "git add <file>..." to update submissions)
#   (Use "git checkout -- <file>..." to discard workspace changes)
#
#   Modify: branch.txt
#
//Modifications have not yet been added to the submission (using "git add" and/or "git commit-a")

Hide the workspace at this time

[root@git ll]# git stash        #This command hides the current workspace
[root@git ll]# git status        #Check the current workspace again and it will be clean
# In branch dev
//No files to submit, clean workspace
[root@git ll]# cat branch.txt       #The file doesn't have anything we've added yet
aaaa
cccc
bbbb

Assume bug fixes on master branch

#Switch to master branch and enter bug branch modification
[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 merged file is as follows
aaaa
cccc
bbbb
eeee
[root@git ll]# git branch -d bug      #Delete bug branch
#Go back to what was modified before the dev branch was restored and continue with your work
#There are two ways to recover:
#One is to use git stash apply to recover, but after recovery, stash content is not deleted and needs to be deleted by git stash drop.
#Another way is to use git stash pop, which restores but also deletes the stash content; here I'm going to use the second method
[root@git ll]# git stash pop        #Restore the contents of a store
[root@git ll]# cat branch.txt     #Our previous content has returned
aaaa
cccc
bbbb
dddd
#Finally, when merging dev branches, there will also be branch conflicts, referring to the previous method for 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 partitions to the current branch
[root@git ll]# git branch -d ui       #Delete ui branch
[root@git ll]# git branch        #View branch (asterisk indicates branch)
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit 
#View Branch Merge Chart
[root@git ll]# git merge --no-ff -m "Submit information on merge" dev   #Do not use fast merge branches
[root@git ll]# git stash        #Temporarily store the state of the current version Library
[root@git ll]# git stash pop   #Recover and delete temporary stored information
[root@git ll]# git stash apply    #Restore temporary stored information without deleting it
[root@git ll]# git stash drop     #Delete information from temporary storage
[root@git ll]# git stash show     #View temporary stored information
[root@git ll]# git branch -D dev       #Force deletion of a branch
[root@git ll]# git remote       #Check to see if the current version library belongs to a remote version Library
[root@git ll]# git remote -v    #View details of remote version Libraries
[root@git ll]# git push origin dev     #Push local dev branch to remote warehouse
[root@git ll]# git checkout -b dev origin/dev   #Create a local dev branch and associate it with the dev branch of a remote warehouse
[root@git ll]# git pull    #Grab remote branches, commonly used to resolve conflicts
[root@git ll]# git branch --set-upstream-to=origin/dev dev   #Associate local branch dev with dev branch of remote warehouse

Posted by PURU on Thu, 14 Nov 2019 14:17:44 -0800