Git creates local branches and associates remote branches (project code backup)

1. Create a local branch

git branch 分支名

For example: git branch dev, this command is the local branch created based on the current branch, assuming the local branch is main, it is the local branch dev created based on the main branch.

2. Switch to the local branch

git checkout 分支名

For example: git checkout dev, this command means switching from the current main branch to the dev branch.

3. Create a local branch and switch

git checkout -b 分支名

For example: git checkout -b dev, this command combines the functions of creating a local branch and switching to this branch, that is, creating a local branch dev based on the current branch master and switching to this branch.

4. Submit the local branch to the remote warehouse

git push origin 本地分支名

For example: git push origin dev, this command indicates that the local dev branch is submitted to the remote warehouse, that is, the remote branch dev is created.
Note: To share a local branch with others, you need to push it to a remote repository to which you have write access. The local branch you created will not be automatically synchronized to the remote server you introduced because of your write operation, you need to explicitly perform the operation of pushing the branch. In other words, for branches that you don't intend to share, you can keep them as private branches, and only push those feature branches that are used for collaborative work.

5. Create a new local branch and remote branch

git branch –set-upstream 本地新建分支名 origin/远程分支名
或者 git branch –set-upstream-to=origin/远程分支名
例:git branch --set-upstream-to=origin/dev  dev

For example: git branch –set-upstream-to=origin/dev, associate the local dev branch with the remote dev branch.
Note: After creating a new branch locally and pushing it to the remote server, an error will be reported when using git pull or git pull to pull or submit data. You must use the command: git pull origin dev (specify the remote branch); if you want to directly use git pull or git push to pull and submit data, you must create an association between the local branch and the remote branch

Guess you like

Origin blog.csdn.net/renfeideboke/article/details/130930418