阿宁的linux学习----Git版本回退

it2026-10-02  5

这是我学习linux的过程,每天都会更新所学习的知识总结,每个例子都是我自己的亲手实践的,作为新人的我希望各位大佬提出宝贵的意见!!

编写一个learn.txt文件

[root@localhost reposiitory]# vim learn.txt The future is scary but you can’t just run to the past cause it’s familiar. Success is the ability to go from one failure to another with no loss of enthusiasm. #第一步,告诉Git把文件添加到仓库 [root@localhost reposiitory]# git add learn.txt #第二步,告诉Git把文件提交到仓库 [root@localhost reposiitory]# git commit -m "versions 1st" [master(根提交) 10ed61a] versions 1st Committer: root <root@localhost.localdomain> 您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确 与否。您可以通过下面的命令对其进行明确地设置以免再出现本提示信息: git config --global user.name "Your Name" git config --global user.email you@example.com 设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份: git commit --amend --reset-author 1 file changed, 2 insertions(+) create mode 100644 learn.txt #继续修改learn.txt文件 [root@localhost reposiitory]# vim learn.txt The future is scary but you can’t just run to the past cause it’s familiar apple. Success is the ability to go from one failure to another with no loss of enthusiasm #查看结果 [root@localhost reposiitory]# git status # 位于分支 master # 尚未暂存以备提交的变更: # (使用 "git add <file>..." 更新要提交的内容) # (使用 "git checkout -- <file>..." 丢弃工作区的改动) # # 修改: learn.txt # 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") #查看修改的内容 [root@localhost reposiitory]# git diff learn.txt diff --git a/learn.txt b/learn.txt index 8477435..d58874e 100644 --- a/learn.txt +++ b/learn.txt @@ -1,2 +1,2 @@ -The future is scary but you can’t just run to the past cause it’s familiar. +The future is scary but you can’t just run to the past cause it’s familiar apple. Success is the ability to go from one failure to another with no loss of enthusiasm

将修改后的文件提交到仓库

[root@localhost reposiitory]# git add learn.txt [root@localhost reposiitory]# git commit -m "version 2nd" [master a478b3a] version 2nd Committer: root <root@localhost.localdomain> 您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确 与否。您可以通过下面的命令对其进行明确地设置以免再出现本提示信息: git config --global user.name "Your Name" git config --global user.email you@example.com 设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份: git commit --amend --reset-author 1 file changed, 1 insertion(+), 1 deletion(-) #提交后查看仓库状态 [root@localhost reposiitory]# git status # 位于分支 master 无文件要提交,干净的工作区

版本回退

#再次创建一个版本 [root@localhost reposiitory]# vim learn.txt The future is scary but you can’t just run to the past cause it’s familiar apple. Success is the ability to go from one failure to another with no loss of enthusiasm GPL. #按照前面的步骤重新来一遍 [root@localhost reposiitory]# git add learn.txt [root@localhost reposiitory]# git commit -m "versions GPL" [master e186dca] versions GPL Committer: root <root@localhost.localdomain> 您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确 与否。您可以通过下面的命令对其进行明确地设置以免再出现本提示信息: git config --global user.name "Your Name" git config --global user.email you@example.com 设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份: git commit --amend --reset-author 1 file changed, 1 insertion(+), 1 deletion(-) #查看修改日志 [root@localhost reposiitory]# git log commit e186dcac99f004c387c7ea33185488cce918d55b #每次修改的版本号 Author: root <root@localhost.localdomain> Date: Wed Oct 21 23:05:24 2020 +0800 versions GPL #最后一次修改的 commit a478b3a701fc92ddd70a2106961fa10816ea5b0b Author: root <root@localhost.localdomain> Date: Wed Oct 21 22:59:34 2020 +0800 version 2nd #倒数第二次修改的 commit 10ed61ad74688524329183b39bf7506289e3dad9 Author: root <root@localhost.localdomain> Date: Wed Oct 21 22:42:47 2020 +0800 versions 1st #第一次修改的

退回上一个版本

[root@localhost reposiitory]# git reset --hard HEAD^ HEAD 现在位于 a478b3a version 2nd #查看版本内容 [root@localhost reposiitory]# vim learn.txt The future is scary but you can’t just run to the past cause it’s familiar apple. Success is the ability to go from one failure to another with no loss of enthusiasm #git reset --hard 【版本号】可以退回到任意版本 [root@localhost reposiitory]# git reset --hard e186dcac99f004c387c7ea33185488cce918d55b HEAD 现在位于 e186dca versions GPL

记录命令

当你你回退到了某个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办? 就用git记录命令的命令 git reflog

[root@localhost reposiitory]# git reflog e186dca HEAD@{0}: reset: moving to e186dcac99f004c387c7ea33185488cce918d55b a478b3a HEAD@{1}: reset: moving to HEAD^ e186dca HEAD@{2}: commit: versions GPL a478b3a HEAD@{3}: commit: version 2nd 10ed61a HEAD@{4}: commit (initial): versions 1st
最新回复(0)