Git常用命令整理

Git常用命令整理(未完待补充)。

Git常用名词

  • Workspace: 工作区
  • Index/Stage: 暂存区
  • Repository: 仓库区(本地仓库)
  • Remote: 远程仓库
  • Master: 主分支
  • Origin: 默认远程分支

Git基本命令

一、新建(初始化)仓库

1
2
3
4
5
6
7
8
9
# 已有项目代码,前往项目目录创建代码库
$ cd your_project
$ git init
# 没有项目代码
$ git init your_project
$ cd your_project
# 提示:其实两种差别不大,一般使用第一种,可以先创建项目目录,再进去项目目录创建版本库
# 克隆一个仓库(会包括该仓库所有历史)
$ git clone [url]

二、配置Git信息

使用git config命令进行配置,Git的配置文件可以在用户主目录下(全局配置),也可以在项目目录下(项目配置/本地配置)

1.配置Git
1
2
3
4
5
6
7
# 编辑Git配置文件
$ git config -e [--local | --global | --system]
# 配置Git最基本的用户信息
$ git config [--local | --global | --system] user.name 'your name'
$ git config [--local | --global | --system] user.email 'your email'
# 最后,查看一下当前Git的配置信息
$ git config --list [--local | --global | --system]
2.git config的作用域
1
2
3
4
5
6
#只对当前仓库有效
$ git config --local
#当前用户所有仓库有效
$ git config --global
#系统所有用户有效
$ git config --system
3.git config优先级

local > global > system

三、文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 将文件添加到暂存区 git add 的相关命令
# 添加指定文件到暂存区(Index/Stage)
$ git add [file1] [file2] [file3] ....
# 添加已经被Git跟踪的文件到暂存区(新的文件不会被添加到暂存区)
$ git add -u <==> git add --update
# 添加目录到暂存区,会包含子目录
$ git add [dir]
# 添加当前目录下所有文件至暂存区
$ git add .
# 添加工作区所有文件至暂存区
$ git add -A <==> git add --add
# 对一个文件进行了多处个性,并想分次提交
$ git add -p
# 对已被Git管理的文件重命名
$ git mv [file-a] [file-b]
# 停止追踪该文件,该文件会保留在工作区
$ git rm --cached [file]
# 删除工作区文件,并将删除操作放至暂存区
$ git rm [file1] [file2]
# 查看状态
$ git status
# 查看变更区别
$ git diff