侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Git 技巧:恢复到前一次提交 (图示分析)

2022-07-03 星期日 / 0 评论 / 0 点赞 / 63 阅读 / 2514 字

对这篇文章的试验:http://www.oschina.net/translate/git-tips-revert-with-new-commit, FYI prerequisites: git in

对这篇文章的试验:http://www.oschina.net/translate/git-tips-revert-with-new-commit, FYI
prerequisites:
git init test
cd test
touch first.txt
git add .
git commit -m "intial commit." (生成commit id: 3c21c9c)
touch second.txt
git add.
git commit -m "2nd commit.",(生成commit id: 3c55cf4)



step1:
git branch revert-branch HEAD^ (新建branch,指向HEAD的前一次commit:3c21c9c)

step2:
git checkout revert-branch(移动HEAD指针到新的branch,同时用HEAD所指的commit覆盖index及working directory)
注:以上两步可以合并成一步git checkout -b revert-branch HEAD^


step3:
git reset --soft master(移动当前branch的head及HEAD到master所指的位置,index及working dir保持不变)


此时执行git status会看到如下结果:

$ git status# On branch revert-branch# Changes to be committed:#   (use  to unstage)##       deleted:    second.txt#


step4:
git commit -m "reverted to initial state."(提交index/staging area的数据到commit history)


step 5:
git checkout master(移动HEAD指针到master分支)


step 6:
git merge revert-branch(执行fast-forward merge)

Let's verify:


Note:
git reset --soft|--mixed|--hard commitID

如果对文中提及的概念不是很清楚的, 请看这里: http://www.cnblogs.com/kidsitcn/p/4513297.html

广告 广告

评论区