Learn Git and Github, start here!

1, Introduction

Git is a free and open source distributed version control system. Due to its small footprint and fast performance, Git is superior to other SCM tools (SVN, CVS, etc.) and is mainly used for code version management.

GitHub is a Git based free version control repository (a concentration of open source projects). Anyone can upload or download open source projects on the platform, which is used by 65 million developers around the world.

2, Git
1. Git overall structure
2. Version object

Git saves the version content of each submission through the commit object, where the tree object points to the snapshot of all code files. Multiple versions are linked through a parent object.

3. File status

Git managed files have four states:

  • Untracked: not added to Git warehouse for version management;
  • Unmodified: not modified after submitting Git warehouse;
  • Modified: modified after submitting Git warehouse;
  • Staged: local staging area, waiting to be submitted to Git warehouse.
4. Installation
  • MacOS uses the package manager Homebrew to install git. more...
# 1. Under the terminal, execute the following command to install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 2. Install git
brew install git

# 3. After installation, first set the global name and mailbox (modify the mailbox and user name below)
git config --global user.name ×××
git config --global user.email ×××@×××
  • Linux/Unix: install git with the command on Debian/Ubuntu and Fedora. more...
# Debian/Ubuntu
apt-get install git

# up to Fedora 21
yum install git
# Fedora 22 and later
dnf install git

# After installation, first set the global name and mailbox (modify the mailbox and user name below)
git config --global user.name ×××
git config --global user.email ×××@×××
  • Windows: install git with exe installation package. download
5. Configuration description
  • Priority: the configuration below will overwrite the configuration above. more...,git config

Path file



It contains the general configuration of each user on the system and their warehouse. When git config is executed, the -- system option is brought to read and write the file. Administrator or super user permissions are required.

~/.gitconfig ~/.config/git/config

Only for current system users. Use the -- global option to read and write this file, and modify the configuration to affect all warehouses of the current system user.


Git profile of the current warehouse. Use the -- local option to read and write this file (this option is not required in the warehouse directory).

  • View configured commands
# When viewing all git configurations, there may be duplicates, because the configuration information displayed later will overwrite the previous configuration information from different configuration files (/ etc/gitconfig and ~ /. gitconfig)
$ git config --list

# View all git configurations and corresponding configuration files
$ git config --list --show-origin

# View individual configuration information
git config user.name

# Query all available configuration information
git help config

Some files do not need git for version management, such as compiled cache files gitignore ignores it.

glob mode

Simplified version of regular matching


Match zero or more arbitrary characters;


Match only one arbitrary character;


Match any character listed in square brackets;


Indicates that all numbers from 0 to 9 are matched;


It means to match any intermediate directory. For example, a/**/z can match a/z, a/b/z or a/b/c/z.

Serial number



Blank lines or # beginning lines are ignored;


Matching patterns can start with (/) to prevent recursion;


The matching pattern can specify the directory at the end of (/);


To ignore files or directories outside the specified mode, add an exclamation mark (!) before the mode Inversion;

# Ignore all a documents

# But track all lib a. Even if you ignore it in front A documents

# Only TODO files in the current directory are ignored, not subdir/TODO

# Ignore the folder named build in any directory

# Ignore Doc / notes Txt, but do not ignore Doc / server / arch txt

# Ignore the doc / directory and all its subdirectories pdf file
7. origin and master

The remote warehouse name origin, like the branch name master, has no special meaning in Git

  • master: is the default starting branch name when running git init;
  • origin: the default remote warehouse name when running git clone. If running git clone -o booyah, the remote branch name will be booyah/master.

HEAD is a symbolic reference to the current branch.

$ cat .git/HEAD
ref: refs/heads/master
  • HEAD~{n}: it means to backtrack n versions with HEAD as the starting point;
  • HEAD^n: indicates the submitted version of the previous version (the current version may be obtained by merging multiple versions);

The following version submission records (submission order is from top to bottom and from left to right). The previous versions of A are B and C. more...

G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
A =      = A^0
B = A^   = A^1     = A~1
C =      = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2
9. Common operation

How to use Git commands for file version management? Details, look here!

3, GitHub
1. Download code mode

GitHub has three ways to download code:

  • HTTPS: use the user name and password to log in to github to the clone project.
  • SSH: upload the SSH key to the background of GitHub so that you can clone the project without a password.
  • GitHub CLI : the platform provides terminal tools to manage the warehouse, and you can also use some additional functions provided by Github.
2. Secret free download code

Configure ssh key to download Github code without secret. It is convenient and safe. You need to use it ssh-keygen Tools.

# 1. Open Terminal and view the existing key (. File name with pub suffix)
$ ls -al ~/.ssh

# 2. If not, create one
$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

# 3. Copy the output to the clipboard (the following two apply to mac systems)
$ cat ~/.ssh/id_rsa.pub
$ pbcopy < ~/.ssh/id_rsa.pub
$ cat ~/.ssh/id_rsa.pub | pbcopy

# 4. Log in to the github official website and create a new SSH key according to this path
Settings --> SSH and GPG keys --> New SSH key
1,Fill in the name (feel free to fill in it, so that you can understand where it is next time you see it) key);
2,Paste clipboard contents to key In the input box of;

# 5. Terminal checks whether the connection is successful
$ ssh -T git@github.com

# 6. Github randomly finds a project and tests the download to see if it succeeds
$ git clone git@github.com:shipwright-io/build.git
3. Desktop tools

If you think it is too troublesome to input commands on the terminal, you can use the desktop application tools provided by Github GitHub Desktop .

4,Pull Requset

Process of contributing code to open source projects:

# 1. Log in to Github, find the project, and click the Fork button to get a copy of the project
# 2. Download project copy to local
git clonet git@github.com:***/test.git

# 3. New branch
cd test
git checkout -b branch1

# 4. Modify code
# 5. Submit code to local warehouse
git commit -a -m 'Modify code'
# 6. Push local warehouse to remote warehouse (project copy)
git push origin branch1
# 7. Log in to Github, prompt the project for a new branch, create a pull request and send it to the source project
# 8. The source project author can see the pull request and decide whether to close or merge
5. Pull requset (conflict)

If the source project has many more submitted versions than the replica, the branch submitted on the replica will not be able to submit the pull request. This is to solve the problem:

# 1. Download the latest source code
git remote add upstream git@github.com:***/orgin.git
git fetch upstream
# 2. Merge the latest code into your own branch
git merge upstream/master

# 3. It is possible to generate conflicts, repair conflicts and make functions intact

# 4. Push branch code to remote warehouse (project copy)
git push origin branch1

# 5. Log in to Github and send a pull request to the source project
6. Fork project

The fork project will not be updated automatically with the source project and needs to be updated manually

# 1. Switch to the branch master of the fork project
git checkout master
# 2. Pull the source project code and merge it into the current branch
git pull https://github.com/progit/progit2.git
# 3. Push updated code to fork project
$ git push origin master (3)
# Each update above requires the input of the source project address, which is too troublesome. It is simplified as follows
# 1. Add source project address to local
git remote add progit https://github.com/progit/progit2.git
# 2. Set the address of the pull code of the master branch as the source project address
git branch --set-upstream-to=progit/master master
# 3. Set the default push warehouse to origin
git config --local remote.pushDefault origin

# 4. Then update the code mode
git checkout master
git pull
git push
4, VS Code

It is recommended to use VS Code to edit the code. It supports all programming languages. You only need to install plug-ins. Git is also built-in. You only need to click the mouse to complete git operation without entering complex git commands.

5, Reference documents

Keywords: git github

Added by djcritch on Sat, 15 Jan 2022 04:46:25 +0200