Git

① Git概念

Git 是一个分布式版本控制工具,通常用来对软件开发过程中的源代码文件进行管理。通过Git 仓库来存储和管理这些文件,Git 仓库分为两种:①本地仓库:开发人员自己电脑上的 Git 仓库;②远程仓库:远程服务器上的 Git 仓库

commit:提交,将本地文件和版本信息保存到本地仓库

push:推送,将本地仓库文件和版本信息上传到远程仓库

pull:拉取,将远程仓库文件和版本信息下载到本地仓库

② 工作区、暂存区、版本库

版本库:前面看到的.git隐藏文件夹就是版本库,版本库中存储了很多配置信息、日志信息和文件版本信息等

工作区:包含.git文件夹的目录就是工作区,也称为工作目录,主要用于存放开发的代码

暂存区:.git文件夹中有很多文件,其中有一个index文件就是暂存区,也可以叫做stage。暂存区是一个临时保存修改文件的地方

③ git工作区文件状态

Git工作区中的文件存在两种状态:untracked 未跟踪(未被纳入版本控制)和 tracked 已跟踪(被纳入版本控制)

Unmodified 未修改状态 颜色
Modified 已修改状态 红色
staged 已暂存状态 绿色

常用指令

Git全局设置

1
2
3
4
5
#设置用户信息
git config --global user.name "cxz206015"
git config --global user.email "206015cxz@gmail.com"
#查看配置信息
git config --list

获取Git仓库

1
2
3
4
#1.在本地初始化一个Git仓库:
git init
#2.从远程仓库克隆(常用):
git clone < url >

本地仓库操作

1
2
3
4
5
6
7
8
9
10
#本地仓库常用命令如下:
git status #查看文件状态
git add <file> #将文件的修改加入暂存区
git add . #将所有文件添加到暂存区
git reset <file> #将暂存区的文件取消暂存暂存或者切换到指定版本
git reset --hard <file>
git commit #将暂存区的文件修改提交到版本库
git commit -a #提交到本地库等待push
git commit -m "message" <file> #提交指定文件
git log #查看日志

远程仓库操作

1
2
3
4
5
6
git remote							#查看远程仓库
git remote -v #查看版本
git remote add <shortname> <url> #添加远程仓库(关联) shortname:简称
git pull <remote-name> <branch> #从远程仓库拉取(下载) remote-name:origin 远程仓库名 branch:分支名称 master
git push orign master #推送到远程仓库(上传) origin master
--allow-unrelated-histories #合并git init的本地仓库和远程仓库报错,在push后面添加

分支操作

分支是动态的,通过git init 命令创建本地仓库时会默认创建一个 master 分支

1
2
3
4
5
6
7
git branch							#查看分支(列出所有本地分支)
git branch -r #列出所有远程分支
git branch -a #列出所有本地和远程分支
git branch <name> #创建分支
git checkout <name> #切换分支
git merge <name> #合并分支 编辑窗口按"i"可以添加"message",ESC退出,再输入":wq"保存退出
#合并相同文件会发生冲突:先解决冲突,git add结束合并分支,再git commit -m "message" <file>提交,若出现"cannot do a #partial commit during a merge",则在其后面加上"-i",然后git push origin master推送到远程仓库实现合并

标签操作

标签是静态的

1
2
3
git tag								#列出已有的标签
git tag <name> #创建标签
git checkout -b <branch> <name> #检出标签 branch:分支名 name:标签名

总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 开始:检查git配置 (可不做)
git config --list # 查看配置信息
git config --global core.editor vim # 设置默认编辑器为vim(git默认用nano)
git config core.ignorecase false # 设置大小写敏感
git config --global user.name "YOUR NAME" # 设置用户名
git config --global user.email "YOUR EMAIL ADDRESS" # 设置邮箱

# 第一步:初始化一个仓库
git init # 初始化本地版本库
git clone <url> # 克隆远程版本库
git remote add origin git@github.com:用户名/仓库名.git # 配置远程git版本库
git pull origin master # 下载代码及快速合并
git fetch origin # 从远程库获取代码

# 第二步:查看状态 (可不做)
git status # 查看git当前状态
git log # 查看提交历史
git diff # 查看变更内容
git branch # 显示所有分支

# 第三步:修改文件 (可不做)
git mv <old> <new> # 文件改名
git rm <file> # 删除文件
git rm --cached <file> # 停止跟踪文件但不删除

# 第四步:切换提交分支 (可不做)
git branch #查看分支 (列出所有本地分支)
git branch -r #列出所有远程分支
git branch -a #列出所有本地和远程分支
git branch <name> #创建分支
git checkout <name> #切换分支
git merge <name> #合并分支 编辑窗口按"i"可以添加"message",ESC退出,再输入":wq"保存退出

# 第五步:保存和提交
git add . # 跟踪所有改动过的文件
git add <file> # 跟踪指定的文件

git checkout master # 切换到master分支
git switch dev # 切换到dev分支
git checkout -b dev # 创建并切换到dev分支

git commit # 将暂存区的文件修改提交到版本库
git commit -a # 提交到本地库等待push
git commit -m "first commit" # 提交所有更新过的文件备注为"first commit"
git commit -am "first commit" # 提交锁头
git commit --amend # 修改最后一次提交。即没有增加新的提交记录,相当于将当前的修改追加到上一次的提交中

# 第五步:提交到远程仓库
git push # 提交到远程仓库
git push origin master # 上传代码及快速合并

全部指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
git init                                                  # 初始化本地git仓库(创建新仓库)
git config --global user.name "xxx" # 配置用户名
git config --global user.email "xxx@xxx.com" # 配置邮件
git config --global color.ui true # git status等命令自动着色
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global --unset http.proxy # remove proxy configuration on git
git clone git+ssh://git@192.168.53.168/VT.git # clone远程仓库
git status # 查看当前版本状态(是否修改)
git add xyz # 添加xyz文件至index
git add . # 增加当前子目录下所有更改过的文件至index
git commit -m 'xxx' # 提交
git commit --amend -m 'xxx' # 合并上一次提交(用于反复修改)
git commit -am 'xxx' # 将add和commit合为一步
git rm xxx # 删除index中的文件
git rm -r * # 递归删除
git log # 显示提交日志
git log -1 # 显示1行日志 -n为n行
git log -5
git log --stat # 显示提交日志及相关变动文件
git log -p -m
git show dfb02e6e4f2f7b573337763e5c0013802e392818 # 显示某个提交的详细内容
git show dfb02 # 可只用commitid的前几位
git show HEAD # 显示HEAD提交日志
git show HEAD^ # 显示HEAD的父(上一个版本)的提交日志 ^^为上两个版本 ^5为上5个版本
git tag # 显示已存在的tag
git tag -a v2.0 -m 'xxx' # 增加v2.0的tag
git show v2.0 # 显示v2.0的日志及详细内容
git log v2.0 # 显示v2.0的日志
git diff # 显示所有未添加至index的变更
git diff --cached # 显示所有已添加index但还未commit的变更
git diff HEAD^ # 比较与上一个版本的差异
git diff HEAD -- ./lib # 比较与HEAD版本lib目录的差异
git diff origin/master..master # 比较远程分支master上有本地分支master上没有的
git diff origin/master..master --stat # 只显示差异的文件,不显示具体内容
git remote add origin git+ssh://git@192.168.53.168/VT.git # 增加远程定义(用于push/pull/fetch)
git branch # 显示本地分支
git branch --contains 50089 # 显示包含提交50089的分支
git branch -a # 显示所有分支
git branch -r # 显示所有原创分支
git branch --merged # 显示所有已合并到当前分支的分支
git branch --no-merged # 显示所有未合并到当前分支的分支
git branch -m master master_copy # 本地分支改名
git checkout -b master_copy # 从当前分支创建新分支master_copy并检出
git checkout -b master master_copy # 上面的完整版
git checkout features/performance # 检出已存在的features/performance分支
git checkout --track hotfixes/BJVEP933 # 检出远程分支hotfixes/BJVEP933并创建本地跟踪分支
git checkout v2.0 # 检出版本v2.0
git checkout -b devel origin/develop # 从远程分支develop创建新本地分支devel并检出
git checkout -- README # 检出head版本的README文件(可用于修改错误回退)
git merge origin/master # 合并远程master分支至当前分支
git cherry-pick ff44785404a8e # 合并提交ff44785404a8e的修改
git push origin master # 将当前分支push到远程master分支
git push origin :hotfixes/BJVEP933 # 删除远程仓库的hotfixes/BJVEP933分支
git push --tags # 把所有tag推送到远程仓库
git fetch # 获取所有远程分支(不更新本地分支,另需merge)
git fetch --prune # 获取所有原创分支并清除服务器上已删掉的分支
git pull origin master # 获取远程分支master并merge到当前分支
git mv README README2 # 重命名文件README为README2
git reset --hard HEAD # 将当前版本重置为HEAD(通常用于merge失败回退)
git rebase
git branch -d hotfixes/BJVEP933 # 删除分支hotfixes/BJVEP933(本分支修改已合并到其他分支)
git branch -D hotfixes/BJVEP933 # 强制删除分支hotfixes/BJVEP933
git ls-files # 列出git index包含的文件
git show-branch # 图示当前分支历史
git show-branch --all # 图示所有分支历史
git whatchanged # 显示提交历史对应的文件修改
git revert dfb02e6e4f2f7b573337763e5c0013802e392818 # 撤销提交dfb02e6e4f2f7b573337763e5c0013802e392818
git ls-tree HEAD # 内部命令:显示某个git对象
git rev-parse v2.0 # 内部命令:显示某个ref对于的SHA1 HASH
git reflog # 显示所有提交,包括孤立节点
git show HEAD@{5}
git show master@{yesterday} # 显示master分支昨天的状态
git log --pretty=format:'%h %s' --graph # 图示提交日志
git show HEAD~3
git show -s --pretty=raw 2be7fcb476
git stash # 暂存当前修改,将所有至为HEAD状态
git stash list # 查看所有暂存
git stash show -p stash@{0} # 参考第一次暂存
git stash apply stash@{0} # 应用第一次暂存
git grep "delete from" # 文件中搜索文本“delete from”
git grep -e '#define' --and -e SORT_DIRENT
git gc
git fsck

忽略提交文件 .gitignore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# .gitignore文件
# maven #
target

# log #
logs

# windows #
Thumbs.db

# Mac #
.DS_Store

# eclipse #
.settings
.project
.classpath
.log
*.class

# idea #
.idea
*.iml

# Package Files #
*.jar
*.war
*.ear
/target