Introduction to git — from operation to theory (I)



This article aims to help students who have not been exposed to git use the basic functions of GIT and GitHub, which is suitable for beginners.

Since Git is a tool, a reasonable learning process should first know how to use it, and then understand the concepts of warehouse, branch, conflict and so on. Therefore, this paper puts various operations according toNovice learning curveIt is easy for beginners to arrange step by step.

Because there are many contents and can’t be finished at one time, it is made into a serial, and other contents will be updated in the future.

Recommended reading:
Rookie tutorial…
Liao Xuefeng’s git tutorial…
Branch learning artifact (highly recommended) https://learngitbranching.js….
If you learn the above tutorial well, there is no need to read this article. Ha ha ha



go to:
Download the Windows version, no brain next step. Installation complete

How do I start git?

Right click – git bash here anywhere tocurrent directoryOpen GIT.

Introduction to git -- from operation to theory (I)

Note: right click where it will open in which directory. When the C disk is opened, the terminal will display the C disk.
Introduction to git -- from operation to theory (I)

Linux & Mac

Both Linux and Mac have built-in git, so you can directly open the terminal.

Note: in addition to manually entering the path, you can also press CD and drag the folder directly into the terminal to display the path directly.

Introduction to git -- from operation to theory (I)

What’s git for?

The core function of Git isversion control , I don’t know what it isversion control It doesn’t matter. Here are some usage scenarios:

  • For example, you are writing a project. When a certain stage is completed, you only need one key to save the current code,No matter how you change it later, it will not destroy the current code(similar)snapshotFunction)
  • When working with others, you can just clickMerge your own code with other code, you no longer need to copy and paste manually
  • If you’re going to try several different solutions, usebranchFunction, just one key, you can put your code inFree switching between different schemes, different schemes are developed separately without mutual influence.
  • If there is a major error in the code, just press one button and take the time machine directly,Return to the previous normal version, mom doesn’t have to worry about me breaking the software anymore

To sum up, we can make good use of GITReduce the risk of code destruction and improve development efficiency

What is a warehouse? What is GitHub?

In a word, an ordinary folder, if managed with git, is aCode warehouse

The warehouse is a whole. Due to git’s hosting, the code in the warehouse can be recorded by git if there is any change.

With this code tracking, it is very convenient to save, restore and submit the code.

The warehouse is divided intoLocal warehouseandRemote warehouse

The local warehouse is the warehouse on the current computer. Remote warehouse is a warehouse on the server that can transfer data between local and remote warehouses during team cooperation.

GitHub is one of the remote warehouses. The warehouses on GitHub can be shared with other people or teams.

In addition, there are other remote warehouse software, and you can even build a warehouse server yourself.

Start: improve language learning efficiency with Git

Many excellent tutorials are on GitHubSample code is provided, and in accordance withTutorial chaptersDivided (a tutorial corresponds to a section of code).

With git, you can quickly switch to ensure that the current code corresponds to the chapters of the tutorial.

Open a set of tutorials containing sample code, which will have the address of the code warehouse:
For example:

Open the GitHub project, and all the files are in it.
Introduction to git -- from operation to theory (I)

How to download it locally?

Git clone copies the remote warehouse code locally

First command:git clone

It should be noted that the command isWhich directoryAfter execution, the code will where the clone will go, so you need to determine before cloneFolder location

//Method of use

//Download the code of the remote warehouse to the local current folder
Git clone < warehouse address > 
git clone

//Download the code of the remote warehouse to a specific folder
Git clone < warehouse address > <本地文件夹的地址>
git clone PHP

The following prompt appears to indicate that clone is complete:
Introduction to git -- from operation to theory (I)
All sample files can be found in the folder:
Introduction to git -- from operation to theory (I)
Then you can follow the tutorial.

Git checkout toggles “savepoint”

What if you learn the second section and your code is still in the first section, but you are lazy to follow the tutorial again?

Because the author of the tutorial has set up a lot for the code“Save point”, we can switch these “savepoints” arbitrarily by using git checkout command.

useCD < folder name >The command enters the code folder and you can see that thegit:(master)word,
Introduction to git -- from operation to theory (I)

Enter at this timegit tag, all savepoints set by the author are displayed:
Introduction to git -- from operation to theory (I)

After viewingPress Q to exit

The next step is switching. For example, I learned chapter 2.4, using

//Switch to a savepoint
git checkout step2.4

//Discard the current code and force a switch to a savepoint
git checkout -f step2.4

The following figure appears:
Introduction to git -- from operation to theory (I)

This shows that as like as two peas, the local code is exactly the same as the tutorial.

Collaborative development using Git

Because this part involvesbranchIt is difficult for beginners to deeply understand the concept of, so provide operation first. As long as the operation is carried out in strict accordance with this article, it can be carried out smoothly.

Brief branch

Branches, literally, can divide code into severalNo mutual influenceAs like as two peas, there are three identical branches: Master bifurcation, A bifurcation and B bifurcation.

Introduction to git -- from operation to theory (I)

Their code is 123.

If I change the code to 456 on branch A and 789 on branch B, the code of the master branch will not be affected, but 123.

Introduction to git -- from operation to theory (I)

Master as projectMain branch, an excellent team is not allowed to submit code directly to the master branch.

The standard practice is toOwn branchAfter the code is written on the, a request for merging the code is submitted to the master branch, which is reviewed by the person in charge. After passing the review, the code will be merged into the main branch.

Introduction to git -- from operation to theory (I)

This is the basic process of team development.


Since it is team development, you need to understand the issue.

Introduction to git -- from operation to theory (I)

Issue means “event”, that is, to tell other members,“What am I doing and what do I want to achieve?”, when others see your issue, they know you are doing it,Avoid two people writing duplicate code

An issue corresponds to a branch:
Introduction to git -- from operation to theory (I)

For example, if the number of this issue is 4, we will create 4 branches locally:

Git checkout – b create new branch

If, of course, on the master branch, enter:

//Create a branch named 4
git checkout -b 4

Successfully created 4 branches:
Introduction to git -- from operation to theory (I)

After that, all changes are made on the 4 branch, which has nothing to do with the main branch.

Git push push changes

When your code is finished, go back to the terminal and enter:

//Let git record all files in the current directory
git add .

//Submit the changes to the local warehouse, and write the remarks casually, such as "what am I doing?"
Git commit - M < remarks >

//Push local changes to the 4 branches of GitHub
git push origin 4

These three commands can push your code to GitHub, which is still the last step


The final step is to initiate a merge request.

Create a new pullrequest on the GitHub website.

Complete at compareThe branch you just uploaded
Fill in at baseMerged branches

Introduction to git -- from operation to theory (I)

The above figure means to merge your 2 branches into the main branch. Don’t write it backwards!

After clicking submit, the rest will be submitted to the person in charge for review.


It’s no use talking too much. Try it yourself and you’ll understand everything.

Say a few more words

I remember when we started our first hand training project last year, we encountered problems with GIT.
Because git involvesTeamwork, ifIf anyone does not master it well, it will affect the code security of the cooperation project(such as mistakenly deleting code, submitting wrong code, etc.).
For this reason, I asked brother Shuo for the basic usage of GitHub in advance, and then told it to other members of the group after I learned it, so as to ensure that each code merge is carried out smoothly and safely.

The following content will be updated as soon as possible.

Author: Liu Yuxuan, mengyunzhi development team, Hebei University of Technology