Git tutorial sharing

brief introduction

Git is a free and open source distributed version control system. The father of Git is Linus, the father of linux. After linux was open-source, there were many participants and contributors, resulting in excessive workload of reviewing, merging and distributing code, so it took another week to develop GIT.

Basic concept analysis

Workspace: refers to the working directory where you store code files on the current device Local warehouse: refers to the local storage area used to store different versions of codes in the current equipment, generally in the working directory git directory Remote warehouse: refers to the remote network storage area used to store codes of different versions, which is generally provided by the code hosting platform. Mainstream platforms include (github|gitlab | privatization | gitlab | privatization | gitea | Alibaba cloud | Tencent cloud), etc Branch: refers to another development history line separated from one development history line commit: refers to the record of code changes in a certain period of time. The snapshot constitutes the code development history Version number: use SHA-1 to generate a 40 bit hexadecimal unique string to identify a code snapshot. (Note: GIT version number is out of order, and there is no global version number)


What can a branch do? Branch can well isolate our operation from the development main line. In order to prevent our modification from affecting the main line program, only the approved code can be incorporated into the main line. Therefore, using branch is the most ideal method.

For example: we are in an iterative cycle It is planned to upgrade the functions of user management and commodity management within (usually two weeks). Nearly 100 code submission records have been generated during the development and testing process. We are ready to go online at the last minute. We give up the commodity management upgrade for some reasons and only retain the user management. At this time, we need to eliminate the code of commodity management. If we pass the reasonable planning branch at the beginning, we just need to Merge user management branches, on the contrary, without reasonable branch management is a nightmare.

Why should programmers learn GIT?

  1. Git is distributed (can work offline, can cooperate with multiple remote warehouses, nested projects), and SVN is centralized (must be networked, single point warehouse)
  2. Git branch is cheap, fast and flexible, while SVN branch is expensive, slow and heavy
  3. Common or private code hosting platforms are mature and easy to use, with rich ecosystems and strong development momentum; Code review is intuitive, convenient and efficient
  4. Mastering GIT can develop in parallel and in collaboration, and easily cope with projects of various sizes and complex requirements
  5. It is easier to integrate into the open source community, participate in or launch open source projects
  6. The rich command line can accurately control the code and make it easier to connect with various development tools

What are the disadvantages of GIT? GIT cannot make incremental submission to binary files, resulting in too many file copies and increasing the warehouse volume. The solution is to use LFS GIT project management is independent and flat. Permissions can only be managed by project rather than directory. Unlike SVN, each directory can be managed as a project Git has high learning costs (mainly powerful functions)

Mainstream branch management methods

There are many branch management methods in the industry, including the famous GitFlow, TBD and the versions derived from them. GitFlow design is very comprehensive. Considering various problems in the actual development process, it can deal with them stably. It is very suitable for code management of large projects. His main ideas include two main branches - Master and development, one Release branch - Release, several development branches - Feature, and HotFix branch. The overall implementation process is slightly complex and not very friendly to continuous integration. TBD (Trunk Based Development) has been popular as early as the svn era. It is a relatively simple branch management model. It has only one trunk branch. After everyone has written the code and passed the self-test, they Push it onto the trunk. When they want to Release, they pull a Release branch, which is friendly to the sequel. However, if problems are found before going online, it is difficult to remove the code.

GIT branching principle

The difference between git branch and SVN branch is that SVN branch is a directory and a copy of code. The new branch has no past history. The GIT branch is a pointer to the commit object, and the difference is recorded in the version library through metadata. Git has a special pointer called HEAD. It is a pointer to the local branch you are working on. Switching branches is essentially moving the HEAD pointer. Git encourages frequent use of branches.

Graphic GIT structure

Drawing tools:

Note: git fetch pulls updates from the remote warehouse to the local warehouse without affecting the work area. git pull pulls the code from the remote warehouse to the local warehouse and merges it into the workspace.

GIT installation

Download from the official website (the network speed is very slow): It can be downloaded through Tencent software library in China!keyword=git Installation method: double click the installation package and click next, which is omitted here.

Auxiliary tool recommendation

Meld is a powerful file comparison tool. It can be used as a git plug-in to check differences and merge code.

meld installation configuration

Download and install from the official website, then open git base and execute the following command to configure your comparison tool

git config --global merge.tool meldgit config --global mergetool.meld.path "/c/Program Files (x86)/Meld/Meld.exe" # Change the software path here to your own installation path

In case of code conflict, execute git mergetool to start three-way merging

VSCODE is a code editing tool developed by Microsoft. The built-in GIT management function is very easy to use, especially the intuitive and efficient conflict resolution TortoiseGit is a GIT GUI tool. Its operation and interface are very similar to TortoiseSVN (I have never seen it myself).

New local code base

# Method 1: initialize a directory for the current directory Git Code base git init# Method 2: create a new directory and initialize it as Git Code base git init [project-name]# Method 3: clone a project git clone [url]cd xxx # Enter directory # Clone a bare version library and push it to the server in the form of image git clone --bare [url] && cd xxxgit push –mirror [url]# View current status git status

Note: the user name and password need to be entered in HTTP mode. If the input is wrong, you can delete git config - system - unset credential helper

to configure

# Show current Git to configure git config --list# edit Git configuration file git config -e --global# Set the user name and mailbox when submitting code git config -- global user name "[name]"git config --global user. email "[email address]"

see information

gitk # git bash Only then git loggit status# View the current detailed branch information (you can see the current branch and the corresponding remote tracking branch):git branch -vv# View current remote warehouse information git remote -vvgit remote -a

Add / delete files

# Add all files in the current directory to the staging area git add .# Delete the workspace file and put the deletion into the staging area git rm [file1] [file2]

Code submission

# Submit staging area to warehouse area git commit -m [message]# Submit workspace since last commit After the change, go directly to the warehouse area git commit -a# Show all on submission diff information git commit -v# Use a new commit,Replace last submission# If there are no new changes in the code, it is used to rewrite the last time commit Submission information for git commit --amend -m [message]# Redo the last commit and include the new changes of the specified file git commit --amend [file1] [file2]


# List all local branches git branch# List all remote branches git branch -r# Lists all local and remote branches git branch -a# Create a new branch, but still stay in the current branch git branch [branch-name]# **** Create a new branch and switch to it ****git checkout -b [branch]# Create a new branch pointing to the specified commitgit branch [branch] [commit]# Create a new branch and establish a tracking relationship with the specified remote branch git branch -- track [branch] [remote branch]
# Switch to the specified branch and update the workspace git checkout [branch-name]# Switch to previous branch git checkout -# Establish a tracking relationship between an existing branch and a specified remote branch git branch --set-upstream [branch] [remote-branch]# Merge the specified branch to the current branch git merge [branch] --no-ff# Merge a branch into the current workspace (forcibly merge branches with completely different histories) git merge <Corresponding branch> --allow-unrelated-histories# Select one commit,Merge into current branch git cherry-pick [commit]# Delete branch git branch -d [branch-name]# Delete local remote branch record git branch -dr [remote/branch]# Delete remote branch git push origin -- delete [branch name]

Remote synchronization

# Download all changes to the remote warehouse git fetch [remote]# Show all remote warehouses git remote -v# Displays information about a remote warehouse git remote show [remote]# Add a new remote warehouse and name it git remote add [shortname] [url]# Retrieve the changes from the remote warehouse and merge with the local branch git pull [remote] [branch]# Upload specified local branch to remote warehouse git push [remote] [branch]# Forcibly push the current branch to the remote warehouse, even if there is a conflict git push [remote] --force# Push all branches to remote warehouse git push [remote] - all


# Restore the specified files in the staging area to the workspace git checkout [file]# Restore a commit To the staging area and workspace git checkout [commit] [file]# Restore all files in the staging area to the workspace git checkout .# Clean up files and directories not included in version management git clean -df# Resets the specified file in the staging area, the same as the last time commit Consistent, but the workspace remains the same git reset [file]# Reset staging area and workspace, same as last time commit bring into correspondence with git reset --hard# Resets the pointer of the current branch to the specified value commit,The staging area is also reset, but the workspace remains unchanged git reset [commit]# Resets the of the current branch HEAD Specify for commit,Reset the staging area and workspace at the same time, as specified commit agreement git reset --hard [commit]# Reset current HEAD Specify for commit,But leave the staging area and workspace unchanged git reset --keep [commit]# Create a new commit,Used to undo the assignment commit# All changes in the latter will be offset by the former and applied to the current branch git revert [commit]# Remove the uncommitted changes temporarily, and then move them to git stashgit stash pop later

Submodule submodule

# Add sub module git submodule add [url] [path]# Sub module batch operation git sub module foreach [git status]

Common commands

Add ignore file or directory

git ignore file needs to submit a hidden file ". gitignore", which defines the rules for ignoring files; You can create this file from the git bash command line

touch .gitignore

Configuration syntax: The directory is indicated by the slash "/"; Use asterisk "*" to match multiple characters; With a question mark Wildcard single character A matching list containing a single character in square brackets "[]"; With exclamation mark "!" Indicates that the matched files or directories are not ignored (followed by sufficient files); git for The ignore configuration file matches the rules from top to bottom, which means that if the matching range of the previous rules is larger, the latter rules will not take effect;

Relevant knowledge


Markdown is a typesetting language jointly designed by Aaron Swartz and John Gruber. Markdown's goal is to achieve "easy to read and write".


The file used to describe the project is written in Markdown syntax. The file name is "", which is actually the abbreviation of Markdown.


Github is a commercial website that uses Git ideas to work. It has more than 24 million developers open source their own projects on it. These 24 million users cover almost all the best developers in the world. Getting fork or star on Github means that other engineers approve the project.


gitlab is an open source project for warehouse management system. gitlab is a web service built on gitlab as a code management tool. gitlab can be used to build a website similar to github.

Lying pit record

Git can create 4096 file names, but the maximum is 260 in windows. Because git uses the old version of windows api, it steps on a hole.

Solution: git config - Global core longpaths true

Added by lives4him06 on Thu, 30 Dec 2021 22:00:16 +0200