Detailed explanation of Git branch operations: create, commit, and merge the main branch

        Git is a powerful distributed version control system. Branching is one of its core features, providing flexibility for team collaboration and project management. This article will introduce the basic usage of Git branches, including operations such as creating branches, committing changes, and merging the main branch.

1. Create a branch

        In Git, branches are different lines of project development that allow teams to develop in parallel without affecting the main line (master branch). The command to create a branch is as follows:

# 创建一个新分支
git branch new-feature

# 切换到新分支
git checkout new-feature
# 或者使用以下命令创建并切换到新分支
# git checkout -b new-feature

The above command creates a new branch named new-feature and switches the working directory to that branch.

2. Submit changes

        After working on a new branch, you need to commit the changes to the version control system. Here is the basic command to commit changes:

# 添加所有更改到暂存区
git add .

# 提交更改到本地仓库
git commit -m "Add new feature"

# 推送更改到远程仓库
git push origin new-feature

The above command adds the changes to the staging area, then commits them to the local repository, and finally pushes them to the remote repository.​ 

3. Merge the main branch 

        Once the work is completed on the new branch, it usually needs to be merged back to the main branch. Use the following command to merge branches:

# 切换回主分支
git checkout main

# 合并新分支到主分支
git merge new-feature

# 推送主分支到远程仓库
git push origin main

The above command switches the working directory back to the master branch, then merges the new-feature branch into the master branch, and pushes the merged changes to the remote warehouse.

4. Resolve conflicts 

        During the branch merging process, if both branches modify the same part of the code, conflicts may occur. The steps to resolve conflicts are as follows:

        ​ ​ 1. Git will mark conflicting files. Manually edit the file to resolve conflicts.

        ​ ​ 2. Add the conflict-resolved files to the temporary storage area:

git add <conflicted-file>

        ​ ​ 3. Submit the changes after resolving the conflict:

git commit -m "Resolve merge conflict"

        4. Continue to merge:

git merge new-feature

5. Delete branch

        After merging branches, you can delete branches that are no longer needed:

# 删除本地分支
git branch -d new-feature

# 删除远程分支
git push origin --delete new-feature

New Era Migrant Workers

Guess you like

Origin blog.csdn.net/sg_knight/article/details/134373559