YouTip LogoYouTip

Git Branch

Git branch management is one of Git's powerful features, allowing multiple developers to work in parallel, develop new features, fix bugs, or conduct experiments without affecting the main codebase. Almost every version control system supports branching in some form, where a branch represents an independent development line. Using branches means you can separate yourself from the main development line and continue working without affecting the mainline. !(https://static.jyshare.com/images/svg/git-brance.svg) In Git, a branch is essentially a pointer to a snapshot of changes. Some people call Git's branching model a **killer feature**, and it is precisely this feature that distinguishes **Git** from other version control systems. ### Creating a Branch Create a new branch and switch to it: git checkout -b For example: git checkout -b feature-xyz To switch branches, use the following 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 you don't need separate directories for each branch. ### Viewing Branches View all local branches: git branch View remote branches: git branch -r View all local and remote branches: git branch -a ### Merging 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 conflicts arise during a merge, Git marks the conflicting files, and you need to resolve them manually. Open the conflicted files and resolve the conflicts according to the markers. Mark the conflict resolution as complete: git add Commit the merged 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 First, let's create a test directory: $ mkdir gitdemo $ cd gitdemo/ $ git init Initialized empty Git repository... $ touch README $ git add README $ git commit -m 'First commit'[master (root-commit) 3b58100] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README * * * ## Git Branch Management ### Listing Branches The basic command to list branches is: git branch Without any arguments, `git branch` lists your local branches. $ git branch * master This means we have a branch called **master**, which is currently checked out. By default, Git creates a **master** branch when you run `git init`. If you want to create a branch manually, simply execute `git branch (branchname)`. $ git branch testing $ git branch * master testing Now we can see that a new branch called **testing** has been created. When you create a new branch after the latest commit and later switch to the **testing** branch, Git restores your working directory to the state at the time the branch was created. Next, we'll demonstrate how to switch branches using `git checkout (branch)`. $ ls README $ echo '.com' > test.txt $ git add . $ git commit -m 'add test.txt' 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 switched to the **testing** branch, the newly added file `test.txt` disappeared. When we switched back to the **master** branch, it reappeared. $ git checkout master Switched to branch 'master' $ ls README test.txt You can also use the command `git checkout -b (branchname)` to create a new branch and immediately switch to it for further operations. $ git checkout -b newtest Switched to a new branch 'newtest' $ git rm test.txt rm 'test.txt' $ ls README $ touch .php $ git add . $ git commit -am 'removed test.txt、added .php' removed test.txt、added .php 2 files changed, 1 deletion(-) create mode 100644 .php delete mode 100644 test.txt $ ls README .php $ git checkout master Switched to branch 'master' $ ls README test.txt As you can see, we created a branch, removed some files like `test.txt`, added `.php`, then switched back to the main branch. The deleted `test.txt` reappeared, while the newly added `.php` did not exist on the main branch. Using branches allows us to isolate our work, enabling us to operate in different development environments and switch back and forth as needed. ### Deleting Branches The command to delete a branch is: git branch -d (branchname) For example, 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 its own content, you will eventually want to merge it back into your main branch. 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 .php | 0 test.txt | 1 - 2 files changed, 1 deletion(-) create mode 100644 .php delete mode 100644 test.txt $ ls README .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 about adding or removing files; Git also merges modifications. $ git branch * master $ cat .php **Note:** If `.php` does not exist, create it first using the command `touch .php`. First, create a branch named `change_site`, switch to it, and modify the content of `.php` to: Create the `change_site` branch: $ git checkout -b change_site Switched to a new branch 'change_site' $ vim .php $ head -3 .php $ git commit -am 'changed the .php' changed the .php 1 file changed, 3 insertions(+) Refer to [Linux vi/vim](#) for instructions on using the `vim` command. Submit the modified content to the `change_site` branch. Now, if we switch back to the `master` branch, we'll find the content restored to its original state (an empty file with no code). We'll modify `.php` again. $ git checkout master Switched to branch 'master' $ cat .php $ vim .php # Modify the content as follows $ cat .php $ git diff diff --git a/.php b/.php index e69de29..ac60739 100644--- a/.php +++ b/.php @@ -0,0 +1,3 @@+ $ git commit -am 'modified the code' modified the code 1 file changed, 3 insertions(+) Now these changes are recorded in my "master" branch. Next, we'll merge the "change_site" branch. $ git merge change_site Auto-merging .php CONFLICT (content): Merge conflict in .php Automatic merge failed; fix conflicts and then commit the result. $ cat .php # Open the file and see the conflicting content<?php <<<<<<>>>>>> change_site ?> We attempted to merge the previous branch into the master branch, resulting in a merge conflict that requires manual resolution. $ vim .php $ cat .php $ git diff diff --cc .php index ac60739,b63d7d7..0000000--- a/.php +++ b/.php @@@ -1,3 -1,3 +1,4 @@@ In Git, you can use `git add` to inform Git that the conflict has been resolved. $ git status -s UU .php $ git add .php $ git status -s M .php $ git commit Merged branch 'change_site' Now we've successfully resolved the merge conflict and committed the result. * * * ## Command Reference | **Command** | **Description** | **Usage Examples** | | --- | --- | --- | | `git branch` | Lists, creates, or deletes branches. It does not switch branches but is used for managing their existence. | `git branch`: List all branches `git branch new-branch`: Create a new branch `git branch -d old-branch`: Delete a branch | | `git checkout` | Switches to a specified branch or restores files in the working directory. Can also be used to check out specific commits. | `git checkout branch-name`: Switch to a branch `git checkout file.txt`: Restore a file to the working directory `git checkout `: Check out a specific commit | | `git switch` | Specifically designed for switching branches, more concise and intuitive than `git checkout`, mainly used for branch operations. | `git switch branch-name`: Switch to a specified branch `git switch -c new-branch`: Create and switch to a new branch | | `git merge` | Merges changes from a specified branch into the current branch. | `git merge branch-name`: Merge changes from a specified branch into the current branch | | `git mergetool` | Launches a merge tool to resolve merge conflicts. | `git mergetool`: Use the default merge tool to resolve conflicts `git mergetool --tool=`: Specify a particular merge tool
← Git Commit HistoryGit Workflow β†’