Git提交流程和日志查看
1 简介
在程序员编程过程中,Git作为非常优秀的代码版本管理工具而为广大程序员所喜爱,了解Git的使用,并熟练掌握Git使用过程中的操作和问题解决过程,对于提升工作效率是必须的技能。
2 Git结构
2.1 工作区
写代码所在的区域,即working area
2.2 暂存区
临时存储,可以通过git add命令把新建的文件或者修改的文件添加到暂存区
2.3 本地库
历史版本信息,通过git commit命令将暂存处待提交列表文件提交到本地库。
2.4 远程库[非必需]
和本地库一样,维持版本信息
2.5 三者结构的关系
平时我们使用的命令git add file1 是把文件从工作区提交到暂存区,git commit -m “prompty” file1 是把文件从暂存区提交到了分支master下面,这里因为只有一个分支master,就提交到master上了
3 git命令
3.1 状态查看操作
git status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
查看工作区、暂存区的状态
3.2 添加操作
git add/rm [file name]
$ git status
On branch master
Your branch is up to date with 'origin/master'.
**Untracked files**:
(use "git add <file>..." to include in what will be committed)
demo.txt
nothing added to commit but untracked files present (use "git add" to track)
全恒@Lenovo-PC MINGW64 /d/Git/huashan_east/huashan (master)
$ git add demo.txt
warning: LF will be replaced by CRLF in demo.txt.
The file will have its original line endings in your working directory.
全恒@Lenovo-PC MINGW64 /d/Git/huashan_east/huashan (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
**Changes to be committed:**
(use "git reset HEAD <file>..." to unstage)
new file: demo.txt
git add 将工作区的“新建/修改”添加到暂存区
3.3 提交操作
git commit –m “commit message” [file name]
将暂存区的内容提交到本地库。
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: demo.txt
全恒@Lenovo-PC MINGW64 /d/Git/huashan_east/huashan (master)
$ git commit -m "[ADD] demo.txt"
[master 745ab10] [ADD] demo.txt
1 file changed, 1 insertion(+)
create mode 100644 demo.txt
3.4 查看历史记录操作
3.4.1 git log
git log 显示的非常详细,包括哈希值,HEAD指针,提交者用户名和邮箱,日期,以及提交信息。
$ git log
commit 219f70eb8fc7948a23dab1d8457de4e20b135e34 (HEAD -> master)
Author: yanchenmochen <[email protected]>
Date: Sat Sep 15 10:02:08 2018 +0800
Insert FFFFF
commit cac9187e3ff3ede2d0df2ff500ca1a8aaefbea23
Author: yanchenmochen <[email protected]>
Date: Sat Sep 15 10:00:43 2018 +0800
Insert EEEEEE
commit 13b6dc9fbd446b06b4e17c0e9b3bd1bfc2b80563
Author: yanchenmochen <[email protected]>
Date: Sat Sep 15 09:59:17 2018 +0800
添加DDDDDD
commit 9361d95e05015bb47d083db0fa31b9a2e46e98a2
Author: yanchenmochen <[email protected]>
Date: Sat Sep 15 09:45:47 2018 +0800
My Second Commit, Modify good.txt
commit 5b59936d144352af147dc8f1c818846350fff6e7
Author: yanchenmochen <[email protected]>
Date: Sat Sep 15 09:34:44 2018 +0800
My first commit. new file good.txt
或许上述的显示方式太过具体,这样很容易占满全屏,可以使用git log –pretty=oneline以每次提交显示一行的方式,可以看到每次提交的信息包括commit id和提交信息。
3.4.2 git log –pretty=oneline
全恒@Lenovo-PC MINGW64 /d/Git/WeChat (master)
$ git log --pretty=oneline
219f70eb8fc7948a23dab1d8457de4e20b135e34 (HEAD -> master) Insert FFFFF
cac9187e3ff3ede2d0df2ff500ca1a8aaefbea23 Insert EEEEEE
13b6dc9fbd446b06b4e17c0e9b3bd1bfc2b80563 添加DDDDDD
9361d95e05015bb47d083db0fa31b9a2e46e98a2 My Second Commit, Modify good.txt
5b59936d144352af147dc8f1c818846350fff6e7 My first commit. new file good.txt
也可以使用轻便的commit id显示
全恒@Lenovo-PC MINGW64 /d/Git/WeChat (master)
$ git log --oneline
219f70e (HEAD -> master) Insert FFFFF
cac9187 Insert EEEEEE
13b6dc9 添加DDDDDD
9361d95 My Second Commit, Modify good.txt
5b59936 My first commit. new file good.txt
Git也提供了git reflog命令,可以非常方便的进行辅助版本切换。可以看到从当前版本切换到另一个版本HEAD指针需要移动的次数。[email protected]{值}表示移动的次数。
全恒@Lenovo-PC MINGW64 /d/Git/WeChat (master)
3.4.3 git reflog
$ git reflog
219f70e (HEAD -> master) [email protected]{0}: commit: Insert FFFFF
cac9187 [email protected]{1}: commit: Insert EEEEEE
13b6dc9 [email protected]{2}: commit: 添加DDDDDD
9361d95 [email protected]{3}: commit: My Second Commit, Modify good.txt
5b59936 [email protected]{4}: commit (initial): My first commit. new file good.txt
HEAD指针用于版本指向。可以用于历史记录的前进和回退。
4 总结
在工作中首先要弄清楚的是Git的操作流程,以及Git的原理。这样对于后续Git的理解非常关键。