Git create and merge branches [reproduced]

Create and merge branches


In the version rollback , you already know that every time you commit, Git will string them into a timeline, and this timeline is a branch. Up to now, there is only one timeline. In Git, this branch is called the master masterbranch. HEADStrictly speaking is not directed to submit, but the point master, masteris the point of submission, so the HEADpoint is the current branch.

At the beginning, the masterbranch is a line. Git masterpoints to the latest commit, and then HEADpoints masterto the current branch and the commit point of the current branch:

git-br-initial

Each time you commit, the masterbranch will move one step forward, so that as you continue to commit, masterthe line of the branch becomes longer and longer.

When we create a new branch, for example dev, Git creates a new pointer called dev, pointing to the mastersame commit, and then HEADpointing to it dev, it means that the current branch is devon:

git-br-create

You see, Git creates a branch very quickly, because apart from adding a devpointer and changing HEADthe point, there is no change in the files in the workspace!

However, from now on, the modification and submission of the workspace is for the devbranch. For example, after a new submission, the devpointer moves one step forward, but the masterpointer does not change:

git-br-dev-fd

If our devwork on the above is completed, we can devmerge into the masterabove. How does Git merge? The easiest way is to directly submit the current masterpointed to devto complete the merge:

git-br-ff-merge

So Git merges branches quickly! Just change the pointer, the content of the workspace is also unchanged!

After merging branches, you can even delete devbranches. To delete a devbranch is devto delete the pointer. After deleting, we are left with a masterbranch:

git-br-rm

It's amazing, can you see that some commits are done through branches?

Start the actual combat below.

First, we create a devbranch, and then switch to the devbranch:

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

git checkoutThe command plus the -bparameter means to create and switch, which is equivalent to the following two commands:

$ git branch dev
$ git checkout dev
Switched to branch 'dev'

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

$ git branch
* dev
  master

git branchThe command will list all branches, and the current branch will be marked with a *number.

Then, we can devcommit normally on the branch, such as making readme.txta modification and adding a line:

Creating a new branch is quick.

Then submit:

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

Now that the devbranch work is complete, we can switch back to the masterbranch:

$ git checkout master
Switched to branch 'master'

After switching back to the masterbranch, check another readme.txtfile, the content just added is gone! Because that commit is on the devbranch, and masterthe commit point of the branch at the moment has not changed:

git-br-on-master

Now, we devmerge the work of the branch into the masterbranch:

$ git merge dev
Updating d46f35e..b17d20e
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

git mergeThe command is used to merge the specified branch to the current branch. After merging, check readme.txtthe content again, you can see that devit is exactly the same as the latest commit of the branch.

Fast-forwardTaking note of the above information, Git tells us that this merge is in "fast forward mode", that is , the current commit directly masterpointed devto, so the merge speed is very fast.

Of course, not every time you merge Fast-forward, we will talk about other ways of merging later.

After the merge is complete, you can safely delete the devbranch:

$ git branch -d dev
Deleted branch dev (was b17d20e).

After deleting, check branch, only the masterbranch is left :

$ git branch
* master

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

switch

We noticed that switching branches are used git checkout <branch>, while the undo modification mentioned earlier is git checkout -- <file>that the same command has two functions, which is indeed a bit confusing.

In fact, the action of switching branches is switchmore scientific. Therefore, the latest version of Git provides new git switchcommands to switch branches:

To create and switch to a new devbranch, you can use:

$ git switch -c dev

To switch directly to an existing masterbranch, you can use:

$ git switch master

With the new git switchcommands, git checkoutit is easier to understand than .

summary

Git encourages extensive use of branches:

View branch:git branch

Create a branch:git branch <name>

Switch branches: git checkout <name>orgit switch <name>

Create + switch branch: git checkout -b <name>orgit switch -c <name>

Merge a branch to the current branch:git merge <name>

Delete branch:git branch -d <name>

 Thanks: Author 

Guess you like

Origin blog.csdn.net/admin123404/article/details/106710743