Detailed explanation of branch management in Git tutorial
Git Branch Management
Almost every version control system supports branches in some form. Using branches means you can branch off from the main line of development and continue working without affecting the main line.
Some people call Git's branching model a "nirvana feature", and it is precisely because of it that it distinguishes Git from the version control system family.
Create branch command:
git branch (branchname)
Switch branch command:
git checkout (branchname)
When you switch branches, Git will use the last commit snapshot of the branch Replace the contents of your working directory, so multiple branches don't require multiple directories.
Merge branch command:
git merge
You can merge to the same branch multiple times, or you can choose to delete the merged branch directly after merging.
Git branch management
List branches
Basic command to list branches:
git branch
Without parameters, git branch will list your local branches.
$ git branch
* master
What this example means is that we have a branch called "master", and this branch is the current branch.
When you execute git init, Git will create the "master" branch for you by default.
If we want to manually create a branch and switch to it. 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 after the last commit update in this way, if there is an update commit later, and then switch to the "testing" branch, Git
Your working directory will be restored to the way 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 '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 testing Switched to branch 'testing' $ ls README hello.php
When we switched to the "testing" branch, the new file test.txt we added was removed,
The originally deleted file hello.php appeared again. When switching back to the "master" branch, they reappeared.
$ 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 the branch to operate in the branch.
$ git checkout -b newtest Switched to a new branch 'newtest' $ git rm test2.txt rm 'test2.txt' $ ls README test.txt $ git commit -am 'removed test2.txt' [newtest 556f0a0] removed test2.txt 1 file changed, 1 deletion(-) 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 in the context of that branch, then switched back to our master branch and those files are back.
Use branches to separate work, allowing us to do things in different contexts and switch back and forth.
Delete branch
Delete 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 Standalone content, you'll eventually want to merge it back into your master 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 test2.txt $ git merge newtest Updating 2e082b7..556f0a0 Fast-forward test2.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test2.txt $ ls README test.txt
In the above example, we merged the newtest branch into the main branch, and the test2.txt file was deleted.
Merge conflicts
Merge is not just a simple operation of adding and removing files, Git will also merge modifications.
$ git branch * master $ cat test.txt w3cschool.cc 首先,我们创建一个叫做"change_site"的分支,切换过去,我们将内容改为 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 -am 'changed the site' [change_site d7e7346] changed the site 1 file changed, 1 insertion(+), 1 deletion(-)
Submit the modified content to the "change_site" branch. Now, if we switch back to "master" In the branch, we can see that the content is restored to what it was before we modified it. We modify the test.txt file again.
$ git checkout master Switched to branch 'master' $ head -1 test.txt w3cschool.cc $ vim test.txt $ cat test.txt w3cschool.cc 新增加一行 $ git diff diff --git a/test.txt b/test.txt index 704cce7..f84c2a4 100644 --- a/test.txt +++ b/test.txt @@ -1 +1,2 @@ w3cschool.cc +新增加一行 $ git commit -am '新增加一行' [master 14b4dca] 新增加一行 1 file changed, 1 insertion(+)
Now these changes have been recorded in my "master" branch. Next we 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 新增加一行 ======= www.w3cschool.cc >>>>>>> change_site
We merged the previous branch into the "master" branch, and a merge conflict appeared. Next, we need to modify it manually.
$ vim test.txt $ cat test.txt www.w3cschool.cc 新增加一行 $ git diff diff --cc test.txt index f84c2a4,bccb7c2..0000000 --- a/test.txt +++ b/test.txt @@@ -1,2 -1,1 +1,2 @@@ - w3cschool.cc + www.w3cschool.cc +新增加一行
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 88afe0e] Merge branch 'change_site'
Now we have successfully resolved the conflict in the merge and submitted the result.
The above is the detailed explanation of branch management in Git tutorial. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

Git code merge process: Pull the latest changes to avoid conflicts. Switch to the branch you want to merge. Initiate a merge, specifying the branch to merge. Resolve merge conflicts (if any). Staging and commit merge, providing commit message.

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

How to update local Git code? Use git fetch to pull the latest changes from the remote repository. Merge remote changes to the local branch using git merge origin/<remote branch name>. Resolve conflicts arising from mergers. Use git commit -m "Merge branch <Remote branch name>" to submit merge changes and apply updates.

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.
