Git使用基本手册

1 基本配置

1.CentOS7 一般默认安装了 git,可以通过如下命令查看:

$ rpm -qa git
git-1.8.3.1-13.el7.x86_64

2.如果没有安装,则可以使用通过 yum 安装:

$ yum install git -y

3.git 配置文件的使用有如下几种,可通过下面命令查看:

$ git config
usage: git config [options]

Config file location
    --global              使用全局配置文件
    --system              使用系统级配置文件
    --local               使用版本库级配置文件
    -f, --file <file>     使用给定的配置文件

4.配置使用git的用户 邮箱 语法高亮:

$ git config --global user.name 'leon'
$ git config --global user.email 'leon@163.com'
$ git config --global color.ui true

5.查看配置:

$ git config --list
user.name=leon
user.email=leon@163.com
color.ui=true

$ cat .gitconfig 
[user]
        name = leon
        email = leon@163.com
[color]
        ui = true

2 git 初始化

1.初始化:

$ mkdir /gitdir
$ cd /gitdir
$ git init
Initialized empty Git repository in /gitdir/.git/

2.查看状态:

$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

3.初始化完成后在工作目录下会生成一个名为.git的隐藏文件夹,该文件夹下目录说明如下:

$ ls .git/ | xargs -n1
branches	# 分支目录
config		# 定义项目的特有配置
description	# 描述
HEAD		# 当前分支
hooks		# git 钩子文件
info		# 包含一个全局排除文件(exclude)
objects		# 存放所有数据,包含 info 和 pack 两个子文件夹
refs		# 存放指向数据(分支)的提交对象的指针
index		# 保存暂存区信息

3 基本命令

1.在工作目录创建测试文件:

$ touch a b c

2.提交文件到暂存区:

# 提交指定文件
$ git add a
# 提交所有文件,也可使用 * 代替 .
$ git add .

3.撤回提交到暂存区的文件:

# 撤回指定文件
$ git rm --cached a
rm 'a'

4.删除提交到暂存区的文件:

# 先从暂存区撤回到工作区,然后直接删除文件
$ git rm --cached c
$ rm -f c
# 直接同时删除工作目录和暂存区的文件
$ git rm -f b

5.提交当前暂存区的所有文件到本地仓库:

$ git commit -m 'commit a'   

6.重命名已提交到本地仓库的文件:

# 手动修改
$ mv a a.txt
$ git rm --cached a
$ git add a.txt
$ git commit -m 'rename a to a.txt'
# git 修改,把工作区和暂存区的文件同时修改
$ git mv a.txt a
$ git commit -m 'rename a.txt to a'

7.比对工作目录的文件和暂存区文件内容的异同:

$ echo 111 > a
# 工作目录的文件和暂存区文件内容不同了
$ git diff

8.比对暂存区和本地仓库的文件内容的异同:

# 此时暂存区和本地仓库内容相同
$ git diff --cached
# 提交到暂存区
$ git add a
# 此时暂存区和本地仓库内容不同了
$ git diff --cached

9.同时将文件添加到暂存区并提交到本地仓库:

$ git commit -am "add newfile"

10.查看当前版本及之前提交日志:

# 多行显示详细信息
$ git log
# 一行显示简要信息
$ git log --oneline
# 显示当前所在分支及版本
$ git log --oneline --decorate
# 显示提交的具体内容
$ git log -p
# 显示最近提交的 2 条内容
$ git log -2
# 查看指定提交记录的详细内容
$ git log 6ef6282 -p

11.回滚到指定版本:

# 这里 744a169 是通过 git log --oneline 查看到的版本号
$ git reset --hard 744a169 

12.查看所有版本(包括当前版本之后)提交日志:

$ git reflog

4 分支

1.创建分支:

$ git branch testing

2.查看所有分支及当前所在分支:

$ git branch

3.切换到指定分支:

$ git checkout testing 

4.合并指定分支到当前分支:

$ git merge testing

5.删除指定分支:

$ git branch -D testing

6.同时创建并切换到指定分支:

$ git checkout -b testing

5 标签

1.创建标签:

# 给当前版本打标签
$ git tag -a v1.0 -m 'init v1.0'
# 给指定版本打标签
$ git tag -a v0.4 718c699 -m 'init v0.4'

2.查看标签:

# 查看所有已打标签
$ git tag
# 查看指定标签详细信息
$ git show v1.0 

3.回滚到指定标签:

$ git reset --hard v0.4

6 github 远程仓库

1.在 github 创建仓库,复制仓库的 ssh 链接,执行下面命令添加远程仓库:

$ git remote add origin git@github.com:pingface/git_data.git

2.如果添加成功,则可通过下面命令查看到已添加的远程仓库:

$ git remote

3.设置免密推送,需添加公钥到远程仓库:

$ ssh-keygen 
# 将公钥内容复制到 github 的 Settings -> SSH AND GPG keys 页中 SSH keys 下
$ cat .ssh/id_rsa.pub 

4.推送当前主分支到远程仓库:

$ git push -u origin master

5.下载远程仓库的代码:

$ git clone git@github.com:pingface/git_data.git
原文地址:https://www.cnblogs.com/centlnx/p/12735305.html