Git Study Notes <branch management> (3)

Source: Internet
Author: User

What is a branch?

Branches are like branches separated by trees. The difference is that they can be merged with each other.

Think of version promotion as an extension of a linked list: Version 1.0 =, version 2.0 =, version3.0.

Master is the main branch that is basically used to publish products. You can separate a Dev from the master, create new functions on it, fix bugs, and debug them. And then merge it into the master. As shown below.

 

Master branch: Version 1.0 =========" version 2.0 ====...

\/(Merge)

Dev branch: \ = version 1.5 ============...

 

You can even have more branches.


Master branch: Version 1.0 =========" version 2.0 ====...

\/(Merge)

Dev branch: \ → o =...

\/(Merge)

Hanson branch: \-> bug fixing->/

 

Branch deletion does not disappear.

(In addition, the current version can be considered as a pointer pointing to the last node of the linked list. The reset (chapter 2) command is to move the Pointer Forward, And the revert command is still backward. )

 

How to manage branches?

 

1. Create and view branches:

Now we want to create a branch named Dev. (Dev is mostly used for developing software in software development. It is similar to the beta version. Its idea is "development version", that is, development version or development edition .) The command to create a branch is$ Git branch DevThe command to move to the specified branch is$ Git checkout Dev

There is also an easy way to combine the two commands:$ Git checkout-B DevIn this way, you can directly create and go to the dev branch. (Equivalent to a copy of the current master version .)

In git bash on Windows, you can use the brackets at the end of each line of command to determine which branch is now. Not in Linux.

 

ThatHow can I check which branches are available and which ones are determined?Input:$ Git BranchAll branches are listed. The asterisks before the branches are the current branches.

$ Git Branch

* Dev

Master

 

2. Merge and delete branches:

Modify the workspace content and use$ Git add hello.txtAnd$ Git commit hello.txt-M "modify"Upload the dev Branch

Then$ Git checkout masterAfter switching to the master branch, enter$ Git merge DevIn this way, Dev will be integrated into the master.

If you want to delete the dev branch input$ Git branch-D DevYou can. (If this branch is not merged, you want to discard it.Need to use$Git branch-D Dev)

 

When using the merge command, you can add the parameter-M "blabla", that is, add remarks.

Another parameter is-- No-FF:

As the name suggests, this parameter means that the FF mode is not required.

FF is fast forward. In this mode, branch information is lost after the branch is deleted. Just as this branch has never existed before.

If -- no-FF is used, the example at the beginning will be shown. After the Hanson branch is deleted, it will still exist in the record. A new commit will be generated at merge.

This command will be like this

git merge --no-ff -m "merge with no-ff" Hanson

There are also some principles to be observed

The master branch should be very stable, that is, it is only used to release new versions and cannot work on it at ordinary times;

Where can I work? All work is on the dev branch. That is to say, the dev branch is unstable. at a certain time, for example, when version 1.0 is released, the dev branch is merged to the master, release Version 1.0 on the master branch;

Everyone you and your friends work on the dev branch. Everyone has their own branch. You can merge them on the dev branch from time to time.

Maybe you have come up with this question."What if there is a conflict during the merger ?"

 

For example, the two branches modify the same row in the same way. On the master, you changed the last line of hello.txt and submitted the last line of hello.txt on the devserver.

Git does this:Git found such a conflict during the merge, so it had to try to combine the two rows and prompt you:

Auto-merging hello.txt

Conflict (content): Merge conflict in hello.txt

Automatic merge failed; fix conflicts and then commit the result.

You know from this that you need to manually change the file merging and then submit.

Input$ Git statusView conflicting files

# On branch master # Your branch is ahead of 'origin/master' by 1 commits. (this statement indicates that the current master has a commit for the master of the remote warehouse.) # unmerged paths: # (use "Git Add/Rm <File>... "as appropriate to mark resolution) # both modified: hello.txt # No changes added to commit (use" Git Add "and/or" Git commit-")

Open the corresponding hello.txt file and find that the two lines you modified exist in the file in this form:

<Head ......... ...... ========== ......... Place…… >>>>>>> Dev

In this way, the content in different branches is marked and you need to manually change it and submit it with $ git add hello.txt and commit-M.

Use the $ git log with parameters to view the merging information. Take note of the parameters later.

$ git log --graph --pretty=oneline --abbrev-commit

 

3. Temporary Storage work site

You often encounter a situation where you are creating a new function on your own branch, but there is a bug that needs to be solved within an hour. It is impossible for you to directly switch to a new branch to fix the bug, so that you will not have to do your work for a long time. It is also impossible to fix and submit on this branch, because it will take a long time for you to finish writing new functions.

 

Input$ Git stash save "work in progress for foo feature"(The save command is used for remarks, not required.) You will find that the workspace is changed to the last submission of the current branch. At this time, you can safely create a branch to fix bugs.

After the fix, you want to go back to Dev to continue working!

 

Input$ Git stash list

The following shows the stash list, such as [email protected] {0}: WIP on Dev :............

How to restore it? There are two methods:

First, useGit stash applyAfter recovery, the stash content is not deleted. You need to useGit stash dropTo delete,

By default, the last save is restored. If you want to save it multiple times, you want to restore the previously saved site. You have to find the marker in front of the stash list for restoration, like this

Git stash apply [email protected] {1};

Another method is to use$Git stash popAnd delete the stash content at the same time. This method can only restore the last stash.

 

$ Git stash clearUsed to clear the stash list.

Git Study Notes <branch management> (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.