Table of contents
2. Common operations of branches
3. Common operations of labels
Preface
In the software development process, version management is a very important part. As one of the most popular distributed version control systems currently, Git provides a wealth of functions to support project version management. Among them, branch (Branch) and tag (Tag) are two concepts commonly used in Git. They play a crucial role in organizing and managing different versions of the code. This blog will delve into the concepts, usage, and best practices of Git branches and Git tags.
1. Git branch (Branch)
1. The concept of branch
In Git, a branch refers to a mutable pointer to a commit object. It allows developers to independently develop new features and fix problems without affecting the mainline code. Each branch contains a complete copy of the project files, which means developers can work on different branches simultaneously without affecting each other.
2. Common operations of branches
- Create a branch:
git branch <branch_name>
- Switch branches:
git checkout <branch_name>
orgit switch <branch_name>
- Merge branches:
git merge <branch_name>
- Delete branch:
git branch -d <branch_name>
3.Git branch management
list branches
Basic command to list branches:
git branch
Without parameters, git branch will list your local branches.
$ git branch * master
What this example means is that we have a branch called master , and this branch is the current branch.
When you execute git init , Git will create the master branch for you by default .
Create and switch branches: You can create a new branch. This branch is created from the state of the current working directory and contains all files and folders in the current working directory.
If we want to create a branch manually. Just execute git branch (branchname).
$ git branch testing $ git branch * master testing
Now we can see that there is a new branch testing .
When you create a new branch after the last committed update in this way, if you later commit an update and then switch to the test branch, Git will restore your working directory to what it was when you created the branch.
Next we will demonstrate how to switch branches. We use git checkout (branch) to switch to the branch we want to modify.
$ ls README $ echo 'ctb.com' > test.txt $ git add . $ git commit -m 'add test.txt' [master 3e92c19] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt $ ls README test.txt $ git checkout testing Switched to branch 'testing' $ ls README
When we switched to the testing branch, the new file test.txt we added was removed. When switching back to the master branch, they reappeared.
$ git checkout master Switched to branch 'master' $ ls README test.txt
We can also use the git checkout -b (branchname) command to create a new branch and immediately switch to the branch to operate in the branch.
$ git checkout -b newtest Switched to a new branch 'newtest' $ git rm test.txt rm 'test.txt' $ ls README $ touch ctb.java $ git add . $ git commit -am 'removed test.txt、add ctb.java' [newtest c1501a2] removed test.txt、add ctb.java 2 files changed, 1 deletion(-) create mode 100644 ctb.java delete mode 100644 test.txt $ ls README ctb.java $ git checkout master Switched to branch 'master' $ ls README test.txt
As you can see, we created a branch, removed some files test.txt on that branch, and added the ctb.java file, then switched back to our main branch, and the deleted test.txt file came back, And the newly added ctb.java does not exist in the main branch.
Using branches to separate work allows us to do things in different development environments and switch back and forth.
delete branch
Delete branch command:
git branch -d (branchname)
For example, we want to delete the testing branch:
$ git branch * master testing $ git branch -d testing Deleted branch testing (was 85fc7e7). $ git branch * master
Branch merge
Once a branch has independent content, you'll eventually want to merge it back into your master branch. You can merge any branch into the current branch using the following command:
git merge
$ git branch * master newtest $ ls README test.txt $ git merge newtest Updating 3e92c19..c1501a2 Fast-forward ctb.java | 0 test.txt | 1 - 2 files changed, 1 deletion(-) create mode 100644 ctb.java delete mode 100644 test.txt $ ls README ctb.java
In the above example, we merged the newtest branch into the main branch, and the test.txt file was deleted.
After merging, you can delete the branch:
$ git branch -d newtest Deleted branch newtest (was c1501a2).
After deletion, only the master branch remains:
$ git branch * master
merge conflicts
Merging is not just a simple operation of adding and removing files, Git will also merge modifications.
$ git branch * master $ cat ctb.java
First, we create a branch called change_site, switch to it, and change the content of ctb.java to:
pubic class ctb(){ }
Create the change_site branch:
$ git checkout -b change_site Switched to a new branch 'change_site' $ vim ctb.java $ head -3 ctb.java pubic class ctb(){ } $ git commit -am 'changed the ctb.java' [change_site 7774248] changed the ctb.java 1 file changed, 3 insertions(+)
Submit the modified content to the change_site branch. Now, if we switch back to the master branch, we can see that the content is restored to what it was before we modified it (empty file, no code), and we modify the ctb.java file again.
$ git checkout master Switched to branch 'master' $ cat ctb.java $ vim ctb.java # Modify the content as follows $ cat ctb.java <?php echo 1; ?> $ git diff diff --git a/ctb.java b/ctb.java index e69de29..ac60739 100644 --- a/ctb.java +++ b/ctb.java @@ -0,0 +1,3 @@ +<?php +echo 1; +?> $ git commit -am 'Modify code' [master c68142b] Modify code 1 file changed, 3 insertions(+)
These changes are now recorded in my "master" branch. Next we merge the "change_site" branch.
$ git merge change_site Auto-merging ctb.java CONFLICT (content): Merge conflict in ctb.java Automatic merge failed; fix conflicts and then commit the result. $ cat ctb.java # Open the file and see the conflicting content <?php <<<<<<< HEAD echo 1; ======= echo 'ctb'; >>>>>>> change_site ?>
We merged the previous branch into the master branch, and a merge conflict occurred. Next, we need to modify it manually.
$ vim ctb.java $ cat ctb.java <?php echo 1; echo 'ctb'; ?> $ git diff diff --cc ctb.java index ac60739,b63d7d7..0000000 --- a/ctb.java +++ b/ctb.java @@@ -1,3 -1,3 +1,4 @@@ <?php +echo 1; + echo 'ctb'; ?>
In Git, we can use git add to tell Git that the file conflict has been resolved
$ git status -s UU ctb.java $ git add ctb.java $ git status -s M ctb.java $ git commit [master 88afe0e] Merge branch 'change_site'
Now we have successfully resolved the conflicts in the merge and committed the results.
2. Git tag (Tag)
1. The concept of label
In Git, a tag is a pointer to a specific commit object, usually used to identify important events such as version releases and milestones. Tags can help developers and users quickly locate and trace back different versions, and can also be used as markers for important versions in the project.
2. Type of label
- Lightweight tag: It simply points to a commit object, similar to a branch, and has no other metadata.
- Annotated tag: In addition to pointing to a submission object, it also contains the tag's creator, creation date, comments and other information.
3. Common operations of labels
- Create lightweight tags:
git tag <tag_name>
- Create annotation tags:
git tag -a <tag_name> -m "tag message"
- View the tag list:
git tag
- Delete tag:
git tag -d <tag_name>
4.Git tag management
There are two types of tags used by Git: lightweight and annotated .
A lightweight tag is like a branch that does not change. In fact, it is a reference to a specific commit object.
The annotated tag is actually an independent object stored in the warehouse. It has its own checksum information, including the tag's name, email address and date, as well as the tag description. The tag itself also allows the use of GNU Privacy Guard (GPG) to sign or verify.
Generally, we recommend using labels with notes to retain relevant information;
Of course, if you only need to add labels temporarily, or if you don’t need additional information, you can use lightweight labels.
Create tags
[root@Git git]# git tag v1.0
View existing tags
[root@Git git]# git tag v1.0 [root@Git git]# git tag v1.1 [root@Git git]# git tag v1.0 v1.1
Delete tag
[root@Git git]# git tag -d v1.1 Deleted tag ‘v1.1’ (was 91388f0) [root@Git git]# git tag v1.0
View what has been modified in this version
[root@Git git]# git show v1.0 commit 91388f0883903ac9014e006611944f6688170ef4 Author: "syaving" <"[email protected]"> Date: Fri Dec 16 02:32:05 2016 +0800 commit dir diff –git a/readme b/readme index 7a3d711..bfecb47 100644 — a/readme +++ b/readme @@ -1,2 +1,3 @@ text hello git +use commit [root@Git git]# git log –oneline 91388f0 commit dir e435fe8 add readme 2525062 add readme
Push tags to remote repository:
Use the above command to push the local tag to origin
the remote repository named.
- Single label push:
git push origin <tag_name>
- All tag push:
git push origin --tags
Pull the tags of the remote warehouse:
- Pull all tags: git fetch origin <tag_name>
- git pull origin <tag_name>