第一步:git add 命令, 添加文件到仓库
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git add readme.txt第二步:git commit 命令,提交文件到仓库
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git commit -m "wrote a readme file" [master (root-commit) 7c5d210] wrote a readme file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 readme.txt 命令Functiongit add添加文件到仓库git commit -m " "提交文件到仓库,-m后面输入的是本次提交的说明修改文件
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ vi readme.txt 命令Functionvi编辑文档按Esc 输入:wq保存退出原文本内容为: Git is a version control system. Git is free software. 修改文本内容为: Git is a distributed version control system. Git is free software.
查看上次修改内容
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git diff readme.txt diff --git a/readme.txt b/readme.txt index e69de29..9247db6 100644 --- a/readme.txt +++ b/readme.txt @@ -0,0 +1,2 @@ +Git is a distributed version control system. +Git is free software. 命令Functiongit diff查看上次修改查看创库当前的状态
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: readme.txt no changes added to commit (use "git add" and/or "git commit -a")上面的命令输出:readme.txt被修改过,但没有提交修改。
命令Functiongit status仓库当前的状态提交修改后,查看当前仓库的状态
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git add readme.txt MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: readme.txt上面的命令输出:可以提交新文件
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git commit -m"add distributed" [master 9791f15] add distributed 1 file changed, 2 insertions(+)提交后再看看仓库的当前状态
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git status On branch master nothing to commit, working tree clean上面命令输出:当前没有需要提交的修改,而且,工作目录是干净的
再次修改文本内容: Git is a distributed version control system. Git is free software distributed under the GPL.
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ vi readme.txt MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: readme.txt no changes added to commit (use "git add" and/or "git commit -a") MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git add readme.txt MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: readme.txt MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git commit -m"append GPL" [master ead6c99] append GPL 1 file changed, 1 insertion(+), 1 deletion(-)不断对文件进行修改,然后不断提交修改到版本库里,就好比玩游戏时,每通过一关就会自动把游戏状态存盘,如果某一关没过去,你还可以选择读取前一关的状态。有些时候,在打Boss之前,你会手动存盘,以便万一打Boss失败了,可以从最近的地方重新开始。 Git也是一样,每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失。
现在有三个版本: 版本1:wrote a readme file 版本2:add distributed 版本3: append GPL
在实际工作中,我们不可能记得每次改动了什么内容,所以版本控制系统里的git log可以告诉我们历史记录。
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git log commit ead6c997dc9dd6455f3ebf9903ebf85226e7ce13 (HEAD -> master) Author: Zhoujy <17372833873@163.com> Date: Fri Oct 9 18:57:12 2020 +0800 append GPL commit 9791f15c72dd3be129899686a536e5e8f954213d Author: Zhoujy <17372833873@163.com> Date: Fri Oct 9 18:43:05 2020 +0800 add distributed commit 7c5d21032f31ab1e3e9f0e3a6017a9d3a63188aa Author: Zhoujy <17372833873@163.com> Date: Mon Oct 5 21:50:31 2020 +0800 wrote a readme file加上- -pretty=oneline参数
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git log --pretty=oneline ead6c997dc9dd6455f3ebf9903ebf85226e7ce13 (HEAD -> master) append GPL 9791f15c72dd3be129899686a536e5e8f954213d add distributed 7c5d21032f31ab1e3e9f0e3a6017a9d3a63188aa wrote a readme file 命令Functiongit log显示提交日志(从最近到最远)git log --pretty=oneline版本号(commit id 、记录)Git版本回退
命令FunctionHEAD表示当前版本HEAD^上个版本HEAD^^上上个版本HEAD~N上N个版本回退到上个版本:add distributed
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git reset --hard HEAD^ HEAD is now at 9791f15 add distributed 命令Functiongit reset回退到之前的版本查看文本内容:
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ cat readme.txt Git is a distributed version control system. Git is free software. 命令Functioncat显示文本内容此时,查看下版本库的当前状态:
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git log commit 9791f15c72dd3be129899686a536e5e8f954213d (HEAD -> master) Author: Zhoujy <17372833873@163.com> Date: Fri Oct 9 18:43:05 2020 +0800 add distributed commit 7c5d21032f31ab1e3e9f0e3a6017a9d3a63188aa Author: Zhoujy <17372833873@163.com> Date: Mon Oct 5 21:50:31 2020 +0800 wrote a readme file发现最新的版本3:append GPL看不到了。 当上面的窗口没有关掉时,可以找到版本3的commit id:
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git reset --hard ead6c HEAD is now at ead6c99 append GPL MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL.如果之前的窗口已经关掉,找不到版本3的commit id时,Git提供了一个命令git reflog。
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master) $ git reflog ead6c99 (HEAD -> master) HEAD@{0}: reset: moving to ead6c 9791f15 HEAD@{1}: reset: moving to HEAD^ ead6c99 (HEAD -> master) HEAD@{2}: commit: append GPL 9791f15 HEAD@{3}: commit: add distributed 7c5d210 HEAD@{4}: commit (initial): wrote a readme file 命令Functiongit reflog记录了每一次命令