Git pulls a specific remote branch

Git pulls a specific remote branch

There are two situations for pulling a specific remote branch code. The first is that there is no project locally, and the code of the remote master branch is pulled by using the git clone link name command. At this time, the remote branch needs to be specified; the second is that there is an existing project locally. However, there is no corresponding branch in the remote branch. At this time, you also need to specify the remote branch and create a local branch.

1. git clone specifies the remote branch

If no remote branch is specified, the code of the master branch is pulled by default. The format of the specified branch is as follows:

git clone -b dev https://gitee.com/test/qqw.git

-b means to choose a branch, **dev** is the branch name, followed by the warehouse address, so that the remote branch can be specified.

2. Pull the remote branch from the local existing project

In the case of an existing local project, it is not suitable to use git clone to pull the code again. At this time, it is necessary to pull the remote branch and automatically create a local branch. The command is as follows:

git checkout -b dev origin/dev

dev is a local branch, origin/dev is a remote branch, and the local branch will automatically establish an association with the remote branch

3. Other commands

3.0. View the correspondence between local branches and remote branches

git branch -vv

3.1. View remote branches

git branch -r

3.2. Create a local branch and associate a remote branch

git checkout -b 本地分支 origin/远程分支

3.3. Existing local branches create remote branch associations

git branch --set-upstream-to origin/远程分支名 本地分支名

3.4. Pull

git pull origin dev

4. Supplement

与远程代码仓库建立连接:git remote add origin 代码仓库地址

将远程分支拉到本地:git fetch origin dev(dev即分支名)

创建本地分支:git checkout -b LocalDev origin/dev (LocalDev 为本地分支名,dev为远程分支名)

*根据分支的变化,感觉这条指令可能是创建并切换到该分支

*最后一步将远程分支拉取到本地:git pull origin dev(dev为远程分支名)


一、 创建test分支提交步骤
1.列出所有分支:

        # git branch -a

2.创建test分支:

        # git branch test

3.切换到test分支:

        # git checkout test

4.添加add修改:

        # git add .

5.添加commit注释:

        # git commit -m "第一次提交代码"

6.提交到服务器:

        # git push origin test

二、将test分支合并到main本地分支
1.创建test分支:

        # git branch test

2.切换到test分支:

        # git checkout test

3.在test分支添加一个readme.txt文件:

        # touch readme.txt

        # git add readme.txt

4.提交commit本地文件:

        # git commit -m “增加readme.txt”

5.切换到main分支:

        # git checkout main

6.把test分支合并到main分支:

        # git merge test

7.提交main分支到服务器:

        # git push origin main

Guess you like

Origin blog.csdn.net/SweetHeartHuaZai/article/details/130148132