🎏 This is the 16th day of my participation in the Gengwen Challenge. For more details, see: Gengwen Challenge

0 x00 📢 preface

👇 The Git Getting Started article is linked below, and read the article in order 👇.

Git from giving up to getting started

This series of articles explains how to quickly master the basic Git usage through examples and practices.

This article is the fifth one, which introduces the basic concepts of Branch through the combination of text and text, laying a good foundation for the subsequent in-depth analysis.

Git’s branch model is one of its “must-kill features.” It’s powerful and unique, and it’s incredibly lightweight, creating branches almost instantly, and switching between branches is just as easy. To use it well, you need to understand this feature.

0x01 Introduction to Branch

Git branches are essentially mutable Pointers to commit objects. Git’s default branch name is Master, and the master branch automatically moves forward with each commit to point to the last commit.

The Master branch of Git is not a special branch. Every repository has a master branch because the git init command creates it by default. It’s no different from any other branch.

Git stores data not as changes or differences in files, but as a series of snapshots at different times. Git holds a commit object when you commit. The submission object also contains the author’s name and mailbox, information entered at the time of submission, and a pointer to its parent object.

Pointer to the HEAD

Tell Git which branch it is on. It is a pointer to the current local branch, or an alias for the current branch.

0x02 Workflow diagram

Git init creates repositories

Run git init to create a Git repository. Since the branch is a pointer to a commit, the repository does not have any commits at this time, so the master branch has not been created, and the HEAD reference refers to the uncreated master branch. Branches cannot be used before content is committed.

The warehouse status is as follows: 👇.

Git commit Indicates the first commit

When you run Git Commit, it takes the contents of the staging area and saves it as a permanent snapshot, then creates a commit object that points to that snapshot, and finally updates the master to point to the commit. At this point, the master branch is created. The commit object generated by the first commit has no parent object.

Git commit Common commit

When you run Git Commit, it takes the staging area, saves it as a permanent snapshot, sets the current node as the parent, and points the current branch to the new commit node. In the figure below, the current branch is master. Before running the command, the master points to 11c6b2a. After submitting, the master points to the new node 7632a63 with 11c6b2a as the parent.

A commit object produced by a normal commit operation has a parent object (containing a pointer to the parent object), while a commit object produced by merging multiple branches has multiple parents.

Use the git log command to view the shorthand hash of each parent commit, using the %p option.

$ git log --pretty=format:'%h --> %p| %ad | %s%d [%an]' --graph --date=short
Copy the code

You can see that the first commit has no parent, and all other commit records can see that the parent hash is the same as the last commit hash.

Git Branch Creates a branch

Only a new branch is created, it does not automatically switch to the new branch.

Create dev branch:

$ git branch dev
Copy the code

Looking at the commit record, you can see that the dev branch points to the latest commit record.

If you run Git Branch with no arguments, you get a list of all branches currently in place. * before the master branch: it represents the branch that is being checked out (which is the branch to which the current HEAD pointer points).

Git Checkout branch switch

Switch to an existing branch using the Git checkout command.

$ git checkout dev
$ git hist
Copy the code

You can see that to switch to the dev branch, the HEAD pointer points to the dev branch 👇.

Dev branch commit

Modify files in dev branch and submit them to the repository. Check the commit record. HEAD branch automatically moves forward with the commit operation.

As you can see, the dev branch has moved forward, but the master branch has not. It still points to the same object as when you run Git Checkout.

Switch to the Master branch

Switch to the master branch using the git checkout command.

$ git checkout master
$ git hist --all  
Copy the code

You can see that the HEAD is pointed back to the master branch, and the working directory is restored to the snapshot that the master branch points to.

Project Bifurcation History

If you make changes now, the project will start with an older version. This will ignore the changes made by Dev to allow development to proceed in the other direction.

The commit history of this project has been forked after modifying file staging commits under the master branch.

Use git log — pretty = the format: ‘% h – > p AD | | % % s % d % % [an]’ – graph – date = short – all command to see submit project history, pointing to the branches, and branches of split ends.

0 x03 📚 reference

“Branch “,ebook” Reset Reveal “,ebook “Git Checkout “,ebook

0x04 Watch the column

Now that the basic concept of branching has been introduced, please keep in mind that the following commands will flow based on the concept of branching. More complex branch management (workflow) will be covered in a future article.

This article has been included in the column 👇, you can directly follow.

Read on for more articles | the series will continue to be updated