Git Branch Management
Git branch management is one of Git's powerful features that allows multiple developers to work in parallel, develop new features, fix bugs, or run experiments 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 separate from the main development line and continue working without affecting the main line.
Git branches are actually pointers to change snapshots.
Some people call Git's branching modelSpecial move feature, and precisely because of it,Gitdistinguishing it from the family of version control systems.
Create branch
Create a new branch and switch to it:
git checkout -b <branchname>
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 last commit on that branch, 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 branch
Merge other branches into the current branch:
git merge <branchname>
For example, switch to the main branch and merge the feature-xyz branch:
git checkout main git merge feature-xyz
Resolve merge conflicts
When a conflict occurs during a merge, Git marks the conflicting files, and you need to resolve the conflict manually.
Open the conflicted file and resolve the conflict according to the markers.
Mark conflict resolution as complete:
git add <conflict-file>
Commit the merge result:
git commit
Delete branch
Delete local branch:
git branch -d <branchname>
Forcefully delete an unmerged branch:
git branch -D <branchname>
Delete remote branch:
git push origin --delete <branchname>
Example
Before we start, let's 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
List branches
Basic commands for listing branches:
git branch
Without arguments,git branchThis will list the branches in your local repository.
$ git branch * master
This example means that we have one calledmasterbranch, and that branch is the current branch.
When you rungit initBy default, Git will create for you whenmasterBranch.
If we want to manually create a branch, rungit branch (branchname)That's it.
$ git branch testing $ git branch * master testing
Now we can see that there is a new branchtesting。
When you create a new branch this way after the last commit, if there are later commits, and then you switch to thetestingbranch, Git will restore your working directory to what it was 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 'example.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 totestingbranch, the new file test.txt we added was removed. Switch back tomasterWhen branching, 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, allowing us to work within that branch.
$ git checkout -b newtest Switched to a new branch 'newtest' $ git rm test.txt rm 'test.txt' $ ls README $ touch example.php $ git add . $ git commit -am 'removed test.txt、add example.php' [newtest c1501a2] removed test.txt、add example.php 2 files changed, 1 deletion(-) create mode 100644 example.php delete mode 100644 test.txt $ ls README example.php $ git checkout master Switched to branch 'master' $ ls README test.txt
As you can see, we created a branch, removed the file test.txt on that branch, and added the example.php file, then switched back to our main branch. The deleted test.txt file returned, and the newly added example.php does not exist in the main branch.
Using branches to separate work allows us to work in different development environments and switch back and forth.
Delete branch
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
Merging branches
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 example.php | 0 test.txt | 1 - 2 files changed, 1 deletion(-) create mode 100644 example.php delete mode 100644 test.txt $ ls README example.php
In the example above, 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 conflict
Merging is not just a simple operation of adding or removing files; Git also merges modifications.
$ git branch * master $ cat example.php
Note:If example.php does not exist, you need to create this file first. The command can betouch example.php。
First, we create a branch called change_site, switch to it, and change the content of example.php to:
<?php echo 'example'; ?>
Create the change_site branch:
$ git checkout -b change_site Switched to a new branch 'change_site' $ vim example.php $ head -3 example.php <?php echo 'example'; ?> $ git commit -am 'changed the example.php' [change_site 7774248] changed the example.php 1 file changed, 3 insertions(+)
For vim command operations, please 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 what it was before our modification (empty file, no code). We modify the example.php file again.
$ git checkout master Switched to branch 'master' $ cat example.php $ vim example.php # 修改内容如下 $ cat example.php <?php echo 1; ?> $ git diff diff --git a/example.php b/example.php index e69de29..ac60739 100644 --- a/example.php +++ b/example.php @@ -0,0 +1,3 @@ +<?php +echo 1; +?> $ git commit -am '修改代码' [master c68142b] 修改代码 1 file changed, 3 insertions(+)
Now these changes have been recorded to my "master" branch. Next, we will merge the "change_site" branch in.
$ git merge change_site Auto-merging example.php CONFLICT (content): Merge conflict in example.php Automatic merge failed; fix conflicts and then commit the result. $ cat example.php # 打开文件,看到冲突内容 <?php <<<<<<< HEAD echo 1; ======= echo 'example'; >>>>>>> change_site ?>
When we merged the previous branch into the master branch, a merge conflict appeared, and now we need to manually modify it.
$ vim example.php $ cat example.php <?php echo 1; echo 'example'; ?> $ git diff diff --cc example.php index ac60739,b63d7d7..0000000 --- a/example.php +++ b/example.php @@@ -1,3 -1,3 +1,4 @@@ <?php +echo 1; + echo 'example'; ?>
In Git, we can use git add to tell Git that the file conflict has been resolved
$ git status -s UU example.php $ git add example.php $ git status -s M example.php $ git commit [master 88afe0e] Merge branch 'change_site'
Now we have successfully resolved the merge conflict and committed the result.
Command manual
| Command | Description | UsageExample |
|---|---|---|
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 branchesgit branch new-branch: Create a new branchgit branch -d old-branch: Delete branch |
git checkout |
Switch to the specified branch or restore files in the working directory. It can also be used to check out specific commits. | git checkout branch-name: Switch branchgit checkout file.txt: Restore files to the working areagit checkout <commit-hash>: Checkout a specific commit |
git switch |
Specifically used to switch branches, compared togit checkoutIt is more concise and intuitive, mainly used for branch operations. |
git switch branch-name: Switch to the specified branchgit switch -c new-branch: Create and switch to a new branch |
git merge |
Merge changes from the specified branch into the current branch. | git merge branch-name: Merge changes from the specified branch into the current branch |
git mergetool |
Launch the merge tool to resolve merge conflicts. | git mergetool: Use the default merge tool to resolve conflictsgit mergetool --tool=<tool-name>: Specify the merge tool |
git log |
Display the commit history. | git log: Show commit historygit log --oneline: Display commit history in concise mode |
git stash |
Save uncommitted changes in the current working directory and restore it to a clean working area. | git stash: Save current changesgit stash pop: Restore the most recently saved changesgit stash list: List all saved changes |
git tag |
Create, list, or delete tags. Tags are used to mark specific commits. | git tag: List all tagsgit tag v1.0: Create a new taggit tag -d v1.0: Delete tag |
git worktree |
Allows checking out multiple workspaces in one repository, suitable for working on multiple branches simultaneously. | git worktree add <path> branch-name: Add a new workspace at the specified path and switch to the specified branchgit worktree remove <path>: Delete the working directory |