Lite Git (IV) - Branch
preface
This column is called Lite Git. I mainly want to correspond to Pro Git, which is the official guide of Git. If you are interested or want to know more details, please move on Download PDF version from the official website.
The purpose of this column is to enable the novice students to master the basic application of Git faster and more reasonably;
At the same time, this column will also introduce what Android developers care about: the use of repo;
This is the third article in this column, which mainly introduces the creation and deletion of Git branches. You can skip this article if you already know about it;
Branch concept
Before introducing the concept of branch, first introduce the command git log:
Although we used git log to view the submission information in the previous section, the standard of its displayed content is not defined in detail. In fact, GIT log can display the tree history starting from the specified endpoint.
Here are some key points:
- Specify the endpoint, that is, the hash value of the submission. If it is not specified, it defaults to the submission pointed to by HEAD;
- Tree history, that is, the displayed history submission must be a submission node that starts from the specified endpoint and can be reached from top to bottom;
Or demo_ Taking the client warehouse as an example, we made two submissions in the previous section. Therefore, using git log here, we should see the information of the two submissions:
ryan ~/git_demo/demo_client (master) $ git log commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 (HEAD -> master) Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA
More parameters of git log will be introduced separately later. Here, you can understand the concept of tree structure first;
Before I explain the branch, I need to review the demo_ The content of the client repository needs some filling to better illustrate the problem:
# Create several files ryan ~/git_demo/demo_client (master) $ touch FileA FileB FileC FileD FileE # View workspace ryan ~/git_demo/demo_client (master) $ ll total 12 drwxr-xr-x 3 ryan ryan 4096 Oct 28 10:57 ./ drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../ drwxr-xr-x 8 ryan ryan 4096 Oct 28 10:57 .git/ -rw-r--r-- 1 ryan ryan 0 Oct 28 10:57 FileA -rw-r--r-- 1 ryan ryan 0 Oct 28 10:57 FileB -rw-r--r-- 1 ryan ryan 0 Oct 28 10:57 FileC -rw-r--r-- 1 ryan ryan 0 Oct 28 10:57 FileD -rw-r--r-- 1 ryan ryan 0 Oct 28 10:57 FileE # Add to staging area ryan ~/git_demo/demo_client (master) $ git add FileA FileB FileC FileD FileE # Submit to warehouse ryan ~/git_demo/demo_client (master) $ git commit -m '[demo_client]add FileA FileB FileC FileD FileE' [master 91a45d2] [demo_client]add FileA FileB FileC FileD FileE 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 FileA create mode 100644 FileB create mode 100644 FileC create mode 100644 FileD create mode 100644 FileE # View submission history ryan ~/git_demo/demo_client (master) $ git log commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (HEAD -> master) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA # Push to remote demo_bare warehouse ryan ~/git_demo/demo_client (master) $ git push origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 6 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To /home/ryan/git_demo/demo_bare 0b64004..91a45d2 master -> master
Then, we push these changes to the remote demo through the git push command_ Bare warehouse;
ryan ~/git_demo/demo_client (master) $ git push origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 6 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To /home/ryan/git_demo/demo_bare 0b64004..91a45d2 master -> master
Well, let's consider this question:
Suppose there are two development teams: A and B. their responsibilities are divided as follows:
- A is demo_ The maintainer of bare warehouse shall be responsible for updating all documents;
- B is demo_ Users of bare warehouse need to modify the demo according to the project requirements_ Modify the contents of the bare warehouse, but these modifications cannot be submitted to the master branch as public modifications;
At this point, you need to introduce the concept of branch:
The so-called branch actually refers to a path from a specified endpoint to the initial submission of the whole warehouse from top to bottom;
Is it a little familiar? Is it a little similar to the expression of git log?
Yes, GIT log shows a path. When git log is followed by the branch name (such as git log master), the submission history on this branch is displayed;
In order to fit the scenario assumed above, we re start from the demo_bare clones a warehouse to simulate the usage of team B:
# Clone to a demo_client_2 ryan ~/git_demo $ git clone ~/git_demo/demo_bare demo_client_2 Cloning into 'demo_client_2'... done. ryan ~/git_demo $ cd demo_client_2 # Confirm current progress ryan ~/git_demo/demo_client_2 (master) $ git log commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (HEAD -> master, origin/master, origin/HEAD) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA # Use git branch to view local branches ryan ~/git_demo/demo_client_2 (master) $ git branch * master
Use git branch without parameters to view the current local branch. Here we only have one master branch, and the front * indicates that the current branch is the master branch;
If we want to create a branch, the command we use is git branch, but we need to bring a branch name. For example, here I call it dev:
# Create a branch named dev, and the branch progress is consistent with the current HEAD ryan ~/git_demo/demo_client_2 (master) $ git branch dev # Looking at the local branch information again, it is found that there is a dev branch, but it is still on the master branch ryan ~/git_demo/demo_client_2 (master) $ git branch dev * master
It should be pointed out that there are many ways to create branches. Only one of them is mentioned here, which is also the most common way to create new branches locally;
If you don't want to create a new branch with the progress of HEAD, you can add the node you want to fork after git branch < branchname >, for example:
#The submission node can be abbreviated as 0b64004 ryan ~/git_demo/demo_client_2 (master) $ git branch dev 0b64004ae55297fee8ba6453f065a43a9135e5d7
Here, we want to create a branch with HEAD, so we don't take the last parameter;
Then, if you want to switch the branch to dev, you can use git checkout dev (GIT after version 2.23 can use git switch dev to achieve the same effect):
# Switch branch to dev ryan ~/git_demo/demo_client_2 (master) $ git checkout dev Switched to branch 'dev' # View local branch information ryan ~/git_demo/demo_client_2 (dev) $ git branch * dev master # View current branch commit history ryan ~/git_demo/demo_client_2 (dev) $ git log commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (HEAD -> dev, origin/master, origin/HEAD, master) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA
In this way, our dev branch is created locally.
Then modify some files according to our hypothetical scenario:
# Modify the contents of FileB ryan ~/git_demo/demo_client_2 (dev) $ echo bbb > FileB # View modifications ryan ~/git_demo/demo_client_2 (dev) $ git status On branch dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: FileB no changes added to commit (use "git add" and/or "git commit -a") # Compare the differences between the workspace and staging area of FileB ryan ~/git_demo/demo_client_2 (dev) $ git diff -- FileB diff --git a/FileB b/FileB index e69de29..f761ec1 100644 --- a/FileB +++ b/FileB @@ -0,0 +1 @@ +bbb # Add FileB to staging area ryan ~/git_demo/demo_client_2 (dev) $ git add FileB # Submit changes to staging area to warehouse ryan ~/git_demo/demo_client_2 (dev) $ git commit -m '[demo_client_2]update FileB' [dev 81355ba] [demo_client_2]update FileB 1 file changed, 1 insertion(+) # View the commit record of the current branch (i.e. dev branch) ryan ~/git_demo/demo_client_2 (dev) $ git log commit 81355ba55b9819d84a10d9d215150001f7224a4b (HEAD -> dev) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 13:05:27 2021 +0800 [demo_client_2]update FileB commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (origin/master, origin/HEAD, master) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA # View the submission record of the master branch ryan ~/git_demo/demo_client_2 (dev) $ git log master commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (origin/master, origin/HEAD, master) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA
It can be seen that the newly submitted "modification to FileB" is only recorded on the dev branch, but there is no submission information when viewing the master branch submission record with git log master. The previous figure here:
Again, the concepts of git log and branch are top-down. Therefore, although 91a45d2 has a unique and direct connection with 81355ba, 81355ba is above 91a45d2, and the vertex of the master branch is 91a45d2, so the master branch does not have the submitted information of 81355ba. On the contrary, the dev branch contains all the submitted information of the master branch at this time; (note, only at this time)
Why emphasize this? This is because the dev branch is created based on the master branch, that is, the HEAD 91a45d2 of the master branch at that time. Therefore, the dev branch contains at least all the submission information of the master branch from 91a45d2 to the first submission; Since there is no change in the HEAD of the master branch after the dev branch is created, the
During the period of "before the vertex of the master branch changes", the dev branch contains all submissions on the master branch;
Now, let's move the vertex (HEAD) of the master branch to deepen your understanding of the branch;
Before that, we will push the dev branch information to the remote demo_ Barego;
ryan ~/git_demo/demo_client_2 (dev) $ git push origin dev Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 6 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) To /home/ryan/git_demo/demo_bare * [new branch] dev -> dev
OK, now let's go back to the demo_client, A local warehouse, simulates the work of team A:
# Back to demo_ This directory of client simulates the work of team A as illustrated above ryan ~/git_demo/demo_client_2 (dev) $ cd ~/git_demo/demo_client # Using git pull from demo_bare updates all branch information ryan ~/git_demo/demo_client (master) $ git pull origin remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done. From /home/ryan/git_demo/demo_bare * [new branch] dev -> origin/dev Already up to date. # Then use git pull to ensure that the local branch of the master is synchronized to the latest state ryan ~/git_demo/demo_client (master) $ git pull origin master From /home/ryan/git_demo/demo_bare * branch master -> FETCH_HEAD Already up to date. # View the submission record of the current branch (i.e. master) ryan ~/git_demo/demo_client (master) $ git log commit 7a0a4ffe3713ed92346a43f56b2159b02ac7f24c (HEAD -> master) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 13:23:30 2021 +0800 [demo_client]update FileB commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (origin/master) Author: Ryan_ZHENG <*****> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <*****> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <*****> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA
When we are ready, we modify the files in the master branch:
# Team A also modified FileB, but the content was different ryan ~/git_demo/demo_client (master) $ echo "b2b2b2" > FileB # View modifications ryan ~/git_demo/demo_client (master) $ git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: FileB no changes added to commit (use "git add" and/or "git commit -a") # View the difference between the workspace and staging area of FileB ryan ~/git_demo/demo_client (master) $ git diff -- FileB diff --git a/FileB b/FileB index e69de29..5d8239e 100644 --- a/FileB +++ b/FileB @@ -0,0 +1 @@ +b2b2b2 # Add FileB to staging area ryan ~/git_demo/demo_client (master) $ git add FileB # Submit to warehouse ryan ~/git_demo/demo_client (master) $ git commit -m '[demo_client]update FileB' [master 7a0a4ff] [demo_client]update FileB 1 file changed, 1 insertion(+) # see ryan ~/git_demo/demo_client (master) $ git log commit 7a0a4ffe3713ed92346a43f56b2159b02ac7f24c (HEAD -> master) Author: Ryan_ZHENG <easter_walk@hotmail.com> Date: Thu Oct 28 13:23:30 2021 +0800 [demo_client]update FileB commit 91a45d2c680b096610e8cb785ffb16f3ec478de4 (origin/master) Author: Ryan_ZHENG <easter_walk@hotmail.com> Date: Thu Oct 28 10:57:51 2021 +0800 [demo_client]add FileA FileB FileC FileD FileE commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 Author: Ryan_ZHENG <easter_walk@hotmail.com> Date: Wed Oct 27 14:19:08 2021 +0800 [demo_client]delete FileA commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f Author: Ryan_ZHENG <easter_walk@hotmail.com> Date: Wed Oct 27 13:58:48 2021 +0800 [demo_client]add FileA
At this time, the vertex of the master branch has changed, so the dev branch at this time no longer contains all the submissions of the master branch, and the two officially bifurcate. The branch structure is shown in the figure below:
summary
This paper introduces the concept of branch and the creation of new branch;
Careful readers may have questions. Both the master and dev branches have modified FileB, so how to realize the hypothetical scenario of "notifying B of A's changes"? In the next section, we will introduce git merge;