Git Branch Management
Git branch management is one of Git's powerful features, allowing multiple developers to work in parallel, develop new features, fix bugs, or experiment without affecting the main codebase.
Almost every version control system supports branches in some form; a branch represents an independent line of development.
Using branches means you can diverge from the main line of development and continue working without affecting it.
A Git branch is actually a pointer to a snapshot of changes.
Some people call Git's branch model akiller feature, and it is precisely because of this that Git is distinguished from the family of version control systems.
Create a branch
Create a new branch and switch to it:
git checkout -b
For example:
git checkout -b feature-xyz
Switch branch command:
git checkout (branchname)
For example:
git checkout main
When you switch branches, Git replaces the contents of your working directory with the snapshot of the branch's last commit, so multiple branches do not require multiple directories.
View Branches
View all branches:
git branch
View remote branches:
git branch -r
View all local and remote branches:
git branch -a
Merge branches
Merge another branch into the current branch:
git merge
For example, switch to the main branch and merge the feature-xyz branch:
git checkout main
git merge feature-xyz
Resolving Merge Conflicts
When a conflict occurs during merging, Git marks the conflicting files, and you need to resolve the conflict manually.
Open the conflicted files and resolve the conflicts according to the markers.
Mark the conflict as resolved:
git add
Commit the merge result:
git commit
Deleting Branches
Delete a local branch:
git branch -d
Force-delete an unmerged branch:
git branch -D
Delete a remote branch:
git push origin --delete
Example
Before starting, we first create a test directory:
$ mkdir gitdemo
$ cd gitdemo/
$ git init
Initialized empty Git repository...
$ touch README
$ git add README
$ git commit -m '第一次版本提交'
[master (root-commit) 3b58100] 第一次版本提交
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
Git Branch Management
Listing Branches
Basic command to list branches:
git branch
With no arguments,git branch will list your local branches.
$ git branch
* master
This example means that we have a branch called master , and that branch is the current branch.
When you execute git init , by default Git will create master branch is not executed.
for you. If we want to create a branch manually, just 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 this way after the last commit update, if there are later update commits, and then you switch to the testing branch, Git will restore your working directory to the state it was in when you created the branch.
Next, we will demonstrate how to switch branches; we use git checkout (branch) to switch to the branch we want to modify.
$ ls
README
$ echo 'runoob.com' > test.txt
$ git add .
$ git commit -m 'add test.txt'
[master 3e92c19] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$ ls
README test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README
When we switch to the testing branch, the new file test.txt we added is removed. When switching back to the master branch, they reappear.
$ 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 it, so as to operate in that branch.
$ git checkout -b newtest
Switched to a new branch 'newtest'
$ git rm test.txt
rm 'test.txt'
$ ls
README
$ touch runoob.php
$ git add .
$ git commit -am 'removed test.txt、add runoob.php'
[newtest c1501a2] removed test.txt、add runoob.php
2 files changed, 1 deletion(-)
create mode 100644 runoob.php
delete mode 100644 test.txt
$ ls
README runoob.php
$ git checkout master
Switched to branch 'master'
$ ls
README test.txt
As you can see, we created a branch, removed some files test.txt on it, and added the runoob.php file, then switched back to our main branch; the deleted test.txt file came back, and the newly added runoob.php does not exist in the main branch.
Using branches separates the work, allowing us to do things in different development environments and switch back and forth.
Deleting Branches
Delete branch command:
git branch -d (branchname)
For example, if we want to delete the testing branch:
$ git branch
* master
testing
$ git branch -d testing
Deleted branch testing (was 85fc7e7).
$ git branch
* master
Branch Merging
Once a branch has independent content, you will eventually want to merge it back into your main branch. You can use the following command to merge any branch into the current branch:
git merge
$ git branch
* master
newtest
$ ls
README test.txt
$ git merge newtest
Updating 3e92c19..c1501a2
Fast-forward
runoob.php | 0
test.txt | 1 -
2 files changed, 1 deletion(-)
create mode 100644 runoob.php
delete mode 100644 test.txt
$ ls
README runoob.php
In the above example we merged the newtest branch into the main branch, and the test.txt file was deleted.
After merging, you can delete the branch:
$ git branch -d newtest
Deleted branch newtest (was c1501a2).
After deletion, only the master branch remains:
$ git branch
* master
Merge Conflicts
Merging is not just a simple operation of adding or removing files; Git also merges modifications.
$ git branch
* master
$ cat runoob.php
**Note:** If runoob.php does not exist, create this file first; the command can be touch runoob.php.
First, we create a branch called change_site, switch to it, and change the content of runoob.php to:
Create the change_site branch:
$ git checkout -b change_site
Switched to a new branch 'change_site'
$ vim runoob.php
$ head -3 runoob.php
$ git commit -am 'changed the runoob.php'
[change_site 7774248] changed the runoob.php
1 file changed, 3 insertions(+)
For vim command operations, you can refer to:Linux vi/vim。
Commit the modified content to the change_site branch. Now, if we switch back to the master branch we can see the content restored to before our modification (an empty file, no code), and we modify the runoob.php file again.
$ git checkout master
Switched to branch 'master'
$ cat runoob.php
$ vim runoob.php # 修改内容如下
$ cat runoob.php
$ git diff
diff --git a/runoob.php b/runoob.php
index e69de29..ac60739 100644
--- a/runoob.php
+++ b/runoob.php
@@ -0,0 +1,3 @@
+
$ git commit -am '修改代码'
[master c68142b] 修改代码
1 file changed, 3 insertions(+)
Now these changes have been recorded in our "master" branch. Next we will merge in the "change_site" branch.
$ git merge change_site
Auto-merging runoob.php
CONFLICT (content): Merge conflict in runoob.php
Automatic merge failed; fix conflicts and then commit the result.
$ cat runoob.php # 打开文件,看到冲突内容
>>>>>> change_site
?>
When we merge the previous branch into the master branch, a merge conflict appears, and next we need to modify it manually.
$ vim runoob.php
$ cat runoob.php
$ git diff
diff --cc runoob.php
index ac60739,b63d7d7..0000000
--- a/runoob.php
+++ b/runoob.php
@@@ -1,3 -1,3 +1,4 @@@
In Git, we can use git add to tell Git that the file conflict has been resolved
$ git status -s
UU runoob.php
$ git add runoob.php
$ git status -s
M runoob.php
$ git commit
[master 88afe0e] Merge branch 'change_site'
Now we have successfully resolved the conflict in the merge and committed the result.
Command Manual
| Command | Description | Usage Examples |
|---|---|---|
git branch |
List, create, or delete branches. It does not switch branches; it is only used to manage the existence of branches. | git branch: List all branches |
`git branch new-branch`:创建新分支
`git branch -d old-branch`:删除分支 |
| git checkout | Switch to the specified branch or restore files in the working directory. It can also be used to check out a specific commit. | git checkout branch-name: Switch branches
git checkout file.txt: Restore files to the working area
git checkout : Check out a specific commit | | git switch | Specifically used for switching branches; compared with git checkout it is more concise and intuitive, mainly used for branch operations. | git switch branch-name: Switch to the specified branch
git switch -c new-branch: Create and switch to a new branch | | git merge | Merge the changes from the specified branch into the current branch. | git merge branch-name: Merge the changes from the specified branch into the current branch | | git mergetool | Launch a merge tool to resolve merge conflicts. | git mergetool: Use the default merge tool to resolve conflicts
git mergetool --tool=: Specify the merge tool | | git log | Display the commit history records. | git log: Display commit history
git log --oneline: Display commit history in a concise mode | | git stash | Save uncommitted changes in the current working directory and restore it to a clean working area. | git stash: Save current changes
git stash pop: Restore the most recently saved changes
git stash list: List all saved changes | | git tag | Create, list, or delete tags. Tags are used to mark specific commits. | git tag: List all tags
git tag v1.0: Create a new tag
git tag -d v1.0: Delete a tag | | git worktree | Allow checking out multiple working directories in one repository, suitable for working on multiple branches at the same time. | git worktree add : Add a new working directory at the specified path and switch to the specified branch
git worktree remove : Delete a working directory |