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 master
branch. HEAD
Strictly speaking is not directed to submit, but the point master
, master
is the point of submission, so the HEAD
point is the current branch.
At the beginning, the master
branch is a line. Git master
points to the latest commit, and then HEAD
points master
to the current branch and the commit point of the current branch:
Each time you commit, the master
branch will move one step forward, so that as you continue to commit, master
the 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 master
same commit, and then HEAD
pointing to it dev
, it means that the current branch is dev
on:
You see, Git creates a branch very quickly, because apart from adding a dev
pointer and changing HEAD
the 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 dev
branch. For example, after a new submission, the dev
pointer moves one step forward, but the master
pointer does not change:
If our dev
work on the above is completed, we can dev
merge into the master
above. How does Git merge? The easiest way is to directly submit the current master
pointed to dev
to complete the 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 dev
branches. To delete a dev
branch is dev
to delete the pointer. After deleting, we are left with a master
branch:
It's amazing, can you see that some commits are done through branches?
Start the actual combat below.
First, we create a dev
branch, and then switch to the dev
branch:
$ git checkout -b dev
Switched to a new branch 'dev'
git checkout
The command plus the -b
parameter 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 branch
command to view the current branch:
$ git branch
* dev
master
git branch
The command will list all branches, and the current branch will be marked with a *
number.
Then, we can dev
commit normally on the branch, such as making readme.txt
a 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 dev
branch work is complete, we can switch back to the master
branch:
$ git checkout master
Switched to branch 'master'
After switching back to the master
branch, check another readme.txt
file, the content just added is gone! Because that commit is on the dev
branch, and master
the commit point of the branch at the moment has not changed:
Now, we dev
merge the work of the branch into the master
branch:
$ git merge dev
Updating d46f35e..b17d20e
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
git merge
The command is used to merge the specified branch to the current branch. After merging, check readme.txt
the content again, you can see that dev
it is exactly the same as the latest commit of the branch.
Fast-forward
Taking note of the above information, Git tells us that this merge is in "fast forward mode", that is , the current commit directly master
pointed dev
to, 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 dev
branch:
$ git branch -d dev
Deleted branch dev (was b17d20e).
After deleting, check branch
, 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 complete a task, and then delete the branch after merging. This master
is 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 switch
more scientific. Therefore, the latest version of Git provides new git switch
commands to switch branches:
To create and switch to a new dev
branch, you can use:
$ git switch -c dev
To switch directly to an existing master
branch, you can use:
$ git switch master
With the new git switch
commands, git checkout
it 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