Skip to content

Git manual ​

Installation ​

Official website download: https://git-scm.com/downloads After the download is complete, use the default installation.

After the installation is complete, find Git -> Git Bash in the start menu. If something similar to a command line window pops up, it means that Git is installed successfully! The last step of setting is required, enter on the command line:

bash
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

Because Git is a distributed version control system, each machine must report its name: your name and email address.

Create a repository (warehouse) ​

bash
# 创建仓库
mkdir <仓库name> 

# 进入仓库
cd <仓库name>

# 此命令用于显示当前目录
pwd

# 把当前目录初始化成Git仓库
git init

It is not necessary to create a Git repository in an empty directory. You can also choose a directory that already has something in it and directly git init.

Add files to the repository ​

Put the file in the warehouse directory, such as creating a new one test.txt file and then use git add test.txt The command tells Git to add the file to the cache and then use git commit -m "提交描述" Tell Git to commit the file to the repository.

bash
# 添加指定文件或文件夹到缓存区,文件需添加后缀
git add <文件或文件夹name>                   # 单个文件
git add <文件或文件夹name> <文件或文件夹name>  # 多个文件

# 或 全部文件同时添加到缓存区
git add .

# 把文件从缓存区提交至仓库
git commit -m "提交描述"

Submit description specifications ​

Entering clear instructions when submitting code is beneficial to version management. It is recommended to write the submission description with the following keywords

bash
# 新建(add)
# 删除(rm)
# 更新(update)
# 改动(change)
# 实现
# 发布
# 修复
...

Version management ​

Submit changes ​

For example, after modifying the contents of test.txt, run the git status command to see the modified files, and then use git add test.txt, git commit -m "修改描述" Submit the modified file to the warehouse, and it can be used again after submission. git status View current status.

bash
# 显示 新增/删除/被改动等 的文件
git status

View version history ​

bash
# 查看版本记录
git log   # 显示版本号、提交时间等信息

You can also use visualization tools to view Git version history: Right-click on the repository directory > Git BUI Here

Fallback version ​

First of all, Git must know which version the current version is. In Git, HEAD is used to represent the current version, which is the latest submission 1094adb... (note that my submission ID is definitely different from yours), the previous version is HEAD^, and the previous version is HEAD^^. Of course, it is easier to write 100^ for the previous 100 versions, so it is written as HEAD~100.

bash
# 回退到上一个版本
$ git reset --hard HEAD^

At this time, I checked the git log record and found that the original latest version was no longer available. What should I do if I want to go back to the original latest version?This requires knowing the version number of the latest version

bash
# 跳转到指定版本
git reset --hard <版本号前几位>

But what should I do if I don’t know the version number?Git provides a command git reflog to record each of your commands

bash
git reflog

Undo changes ​

https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536

Delete files ​

https://www.liaoxuefeng.com/wiki/896043488029600/900002180232448

Remote warehouse ​

SSH verification ​

To enable this machine to associate with a remote warehouse, SSH authentication is required for the first time.

  • Step 1: Create SSH Key.In the user home (C:\Users\dell) directory, check if there is a .ssh directory. If so, check to see if there are two files id_rsa and id_rsa.pub in this directory. If they already exist, you can jump directly to the next step. If not, open Shell (open Git Bash under Windows) and create SSH Key:
bash
# 创建SSH Key
ssh-keygen -t rsa -C "邮件地址"
  • Step 2: Log in to GitHub, go to avatar in the upper right corner > settings > SSH and GPG keys >Add SSH Key, and paste the contents of the id_rsa.pub file in the key text box.

Associated with remote warehouse ​

After SSH verification is completed, create a warehouse on github. When creating the warehouse, remember to uncheck Initialize this repository with a README, and then run the local command:

bash
# 关联远程仓库,仓库名一般使用origin
git remote add <仓库名> <远程仓库地址>

# 示例
git remote add origin git@github.com:xugaoyi/test.git

In the next step, you can push all the contents of the local library to the remote library

bash
# 把文件推送到远程仓库
git push -u <仓库名> <分支名>

# 示例
git push -u origin master

Since the remote library is empty, when we push the master branch for the first time, we add the -u parameter. Git will not only push the content of the local master branch to the new remote master branch, but also associate the local master branch with the remote master branch, which can simplify the commands when pushing or pulling in the future.

The premise is that the directory has git init initialized into a warehouse, and git status The status has not changed. If there is any change, first git add . Add to cache, git commit -m '提交描述' Submit it to the warehouse and execute the above command.

If you checked Initialize this repository with a README when creating the warehouse, you need to first pull the README.md file to the local warehouse git pull

Multiple remote warehouses can be associated. Be careful to give different remote warehouses different names. Submissions are submitted to different remote warehouses by name.

bash
# 简化的推送命令
git push

View remote warehouse ​

bash
# 查看远程仓库
git remote -v

Delete remote repository ​

bash
# 删除远程仓库
git remote rm <仓库名>

Clone project from remote repository ​

bash
# 从远程库克隆项目
git clone <仓库地址>
Clone specified branch ​
bash
# 克隆指定分支
git clone -b <分支名> <仓库地址>

branch management ​

Create a branch ​

bash
# 创建分支
git checkout -b <分支名>

View branches ​

bash
# 查看分支
git branch

When viewing branches, the * sign in front of the branch indicates the current branch.

switch branch ​

bash
# 切换分支
git checkout <分支名>

merge branches ​

bash
# 合并本地的分支
git merge <分支名>

# 合并远程的分支
git merge <远程仓库名>/<分支名>

Note, the specified branch is merged into the current branch, not the current branch is merged into the specified branch.

Generally, the current branch is switched to the main branch, and then the sub-branch is merged into the main branch.

delete branch ​

bash
# 删除分支
git branch -d <分支名>

Modify branch name ​

bash
# 修改分支名
git branch -m <原分支名> <新分支名>

help command ​

If you are unclear about the command, you can use the git help command to display the git command introduction.

bash
# 帮助命令
git help
bash
$ git help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

Translation tool translate Chinese

bash
$ git help
使用:git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

这些是在各种情况下使用的通用Git命令:

start a working area (参见命令: git help tutorial)
   clone      将存储库克隆到新目录中
   init       创建一个空的Git存储库或重新初始化一个现有的存储库

work on the current change (参见命令: git help everyday)
   add        将文件内容添加到索引中
   mv         移动或重命名文件、目录或符号链接
   reset      将当前磁头重置为指定状态
   rm         从工作树和索引中删除文件

examine the history and state (参见命令: git help revisions)
   bisect     使用二分查找查找引入错误的提交
   grep       打印与模式匹配的行
   log        显示提交日志
   show       显示各种类型的对象
   status     显示工作树状态

grow, mark and tweak your common history
   branch     列出、创建或删除分支
   checkout   切换分支或还原工作树文件
   commit     记录对存储库的更改
   diff       显示提交、提交和工作树等之间的更改
   merge      将两个或多个开发历史连接在一起
   rebase     在另一个基本提示之上重新应用提交
   tag        创建、列表、删除或验证用GPG签名的标记对象

collaborate (参见命令: git help workflows)
   fetch      从另一个存储库下载对象和引用
   pull       从另一个存储库或本地分支获取并与之集成
   push       更新远程引用和相关对象

'git help -a' 和 'git help -g' 列出可用的子命令和一些概念指导。
命令'git help <command>' 或 'git help <concept>' 查看特定子命令或概念.

Reference documentation ​

https://www.liaoxuefeng.com/wiki/896043488029600