oh-my-docs

Summary for developers


Project maintained by italkso Hosted on GitHub Pages — Theme by mattgraham

Git

Do not forget the git --help command.

1. 概述

2005,在不能再免费使用 BitKeeper的情况下,为了继续维护Linux 内核,Linux 开源社区(特别是Linus Torvalds)开发了自己的版本系统。他们对新的系统制订了若干目标:速度快、简单设计、对非线性开发模式的强力支持(允许成千上万个并行开发的分支)、完全分布式、有能力高效管理类似 Linux内核一样的超大规模项目(速度和数据量)。下面,让我们深入了解 Git 的相关概念和使用方法。

1.1 版本控制

版本控制(version control)是一种「记录一个或若干文件内容变化,以便将来查阅特定版本的修订情况」的系统。有以下几种类型的版本控制系统:

如果你使用的是装有 macOS 的电脑,那么只要安装了 Xcode Command Line Tools 就可以使用 Git 了。

在终端(Terminal)里运行如下命令,可以查看安装的 Git 版本。如果没有安装,它会提示你安装。你可以到 Git 官网下载相应平台的软件包进行安装。

$ git --version
# git version 2.23.0

你也可以使用 SourceTree、Github Desktop 等 GUI 客户端,但是 GUI 软件只实现了 Git 所有功能的一个子集。如果想要执行所有命令,只能使用命令行模式。

你可以使用下面的命令获取帮助。

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

1.2 Git的基本思想和特点

1.3 Git 的重要概念

1.4 基本的 Git 工作流

1.5 Git 基本操作的含义

用于将修改的文件记录(提交)至本地 Repository 中。

2. 配置

每台计算机上只需要配置一次,程序升级时会保留配置信息。以后也可以通过一些命令来修改这些配置信息。

Git 使用自带的工具 git config 来帮助设置控制 Git 外观和行为的配置变量。这些变量存储在三个不同的位置,每一个级别会覆盖上一级别的配置。

# 查看所有的配置及所在文件
$ git config --list --show-origin 

# 检查配置信息
$ git config --list

# 读取通用配置(/etc/gitconfig 文件)
$ git config --system

# 检查 Git 的某一项配置
$ git config user.name

# 查询 Git 中该变量的 原始值
$ git config --show-origin user.name

# 用户信息设置
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@example.com"

# 文本编辑器设置
$ git config --global core.editor 'vim'

# 命令别名设置/取消设置(也可在终端里直接修改)
$ git config --global alias.s status
$ git config --global unset alias.s

$ git config --global color.ui auto
$ git config --global color.diff auto
$ git config --global color.branch auto

$ git config --global init.defaultBranch main

3. Git 的基础用法

创建(Create)

# Initializing a Repository in an Existing Directory
$ cd /Users/user/my_project
$ git init

# Cloning an Existing Repository
$ git clone https://github.com/libgit2/libgit2 mylibgit

# Tracking New Files
$ git add file_name

当你想新建一个目录并对其使用版本控制时,使用以下命令:

$ mkdir git-learning
$ cd git-learning
$ git init
$ ls -al

你也可以克隆现有仓库。如果需要自定义本地仓库的名字,请指定新目录名(如 learnGit)。Git 支持多种数据传输协议,如https://、git://或 SSH 传输协议。

查看(Browse)

$ git status
$ git log
$ git log --graph
$ git diff
$ git branch
$ git show

你可以使用 git status 查看文件状态,使用 -s选项能够缩短状态命令的输出。

# 查看文件状态
$ git status

# 缩短状态命令的输出
$ git status -s
$ git status --short

# 查看忽略文件
$ cat .gitignore

使用-s或–short选项得到的输出中,不同标记的含义是,?? 表示新添加的未跟踪文件 ,A 表示新添加到暂存区的文件 ,M 表示修改过的文件。

你可以使用 git log 查看提交历史。

# 查看提交历史
$ git log

# 显示每次提交所引入的差异(最近 3 次)
$ git log -p -3

# 查看每次提交的简略统计信息
$ git log --stat

# 格式化展示提交历史(一行展示)
$ git log --pretty=oneline

为文件创建快照:使用 git add,执行后文件处于暂存状态。

# 在learnGit项目下创建一个新的README文件(未跟踪)
$ echo learnGit > README

# 跟踪新文件
$ git add README

# 为改动创建快照
$ git add .

查看已暂存和未暂存的修改:比较工作目录中当前文件和暂存区域快照之间的差异。

# 查看修改后未暂存的变化
$ git diff

# 查看已暂存的将要添加到下次提交里的内容(已暂存文件与最后一次提交的文件差异)
# 下面两条命令等价
$ git diff --staged
$ git diff --cached

更新(Update)

$ git pull
$ git merge
$ git fetch
$ git am

分支(Branch)

$ git checkout -b
$ git checkout -b feature-A
$ git checkout main
$ git checkout -

提交和发布(Commit and Publish)

# 提交更新
$ git commit

# -m:将提交信息与命令放在同一行
$ git commit -m  "Story 182: Fix benchmarks for speed”

# 跳过使用暂存区域
$ git commit -a
$ git commit --amend

$ git rebase -i
$ git push	

其他

使用stash缓存代码修改 WIP(Working In Process)

$ git stash

使用 git rm 命令将文件从已跟踪清单(暂存区)移除,然后提交。

使用 git mv移动或重命名文件。

$ git mv file_from file_to

Revert

$ git reset
$ git checkout
$ git revert

4. Git 命令集锦

# 1 - Start a working area
$ git init			# Create an empty Git repository or reinitialize an existing one
$ git clone			# Clone a repository into a new directory

# Work on the current change
$ git add			# Add file contents to the index
$ git mv    		# Move or rename a file, a directory, or a symlink
$ git restore   	# Restore working tree files
$ git rm      		# Remove files from the working tree and from the index

# 2 - Examine the history and state

$ git bisect    	# Use binary search to find the commit that introduced a bug
$ git diff			# Show changes between commits, commit and working tree, etc
$ git grep      	# Print lines matching a pattern
$ git log     		# Show commit logs
$ git show      	# Show various types of objects
$ git status		# Show the working tree status

# 3 - Grow, mark and tweak your common history
$ git branch		# List, create, or delete branches
$ git commit		# Record changes to the repository
$ git merge			# Join two or more development histories together
$ git rebase    	# Reapply commits on top of another base tip
$ git reset     	# Reset current HEAD to the specified state
$ git switch   		# Switch branches
$ git tag     		 # Create, list, delete or verify a tag object signed with GPG

# 4 - Collaborate
$ git fetch     # Download objects and refs from another repository
$ git pull 		# Fetch from and integrate with another repository or a local branch
$ git push		# Update remote refs along with associated objects

Reference

[1] https://git-scm.com/book/en/v2