Git的基础

1 安装

Ubuntu :

1
sudo apt-get install git

Windows :

1
https://www.git-scm.com/downloads

2 设置

2.1 全局设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//设置列表查看
$ git config --global --list

//用户名和邮箱
$ git config --global user.name denglin
$ git config --global user.email dlgithub@outlook.com

//远程同步操作时,不会重新输入账号密码
$ git config --global credential.helper store

//忽略换行符差异
git config --global core.whitespace cr-at-eol

//别名
$ git config --global alias.st status
$ git config --global alias.lo "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
$ git config --global alias.los "log --color --graph --stat --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

或者是在Git/etc/profile.d上建立sh文件,输入别名 alias gs = ‘git status’

3 启动项目

3.1 从gitlab上获取

1
$ git clone https://www.gitlab.com/denglin/xxxx.git

3.2 从本地上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//git初始化 自动生成 .git .gitignore .gitattributes
$ git init

//远程关联
$ git remote add origin https://www.gitlab.com/denglin/xxx.git

//Add
$ git add .

//commit
git commit -m "Initial commit"

//push
git push -u origin master

在visual studio平台下,打开解决方案,右键选择将解决方案添加到源代码管理,选择Git,自动生成.git,.gitattributes,.gitignore。其中.gitignore文件内自动包含了vs平台不需要纳入源码管理的文件夹和文件名。所以建议不要用$ git init来生成git配置环境。

4 常见命令

Git分工作目录、暂存区、本地仓库、远程仓库

4.1 查看命令

1
2
3
4
5
6
7
8
9
10
11
//查看状态
$ git status
//查看状态(简化版)
$ git status -s

//查看日志
$ git log
//查看日志(简化版)
$ git log --pretty=oneline
//所有操作过的log日志(遇到版本回退后,还能找到回退前之后的日志)
$ git reflog

4.2 添加和提交命令

1
2
3
4
5
6
7
8
9
//工作目录添加到暂存区
$ git add .
$ git add filename
$ git rm filename //添加移除文件

//暂存区提交到本地仓库
$ git commit -m "commit infomation"
//工作目录直接提交到本地仓库
$ git commit -a -m "commit infomation"

4.3 恢复命令

1
2
3
4
5
6
7
8
9
10
11
12
//恢复暂存区的记录到工作目录
$ git checkout .
$ git chekout --filename


//直接恢复本地仓库的记录到工作目录
$ git reset HEAD
$ git reset HEAD filename


//切换到对应的commit_id 并加载到本地目录(比如版本回退)
$ git reset --hard commit_id

4.4 对比命令

1
2
3
4
5
6
7
8
9
10
11
12
//对比工作目录和暂存区
$ git diff
$ git diff filename

//对比暂存区和本地仓库
$ git diff --cached

//对比工作目录和本地仓库
$ git diff HEAD

//对比工作区与指定commit-id的差异
$ git diff commit-id [<path>...]

4.5 远程命令

1
2
3
4
5
6
7
8
//本地仓库master push到远程仓库origin
$ git push origin master

//远程仓库origin fetch到本地仓库master
$ git fetch origin master

//远程仓库origin 直接pull到本地工作目录
$ git pull origin master

4.6 git status -s 命令说明

congmand info
_M 工作目录文件修改,但没有放到暂存区
M_ 工作目录文件修改,已放到暂存区
MM 工作目录文件修改,已放到暂存区,但又再次修改
A 新增到暂存区
?? 新增未跟踪

5 分支

5.1 分支基础流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//查看分支
$ git branch

//创建分支
$ git checkout -b dev

$ git branch dev
$ git checkout dev

//切换分支
$ git checkout master

//合并分支(默认Fast Forward模式)
$ git merge dev

//合并分支(禁用Fast Forward模式,并生成一个Commit)
git merge --no-ff -m "merge with no-ff" dev

//删除分支
$ git branch -d dev

5.2 储藏工作现场(分支切换)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//储藏当前工作现场
$ git stash

//切换到其它分支
//管理
//切换回当前分支

//存储现场
$ git stash list

//恢复工作现场、删除stash内容
$ git stash pop

$ git stash apply
$ git stash drop

6 Tag

6.1 标签命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//查看标签
$ git tag

//查看标签信息
$ git show v0.9

//创建标签
$ git tag v1.0

//为历史版本创建标签
$ git tag v0.9 f52c633

//创建带有说明的标签
$ git tag -a v0.1 -m "version 0.1 released" 1094adb

//删除标签
$ git tag -d v0.1

6.2 远程仓库的标签管理

1
2
3
4
5
6
7
8
9

//推送标签
$ git push origin v1.0

//推送全部标签
$ git push origin --tags

//删除远程标签
$ git push origin :refs/tags/v0.9