Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Git Branch Management _ Power Node Java College arrangement

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The branch is the parallel universe in science fiction movies. When you are studying Git in front of your computer, you are studying SVN in another parallel universe.

If the two parallel universes don't interfere with each other, it won't affect you now. At some point, however, the two parallel universes merge, and as a result, you learn both Git and SVN!

What is the use of branches in practice? Suppose you are ready to develop a new feature, but it will take two weeks to complete. In the first week, you have written 50% of the code. If you submit it immediately, the incomplete code base will make it impossible for others to work because the code is not finished. If you wait for the code to be written and submitted again, there is a huge risk of losing daily progress.

Now that there are branches, there is no need to be afraid. You create your own branch, others can not see, but also continue to work on the original branch, while you work on your own branch, you want to submit, until the completion of development, and then merge into the original branch at one time, in this way, it is safe and does not affect the work of others.

Other version control systems such as SVN have branch management, but after using it, you will find that these version control systems create and switch branches more slowly than snails, which is simply unbearable. As a result, the branch function has become a device, and no one uses it.

But the branch of Git is different, regardless of creating, switching, and deleting branches, Git can be completed in less than 1 second! Whether your version library is 1 file or 10, 000 files.

Create and merge branches

Each time a submission is submitted, Git strands them into a timeline, which is a branch. So far, there is only one timeline. In Git, this branch is called the main branch, that is, the master branch. Strictly speaking, HEAD does not point to the commit, but to the master,master points to the commit, so HEAD points to the current branch.

At the beginning, the master branch is a line, and Git points to the latest submission with master, and then to master with HEAD, you can determine the current branch and the submission point for the current branch:

Each time you commit, the master branch moves forward so that as you continue to commit, the line of the master branch gets longer and longer:

When we create a new branch, such as dev, Git creates a new pointer called dev, which points to the same commit as master, and then points HEAD to dev, indicating that the current branch is on dev:

You see, Git creates a branch very quickly, because except for adding a dev pointer and changing the point of HEAD, there is no change in the files in the workspace!

From now on, however, modifications and commits to the workspace are for the dev branch. For example, after a new commit, the dev pointer moves forward one step, while the master pointer remains unchanged:

If our work on dev is done, we can merge dev into master. How does Git merge? The easiest way is to point the master directly to the current commit of dev to complete the merge:

So Git merges branches very quickly! If you change the pointer, the content of the work area will not change!

After merging branches, you can even delete dev branches. To delete a dev branch is to delete the dev pointer. After deleting it, we have only one master branch left:

It's amazing. Can you see that some submissions are done through branches?

Let's start the actual combat.

First, we create the dev branch, and then switch to the dev branch:

$git checkout-b devSwitched to a new branch 'dev'

The git checkout command plus the-b parameter means create and switch, which is equivalent to the following two commands:

$git branch dev$ git checkout devSwitched to branch 'dev'

Then, use the git branch command to view the current branch:

$git branch* dev master

The git branch command lists all branches, and the current branch is preceded by a * sign.

Then we can commit normally on the dev branch, such as making a change to readme.txt and adding a line:

Creating a new branch is quick.

Then submit:

$git add readme.txt $git commit-m "branch test" [dev fec145a] branch test 1 file changed, 1 insertion (+)

Now that the work of the dev branch is complete, we can switch back to the master branch:

$git checkout masterSwitched to branch 'master'

After switching back to the master branch, look at a readme.txt file, and the content you just added is gone! Because that commit is on the dev branch, and the commit point of the master branch has not changed at this point:

Now, let's merge the work of the dev branch into the master branch:

$git merge devUpdating d17efd8..fec145aFast-forward readme.txt | 1 + 1 file changed, 1 insertion (+)

The git merge command is used to merge the specified branch into the current branch. After the merge, if you look at the contents of readme.txt, you can see that it is exactly the same as the latest submission from the dev branch.

Noticing the Fast-forward information above, Git tells us that this merge is in "fast-forward mode", that is, pointing the master directly to the current submission of dev, so the merge is very fast.

Of course, not every merger can be Fast-forward, we will talk about other ways of merger later.

After the merge is complete, you can safely delete the dev branch:

$git branch-d devDeleted branch dev (was fec145a).

After deletion, check the branch, and only the master branch is left:

$git branch* master

Because creating, merging, and deleting branches is very fast, Git encourages you to use branches to accomplish a task, and then delete branches after merging, which is the same as working directly on master branches, but the process is safer.

Summary

Git encourages heavy use of branches:

View branches: git branch

Create a branch: git branch

Switch branches: git checkout

Create + switch branches: git checkout-b

Merge a branch into the current branch: git merge

Delete branch: git branch-d

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report