Git Branch Management
Almost every version control system supports branching in some form. Using a branch means that you can detach from the development thread and continue to work without affecting the mainline.
Some people call Git's branching model A "must-kill" feature, and it's because of it, it separates git from the version control system family.
To create a branch command:
Git branch (branchname)
Toggle Branch Command:
git Checkout (branchname)
When you switch the branch, Git replaces the contents of your working directory with the last submitted snapshot of the branch, so multiple branches do not require multiple directories.
Merge Branch Command:
git merge
You can merge to the Unified branch multiple times, or you can choose to delete the merged branch directly after merging.
Git Branch Management
List branches
List Branch basic commands:
Git branch
When there are no parameters, git branch lists your local branch.
$ git Branch
* Master
This example means that we have a branch called "Master", and that the branch is the current branch.
When you execute git init, Git creates a "master" branch for you by default.
If we want to manually create a branch and switch the past. Execute Git branch (branchname).
$ git branch testing
$ git Branch
* Master
Testing
Now we can see that there is a new branch testing.
When you create a new branch in this way after the last commit update, if you later have an update submission and then switch to the "testing" branch, Git restores your working directory to the way you created the branch.
Next we'll show how to switch branches, and we'll switch to the branch we want to modify with git checkout (branch).
$ ls
README
$ Echo ' w3cschool.cc ' > Test.txt
$ git Add.
$ git commit-m ' add test.txt '
[master 048598f] Add test.txt
2 files changed, 1 insertion (+), 3 deletions (-)
delete mode 100644 hello.php
create mode 100644 test.txt
$ ls
README test.txt
$ git checkout testin G
switched to branch ' testing '
$ ls
README hello.php
When we switched to the "testing" branch, the new file we added was Test.txt removed, and the deleted file Hello.php file appeared again. When you switch back to the "master" branch, they appear again.
$ git checkout master
switched to branch ' master '
$ ls
README test.txt
We can also use the Git checkout-b (branchname) command to create a new branch and immediately switch to that branch to operate in that branch.
$ git checkout-b newtest
switched to a new branch ' newtest '
$ git rm test2.txt
rm ' test2.txt '
$ ls
RE ADME test.txt
$ git commit-am ' removed test2.txt '
[newtest 556f0a0] removed test2.txt
1 file changed, 1 de Letion (-)
Delete mode 100644 test2.txt
$ git checkout master
switched to branch ' master '
$ ls
README Test.txt Test2.txt
As you can see, we created a branch, removed some files from the context of the branch, and then switched back to our main branch, and those files came back.
Use a branch to separate work, so that we can do things in different contexts and switch back and forth.
Delete Branch
To delete a branch command:
Git branch-d (branchname)
For example, we want to delete the "testing" branch:
$ git Branch
* Master
testing
$ git branch-d testing
Deleted Branch testing (was 85fc7e7).
$ git Branch
* Master
Branch Merge
Once a branch has an independent content, you will eventually want to merge it back into your main branch. You can merge any branch into the current branch using the following command:
git merge
$ git Branch
* Master
newtest
$ ls
README test.txt test2.txt
$ git merge newtest
updating 2e082b7.. 556f0a0
Fast-forward
test2.txt | 1-
1 file changed, 1 deletion (-)
Delete mode 100644
$ ls
README Test.txt
In the above example we merge the Newtest branch to the main branch, and the Test2.txt file is deleted.
Merge conflicts
Merging is not just a simple file addition, removal operation, Git can also merge changes.
$ git Branch
* Master
$ cat test.txt
w3cschool.cc
First, we create a branch called "Change_site", switching past, we change the content to www.w3cschool.cc.
$ git checkout-b change_site
Switched to a new branch ' Change_site '
$ vim test.txt
$ head-1 test.txt
www.w3cschool.cc
$ git commit-a M ' Changed the site '
[change_site d7e7346] changed the site
1 file changed, 1 insertion (+), 1 deletion (-)
Commits the modified content to the "Change_site" branch. Now, if we switch back to the "master" branch, we can see the content revert to our modified test.txt, and we'll modify the file again.
$ git checkout master
switched to branch ' master '
$ head-1 test.txt
w3cschool.cc
$ vim test.txt
$ cat Test.txt
w3cschool.cc
new Add line
$ git diff
diff--git a/test.txt b/test.txt
index 704cce7. F84C2A4 100644
---a/test.txt
+++ b/test.txt @@-
1 +1,2 @@-
+ new Add line
$ git Commit-am ' new line '
[master 14B4DCA] Adds a new line
of 1 file changed, 1 insertion (+)
Now these changes have been recorded in my Master branch. Next we will merge the "Change_site" branch.
$ git merge change_site
auto-merging test.txt
CONFLICT (content): Merge CONFLICT in test.txt
Automatic merge Failed Fix conflicts and then commit the result.
$ cat Test.txt
<<<<<<< head
w3cschool.cc
Add a new line
=======
www.w3cschool.cc
>>>>>>> Change_site
We merged the previous branch into the "master" branch, and a merge conflict arose, and then we needed to manually modify it.
$ vim test.txt
$ cat test.txt
www.w3cschool.cc
new Add line
$ git diff
diff--cc test.txt
index F84c2a4,bccb7c2.. 0000000
---a/test.txt
+++ b/test.txt @@@ -1,2-1,1
+1,2 @@@ -1,2-1,1
w3cschool.cc
+ www.w3cschool.cc
+ new Add line
In Git, we can use git add to tell git that the file conflict has been resolved
$ git status-s
UU test.txt
$ git add test.txt
$ git status-s
M test.txt
$ git commit
[master 88a FE0E] Merge branch ' Change_site '
We have now successfully resolved the conflicts in the merge and submitted the results.