Super-detailed graphic explanation: how a code warehouse manages multiple projects, and the code interaction does not affect it. Use of the orphan branch

There is a personal public account at the bottom of the article: Xiao Zheng who loves technology. Mainly share development knowledge, if you are interested, you can pay attention. Why share? There is no need for others to step on the pits that have been stepped on, and replaying the game by yourself can also deepen the memory. Self-interest and others, the so-called win-win situation.

foreword

How to use code warehouse to manage multiple projects at the same time? When I wrote a personal project before, it was generally a code warehouse to manage a set of codes, so that as the project increased, there would be more and more code warehouses. If it is a project with front and back ends separated, there is one warehouse for the front end and another warehouse for the back-end code, which is very inconvenient to manage. I also wrote an article before about how a code warehouse manages multiple projects, but that management method has a big drawback. When submitting code, the code of all projects is submitted on a historical line. Instead of managing each project individually

This article will completely solve this problem, and I have checked many methods on the Internet. It doesn't feel very detailed. I will not only give a detailed method here, but also verify it with actual code. And the problems that may be encountered in the actual application of the code.

I take GitHub as an example here

premise

Explain today's protagonist: orphan

Use the parameter --orphan. This parameter has two main functions. One is to copy all the files of the current branch, and the other is to have no parent node, which can be understood as a branch with a completely independent background and clean background .

The core purpose of git checkout --orphan is to create a branch in a git init-like state on a non-new repository.

Without this feature, all of your git branches would have a common ancestor, which is your initial commit. This is a very common situation, but by no means the only one. For example, git allows you to have multiple independent projects as Different branches in a single repository to track.

1. Create a code warehouse

If you don't know how to create a code warehouse, please read a blog I wrote before:
This article teaches you how to create a warehouse in Github?

Created code repository
insert image description here

2. How to use

2.1 Clone the project to local

Create exercises with local projects and remote projects, and you can also create exercises in other ways. Here is a simpler way to directly clone the project to the local

First copy the project address
insert image description here

Clone the project to a local location

git clone 项目地址

insert image description here

2.2 Create an orphan branch

Created on demand, here I need to create three branches to separately store the back-end code, merchant front-end, and customer front-end of a project. Three sets of code, so I'm going to create three branches.

In general, the simple use of the orphan branch has the following commands:

# 1.创建orphan分支(注意创建了分支必须提交文件到该分支下,否则其实没有创建成功)
git checkout --orphan 分支名
# 2.提交到orphan分支下
git add .
git commit -m"desc"
# 提交三步曲只有这步不一样,需要指定分支
git push origin 分支名(确保一致)
# 3.切换分支,比如切换到golang分支(切换了文件会发生变化,只会显示该分支下的文件)
git checkout golang

Here I demonstrate the process of creating a branch. Let me focus on this: If you created a branch, but did not submit the branch to the remote. When you use the command git branchto view the branch, you can't see the branch you just created. You must submit before you can create a branch locally, and after submitting, the remote will display the created branch.
Some commands used
1. View branch: git branch. 2. Switch branch: git checkout 分支名. 3. Add file temporary storage: git add .. 4. Submit information description: git commit -m '提交信息描述'. 5. Submit to remote git push origin 分支名.

insert image description here
insert image description here
insert image description here

2.3 Remote warehouse branch situation

insert image description here

2.4 Submit the project code to the corresponding branch

Here is the project code I have written, which was stored separately in three code warehouses before, which is difficult to maintain. Put it in the same code warehouse here. Then put the three projects into the corresponding code branches.
insert image description here

Submit a set of back-end codes to the created back-end code branch to save. The process is relatively simple. After switching to this branch, add the code to be submitted to this branch, and then commit and push to this branch. The specific process is as follows:

insert image description here
insert image description here

insert image description here

Other branch code will not change
insert image description here

Submit another set of code to the code warehouse

Submit another set of code and switch branches. git checkout 分支名.The
specific submission process is as follows. You can also use the git visualization tool to operate, just submit the code to the remote branch.

insert image description here
insert image description here
insert image description here

2.5 Different branches do not affect each other

insert image description here
insert image description here

3. Problems encountered

3.1 After switching branches, other codes are not visible?

When I excitedly opened the editor to view the code, I found that only the code under the current branch was visible. The codes of other branches are not visible. This creates a serious problem. When developing the front-end and back-end separately, I need to open both the front-end and back-end projects. Now I can only open one. See 3.2 for specific solutions

insert image description here

The situation after switching branches
insert image description here

3.2 Solve the problem of invisible branches (cloning the code under a certain branch)

I also clone the project to another location, and then pull the code under this branch separately. This will solve the problem. At the same time, it has been achieved. The code under a certain branch can be cloned separately without cloning the entire project. In this way, in the code repository, one of the multiple projects can be cloned and managed.

Specific process
Steps:
1. Clone the project to the local (change location)
2. Pull the specified branch code

Since the branch has been opened remotely, just pull it down locally

git checkout -b feature-branch origin/feature-branch    //检出远程的feature-branch分支到本地

insert image description here
There are only two branches locally. Of course, you can also pull other branch codes to this directory, but it becomes the same situation as before. Only the code of the current branch can be viewed, and others cannot be seen. At present, I only think of this method. If you have a good method, you can share it in the comment area.

insert image description here

In this way, you can see the code under different branches, and it is convenient to modify it yourself.

Front-end project code
insert image description here

Backend project code
insert image description here

3.3 Idea file changes, file color does not change

A major role of version control is to facilitate us to view the changes made to the project code. As a result, I encountered a little problem here. After modifying the code file, the color of the file did not change. Not sure if it's because the project was already under version control.

Solution , reload the address of version control
insert image description here
insert image description here
insert image description here

4. View the branch code submission status

The history of the backend here will only see the commit status of the current project. Doesn't care about commits from other branches.
insert image description here

Using the Vscode plug-in can be seen more clearly, here is the code submission status of all branches.
insert image description here
insert image description here
insert image description here

5. View the submission status of the remote warehouse

insert image description here
insert image description here

6. Different branch code changes without affecting each other

Like developing a single project, there is no need to consider the impact of other project code changes.
insert image description here

This is a big disadvantage of managing multiple projects in one code warehouse. One of the project's code changes, if not submitted in time. When other projects submit code, they will submit this unsubmitted piece. And the impact of file changes is also very troublesome. Very bad for development.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43304253/article/details/132031248