How to use Git branch

Table of contents

foreword

1. View available branches

2. Create a new branch

3. Switch to the new branch

4. Work on the new branch

5. Submit changes

6. Switch back to the main branch

Seven, delete the branch

8. Merge branches


foreword

Branches are different version lines within the same code repository. They can be used to simultaneously work on different development tasks, fix bugs, or implement new features without affecting the main codeline. Each branch can be modified, committed and merged independently.

1. View available branches

In a terminal or command prompt, navigate to the directory where your Git project is located, and use the cd command to switch directories;

You can use the following command to view the existing branches in the current warehouse:

git branch

This command lists all existing branches and marks the current branch with an asterisk.

2. Create a new branch

Create a new branch with the following command:

git branch <分支名>

For example, to create a new branch called branch1, you would run:

git branch branch1

3. Switch to the new branch

Once the branch is created, switch to the newly created branch with the following command:

git checkout <分支名>

Alternatively, you can use the following commands to create and switch to a new branch:

git checkout -b <分支名>

For example, to switch to a new branch called branch1, you can run:

4. Work on the new branch

After switching to the newly created branch, you can modify, add, delete files, etc. on the branch. Changes made on this branch will not affect other branches.

5. Submit changes

After making some modifications on the new branch, commit the changes to that branch with:

git add <文件名>
git commit -m "提交信息"

6. Switch back to the main branch

When you're done working on the new branch, you can switch back to the master branch or any other branch.

In Git, the master branch is often called the master or main branch. This is the default main development branch and is used to store the project's stable release and main codeline.

git checkout <主分支名>

Which name to use depends on your Git version and project settings, you can view the branch list by

Seven, delete the branch

1. Make sure you are not on the branch to be deleted, run the following command to confirm the current branch:

git branch

2. Switch to other branches: Select a branch that does not need to be deleted, such as the "master" branch, run the following command to switch branches:

git checkout master

3. Delete the branch: Now you can run the following command to try to delete:

git branch -d 删除分支名

4. Force delete branch: use force delete option -D:

git branch -D 删除分支名

8. Merge branches

1. Switch to the target branch to accept the changes; example: to merge the branch1 branch into the master branch, execute the following command:

git checkout master

2. Merge the target branch into the current branch, merge the branch1 branch into the master branch, and execute the following commands:

git merge branch1

3. When the merge is complete, you can use the git log command to view the history of the merge commit.

git log

 

Guess you like

Origin blog.csdn.net/m0_67906358/article/details/131351814
Recommended