“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

Author: Tangyuan

Personal blog: Javalover.cc

preface

Branch management refers to the creation of multiple branches during development, and the development management in different branches does not affect each other.

Wait until the branch development is complete, then merge into the main branch;

If there is no branching, submitting code before it is written will result in incomplete code being handed to others. But if you don’t commit, you run the risk of losing your code;

So the obvious benefit of branching is that the code is more secure, and the code of each developer does not interfere with each other.

directory

  1. Create a branch
  2. Switch branch
  3. Submit branch
  4. Merging branches
  5. Delete the branch
  6. A shortcut

The body of the

1. Create a branch

Git branch

To view branches: git branch; Master is preceded by an asterisk (*) to indicate that it is currently in the Master branch

PS D:\branch-demo> git branch dev
PS D:\branch-demo> git branch
  dev
* master
Copy the code

As you can see, the current Head points to the master branch, and dev and master point to the same node

2. Switch branches

Next, we switch to the dev branch

Git switch dev

PS D:\branch-demo> git switch dev
Switched to branch 'dev'
Copy the code

After switching branches, the diagram looks like this: You can see that the current Head points to the dev branch

3. Commit the branch

Next, we can make changes on dev and then commit them to the dev branch

The steps are as follows: Let’s create a demo.txt file and submit it

PS D:\branch-demo> git add .\demo.txt
PS D:\branch-demo> git commit -m "demo.txt"
[dev bd68189] demo.txt
 1 file changed, 1 insertion(+)
 create mode 100644 demo.txt
Copy the code

You can see that the latest commit is on dev, and the master is pointing to the old code

PS D:\branch-demo> git log
commit bd681899740d30ee0c793843e4b175d596b3430b (HEAD -> dev)
Author: jalon2015 <[email protected]>
Date:   Wed Nov 10 18:02:41 2021 +0800

    demo.txt

commit 2c053a9d5df9fcb08c29a918f7992d5b763392fc (master)
Author: jalon2015 <[email protected]>
Date:   Wed Nov 10 17:45:41 2021 +0800

    first commit
Copy the code

After submission, the diagram looks like this: Dev has moved forward a step, but master is still in the same place

At this point, we can also see a similar schematic diagram in the Git window of IDEA, as follows:

4. Merge branches

Once committed, we can merge dev into the master branch. Before merging, we need to switch to the master branch and then merge dev

Git switch master

Git merge dev; Merge dev into master branch

PS D:\branch-demo> git checkout master
Switched to branch 'master'PS D:\branch-demo> git merge dev Updating 2c053a9.. bd68189 Fast-forward demo.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 demo.txtCopy the code

Git log: Head = master dev

PS D:\branch-demo> git log
commit bd681899740d30ee0c793843e4b175d596b3430b (HEAD -> master, dev)
Author: jalon2015 <[email protected]>
Date:   Wed Nov 10 18:02:41 2021 +0800

    demo.txt

commit 2c053a9d5df9fcb08c29a918f7992d5b763392fc
Author: jalon2015 <[email protected]>
Date:   Wed Nov 10 17:45:41 2021 +0800

    first commit
Copy the code

A schematic diagram of the merged branches is shown below:

Merge branches. If there are conflicts, resolve them first and then merge them. How to resolve branch merge conflicts

5. Rename the branch

Git branch -m

PS D:\branch-demo> git branch -m dev newDev
PS D:\branch-demo> git branch
* master
  newDev
Copy the code

This command may not be used often, but it is worth knowing

5. Delete the branch

Once the merge is complete, we can delete the branches;

But before deleting a branch, we need to switch to another branch; otherwise, we cannot delete it.

Git switch Master

Git branch -d dev

PS D:\branch-demo> git switch master
Switched to branch 'master'
PS D:\branch-demo> git branch -d dev
Deleted branch dev (was bd68189).
Copy the code

After deletion, the diagram looks like this: The dev branch is gone

shortcuts

Above we create branch and switch branches are separate operation, in fact, can be in one step;

Git switch -c dev

PS D:\branch-demo> git switch -c dev
Switched to a new branch 'dev'
Copy the code

conclusion

  • Git branch

  • Git switch

  • Check branches: git branch; In the branch list, the one preceded by * is the current branch

  • Git merge

    ; Merges the specified branch into the current branch

  • Git branch -d

  • Git switch -c