git branch of git common commands

Hello everyone, I am 17.

Create a new git branch

Branching is the basis for parallel development. The essence of a branch name is a reference to the last commit of the branch. There are multiple branches, but there is only one HEAD, which can be considered as the "current branch" (the current branch). When you git switchswitch branches, HEAD re-points to the new branch.

Branching is git's killer app. The way Git handles branches is incredibly lightweight, and git encourages frequent branching and merging in your workflow, even many times a day. Branching involves many common git commands, which we will discuss together here.

The name of the branch is a reference to the last supported commit node. A branch is a collection of all nodes from the first node of the parent branch to the last node.

When you create a new warehouse, the master branch will be created by default.

 git init test 
 git init

Either way can be written. The difference is that git init testa new test folder will be created in the current directory, and git initgit will be initialized in the current directory.

As for the suggestion to use main instead of master, it is actually the name, which has no meaning at all. In git, it is just the name of the branch. It is the person who reads the name who reads the so-called meaning. If you care about the name, you can also modify it.

git init -b main   用 -b 指定初始化的分支为 main 

It can also be specified globally, so you don’t need to -bspecify it .

git config --global init.defaultBranch main

After the initialization is complete, there are three ways to create other branches.

git checkout -b dev
git switch -b dev
 
git branch dev

All three methods can create a new dev branch. The difference is that the first two create a new branch and switch to a new branch, and the last one only creates a new branch without jumping.

Since git checkout -b devand git switch -b devthe effect is exactly the same, why come up with two? It was used at the beginning git checkout -b, but later I felt that it was git checkout -bnot clear enough and checkoutwas given too many responsibilities, so the switch command was added.

local branch

List, create, or delete branches.

git branch         列出
git branch dev     创建
git branch -d dev  删除

git branch -vv 

输出:
feature   3446d05 [origin/feature] feat:开发登录功能
* master  ad9da22 [origin/master: ahead 3, behind 2] Merge branch dev

* means master is the current branch.

origin/master is the remote branch and master is the tracking branch of origin/master.

  • ahead 3: master is 3 commits ahead of origin/master
  • behind 2: master is 2 commits behind origin/master

You may be confused, how can you be ahead and behind at the same time? In fact, there is no contradiction. Commits that are not updated to the remote are ahead, and commits that are not synchronized to the local are behind.

delete remote branch

Delete the remote branch first

git branch -d origin feature

Then delete the locally tracked branch with the same name

git branch -d feature

Note: You must switch to other branches first. If you are currently in the feature branch, you cannot delete it

If the feature is not merged into master, you need to use -D to force delete

Note: It is recommended not to delete the remote branch. If the released branch is deleted, it may affect others.

Guess you like

Origin blog.csdn.net/m0_55635384/article/details/129020251