博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git系列(二)版本回退
阅读量:4107 次
发布时间:2019-05-25

本文共 3931 字,大约阅读时间需要 13 分钟。

复习

在 中知道了如何初始化版本仓库,将文件添加到仓库以及将文件提交到仓库。对应的git命令分别为:

* git init
* git add -fileName
* git commit -m“message” :message是对当前提交做的解释说明
以及用git status来查看当前仓库的状态。


git status

过了两天,我们继续在原有的文件上工作。我们用git status来查看当前的仓库状态

Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")

它告诉我们hello.txt这个文件被修改了。

git diff

但是由于周末两天假期太浪,周一来的时候我忘记了在上一个版本基础之上做了哪些改动,这时候 git diff 命令起作用了。输入git diff 命令

$ git diffdiff --git a/hello.txt b/hello.txtindex b56e903..a84e2d8 100644--- a/hello.txt+++ b/hello.txt@@ -1,2 +1,3 @@ hello word-hello git\ No newline at end of file+hello git+git is a free software\ No newline at end of file

在命令行中我们能清楚的看到,-hello git \ +hello git相当于这里没变,+git is a free software表示在原有的基础之上我们又加上了这么一行内容,知道了哪里做了改动后我们可以放心的继续工作了。先将上礼拜的内容提交到仓库

asus@asus-pc MINGW64 /f/gitRepository/helloGit (master)$ git add . asus@asus-pc MINGW64 /f/gitRepository/helloGit (master)$ git commit -m"Second Commit"[master 1229e00] Second Commit 1 file changed, 2 insertions(+), 1 deletion(-)

上面没用git add hello.txt而是用git add .来进行了代替。点.代表了该文件夹下所有的改动文件。

当然作为热爱工作学习的你肯定每天都会提交新的东西到仓库,到了星期三早上来打开文件内容已经是

hello wordhello gitgit is a free softwareToday is Monday . My plan is.....Today is Tuesday .The weather is great
git log / git log –pretty=oneline

当然这里的文件内容只是作为演示功能使用,在实际生活中,不可能每次就一行内容改动,当改动内容很大,改动次数变多后你不可能记住每次的改动。这时候我们可以用git log来查看我们的操作记录

asus@asus-pc MINGW64 /f/gitRepository/helloGit (master)$ git logcommit 3246df47611b37166c005794abad78c14242f02c (HEAD -> master)Author: asus <1459134690@qq.com>Date:   Mon Jul 9 23:18:41 2018 +0800    Tuesday is greatcommit 1a0e325ef3f6a4808836fe06707798561219c4c4Author: asus <1459134690@qq.com>Date:   Mon Jul 9 23:16:34 2018 +0800    Monday commitcommit 1229e00eaf7805de220ddfaa74116bb6e9b55719Author: asus <1459134690@qq.com>Date:   Mon Jul 9 23:05:51 2018 +0800    Second Commitcommit aec5e0cb32bde5f5335d441c34d3f7e1f431e756Author: asus <1459134690@qq.com>Date:   Wed Jul 4 23:52:57 2018 +0800    first commit

上面清楚的记录着我们的提交记录和提交注释。commit 后面的那一大串数字记录的是commit id,可以简单理解为版本号。我们也可以用 git log --pretty=oneline命令将上述log精简一下。

3246df47611b37166c005794abad78c14242f02c (HEAD -> master) Tuesday is great1a0e325ef3f6a4808836fe06707798561219c4c4 Monday commit1229e00eaf7805de220ddfaa74116bb6e9b55719 Second Commitaec5e0cb32bde5f5335d441c34d3f7e1f431e756 first commit
git reset

到了星期三早上,发现星期二的内容有点不对,想回退到星期一提交的那个版本。这时候常规操作就是直接上手删掉星期二写的内容,那么这也太low了,而且这还得建立在你能记得那些内容是星期二加上去的前提下。

这时候只要用git reset --hard HEAD^命令就能回到上一个版本啦。

asus@asus-pc MINGW64 /f/gitRepository/helloGit (master)$ git reset --hard HEAD^HEAD is now at 1a0e325 Monday commit

解释下上面的命令,在Git命令中HEAD表示当前版本,HEAD\^表示上一个版本,HEAD\^\^表示上上个版本。那么同理前20个版本是不是要写上20个\^呢?按道理来说可以是可以,但是一来这样书写不美观二来容易出错,这时候直接写成 HEAD~20,–hard代表什么意思会在后期解释。提交命令回退到星期一提交的那个版本后,文件内容变为了

hello wordhello gitgit is a free softwareToday is Monday . My plan is.....

人总是善变的动物,刚回退到了星期一的版本不久后,又开始对星期二的内容念念不忘。好在Git总能让你有后悔药吃,既能回到过去,又能去到将来。

如果这时候命令窗口还没关,可以往上翻记录找到星期二的那个commit id,我这上面的commit id是3246df47611…….,还是用回退命令 git reset --hard 3246d,commit id不用完全写入,只需要输入几个字符就可以区分了。这时候你会发现文件的内容又回到了星期二提交的那个版本。

如果命令窗口已经关了 ,还是可以用git reflog命令查看以往的git命令来找到commit id

asus@asus-pc MINGW64 /f/gitRepository/helloGit (master)$ git reflog1a0e325 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^3246df4 HEAD@{1}: reset: moving to 3246d1a0e325 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^3246df4 HEAD@{3}: commit: Tuesday is great1a0e325 (HEAD -> master) HEAD@{4}: commit: Monday commit1229e00 HEAD@{5}: commit: Second Commitaec5e0c HEAD@{6}: commit (initial): first commit
总结
  • git status : 查看当前仓库的状态
  • git diff : 查看工作区中修改后没添加的内容
  • git log :查看每次的提交记录 或者用 git log –pretty=oneline 来精简log
  • git reset : 版本回退
    • git reset –hard HEAD^ 回退到上个版本
    • git reset –hard HEAD~20 回到前20个版本
    • git reset –hard commitId 回退到具体的某个commit id 版本
  • git reflog :用于查看git命令的操作历史

转载地址:http://hwtsi.baihongyu.com/

你可能感兴趣的文章
Hid Report Descriptor
查看>>
strlen,strcpy,strcat,strcmp,strstr各代表什么意思
查看>>
手机GPRS、短信等设置
查看>>
结构体声明
查看>>
SIM300 AT指令集
查看>>
lwIP移植工作
查看>>
USB 协议简介
查看>>
USB入门系列之一----基础知识
查看>>
USB入门系列之二-----USB的连接模型
查看>>
USB入门系列之三-----USB的电气特性
查看>>
USB入门系列之四 —— USB的线缆以及插头、插座【转】
查看>>
USB入门系列之五 —— USB设备的插入检测机制
查看>>
USB入门系列之六 —— USB设备的枚举过程
查看>>
USB入门系列之七 —— USB的描述符及各种描述符之间的依赖关系【转】
查看>>
LwIP移植心得[转]
查看>>
LWIP ethernetif.c分析
查看>>
wireshark报The capture session could not be initiated 错误
查看>>
MDK中加载指定文件的技巧
查看>>
stm32 堆和栈(stm32 Heap & Stack)【worldsing笔记】
查看>>
STM32 keil mdk启动代码发分析 .
查看>>