git命令总结
[toc]
1.git简介
1.1 git工作流程

1.2 git四种状态

2.git命令总结
2.1 git工作区域及文件颜色

git status 文件三种颜色的变化
红色- 新增文件或者修改的旧文件-->执行命令
git add .或者git add 文件名
- git已经管理起来的文件-->执行命令
git commit -m '描述信息'
白色
- 已经生成版本的文件
2.2 git提交数据
创建文件
$ touch aaa bbb
查看git文件状态(此时文件是红色的,属于新增文件)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
aaa
bbb
nothing added to commit but untracked files present (use "git add" to track)

提交文件至暂存区
$ git add .
此时再查看文件,文件是绿色的,已被git管理起来
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: aaa
new file: bbb

2.3 git删除数据
2.3.1 git删除暂存区中的文件 git rm --cached
git删除暂存区中的文件
$ git rm --cached aaa bbb
rm 'aaa'
rm 'bbb'
此时文件变回红色
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
aaa
bbb
nothing added to commit but untracked files present (use "git add" to track)

2.3.2 git删除工作区和暂存区中的文件 git rm -f 文件名
查看暂存区中的文件,此时是绿色的
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: aaa
new file: bbb

删除工作区的文件同时暂存区中的文件也会同时被删除
$ git rm -f aaa bbb
rm 'aaa'
rm 'bbb'
$ ls
$ git status
On branch master
nothing to commit, working tree clean
2.4 git移动数据
2.4.1 git提交数据至版本库 git commit -m '描述信息'
创建文件
$ touch aaa bbb
提交文件至暂存区
$ git add .
此时文件是绿色的
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: aaa
new file: bbb
提交文件至版本库
$ git commit -m 'touch aaa bbb'
[master 7215e51] touch aaa bbb
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 aaa
create mode 100644 bbb
此时再查看文件暂存区中已经没有了,已经被git管理起来了
$ git status
On branch master
nothing to commit, working tree clean
2.4.2 git移动数据, 有时会将已经添加至暂存区的文件重命名 git mv 原文件 新文件
此时文件是绿色的
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: aaa
new file: bbb
现在想把暂存区中的文件 aaa 修改为 AAA
$ git mv aaa AAA
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: AAA
new file: bbb

提交文件至git版本 库
$ git commit -m 'change file aaa->AAA'
[master 7de2d02] change file aaa->AAA
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 AAA
create mode 100644 bbb
2.5 git历史数据
2.5.1 git查看历史数据 git log
操作按键
往回滚动(返回到上一个提交)
- 按键
k或↑:向上滚动一行 - 按键
b:回到上一页
向下滚动:
- 按键
j或↓:向下滚动一行。 - 按键
Space:向下滚动一页。
退出日志视图:
- 按键
q:退出git log界面
查看全部日志
$ git log
commit 7de2d02e662b1c47cb23085240860bf6a8d0d800 (HEAD -> master)
Author: 什么都不会 <pp@163.com>
Date: Sun Feb 23 21:10:03 2020 +0800
change file aaa->AAA
commit b32661c0627eb5cdac793c3e80bcc89f3d40a13d (origin/master)
Author: 什么都不会 <pp@163.com>
Date: Sun Feb 23 20:00:47 2020 +0800
清空文件
commit 61acb78adac52288805ab59992e9c260866186f0
Author: 什么都不会 <pp@163.com>
Date: Sat Feb 22 22:16:24 2020 +0800
忽略文件测试
.........
指定显示日志个数
$ git log -n 1
commit 7de2d02e662b1c47cb23085240860bf6a8d0d800 (HEAD -> master)
Author: 什么都不会 <pp@163.com>
Date: Sun Feb 23 21:10:03 2020 +0800
change file aaa->AAA
2.5.2 git以一行的形式查看日志 git log --oneline
以一行的形式查看
$ git log --oneline
7de2d02 (HEAD -> master) change file aaa->AAA
b32661c (origin/master) 清空文件
61acb78 忽略文件测试
4901535 忽略文件测试
1b7cabd 提交忽略文件
a32513a (bug) master清空测试文件
42f05ec 文件内容就是文件名
d66565f caonima
4ae41d2 提交haha hehe test
c266f9e touch hehe
7e353d7 增加test文件内容
9f30440 touch test
显示更长的commit号
$ git log --pretty=oneline
7de2d02e662b1c47cb23085240860bf6a8d0d800 (HEAD -> master) change file aaa->AAA
b32661c0627eb5cdac793c3e80bcc89f3d40a13d (origin/master) 清空文件
61acb78adac52288805ab59992e9c260866186f0 忽略文件测试
49015353f48daf06f8e14c8d11f692eae795caa1 忽略文件测试
1b7cabd3ee779d68da6cf07241bd8a8dc1542ad4 提交忽略文件
a32513a471688ba24dff4851ce7b2100314c5497 (bug) master清空测试文件
42f05ec321aa987ecf5da2fc303ead235bd59822 文件内容就是文件名
d66565f107148d0827bbed931616a5f21b9bc581 caonima
4ae41d2706308d05fae5ef2183e7f6933bd955e0 提交haha hehe test
c266f9ebd1d9bdac4fe8ce265484cf1c58ca6c68 touch hehe
7e353d71a408ec7414e42cbd51b39f208a16d618 增加test文件内容
9f30440387a13fec21d0de2e0e55ce12e32cd5ae touch test
2.5.3 显示具体内容变化 git log -p
$ git log -p
commit 7de2d02e662b1c47cb23085240860bf6a8d0d800 (HEAD -> master)
Author: 什么都不会 <pp@163.com>
Date: Sun Feb 23 21:10:03 2020 +0800
change file aaa->AAA
diff --git a/AAA b/AAA
new file mode 100644
index 0000000..e69de29
diff --git a/bbb b/bbb
new file mode 100644
index 0000000..e69de29
.........