Git associates remote warehouses and branches

background

In our development process, we often put local code in the code warehouse.

There are two solutions, both of which have a premise that a new code warehouse needs to be created in gitLab.

The first method is to pull the new remote code warehouse to the local first, then transplant the local code to the local warehouse, and finally push it to the remote warehouse.

Another way is to directly associate the local code with the remote code warehouse. We will focus on this approach below.

prerequisite

Install and configure the git environment locally.

Use the git command to associate remote warehouses and branches

1. Add git index to local project

Execute the following command in the root directory of the project

$ git init

After the command is executed, a default code branch (master) will be created locally, and the code is in an uncommitted state.

At this point, you can use the following command to view the status of the current code

$ git status

2. Create a local branch

By default, the local branch name is master. If you need to create a new branch, you can use the following command

$ git checkout -b common

3. Submit the code

After the local branch is created, the code can be committed to the local warehouse first. Use the following command:

$ git add -A
$ git commit -m "<提交信息>"

4. View the remote warehouse

$ git remote -v

This command will display the added remote code. If you have not associated the code warehouse, the following information will be empty.

5. Add remote warehouse

$ git remote add <自定义仓库名><仓库地址>

After the remote code warehouse is added, you can use the view remote warehouse command to view the associated code warehouse.

 You can use the following command to view information about remote warehouses:

$ git remote show

6. Pull the warehouse branch information:

$ git fetch <仓库名>
或者
$ git remote update // 更新所有仓库,后面可以跟 --prune,表示清理本地仓库中失效的远程分支,注意,不是本地自己创建的分支

7. Associate local code and remote warehouse branches

If the warehouse already has a specified branch, use the following command to associate it:

$ git branch --set-upstream-to=<仓库名>/<分支名>

After the local warehouse is associated with the remote warehouse branch, you can use the following command to view the branch association

$ git branch -vv

At this point, the local code has been successfully associated with the remote warehouse.

8. Delete the warehouse

$ git remote remove <仓库名>
或者
$ git remote rm <仓库名>

9. Rename the repository

$ git remote rename <旧名字> <新名字>

Guess you like

Origin blog.csdn.net/HaveFun_Wine/article/details/127074637