简体   繁体   English

如何在不丢失工作的情况下返回早期版本?

[英]How to go back to an earlier revision without losing work done?

I've done some work on a project but then realized it wasn't the way to go. 我已经完成了一个项目的工作,但后来意识到这不是要走的路。 So I want to go back to an earlier, clean, revision. 所以我想回到早期的,干净的修订版。 However, I do not want to lose the work I've done between this clean revision and today, so that I can refer to it later if I change my mind. 但是,我不想失去我在今天这个干净的版本之间所做的工作,所以如果我改变主意,我可以稍后再参考它。

What would be the best way to handle this in git? 在git中处理这个问题的最佳方法是什么?

这正是git stash存在的原因:

  1. git stash
  2. git checkout earlier_point
  3. git stash pop

You have two reasonable options: 你有两个合理的选择:

  1. Use git-stash . 使用git-stash Just type git stash to stash, or git stash save some descriptive message to give it a descriptive message. 只需输入git stash to stash,或git stash save some descriptive message即可为其提供描述性消息。 You can then reapply this later with git stash apply (or git stash pop ). 然后你可以使用git stash apply (或git stash pop )重新应用它。

  2. Make a commit, drop a tag on it, then reset back to parent. 进行提交,在其上放置一个标记,然后重置回父级。 You can then retrieve your work later by accessing the tag. 然后,您可以通过访问标记来检索您的工作。 This would be git commit -m 'Temp work'; git tag tempWork; git reset --hard HEAD^ 这将是git commit -m 'Temp work'; git tag tempWork; git reset --hard HEAD^ git commit -m 'Temp work'; git tag tempWork; git reset --hard HEAD^

Assuming your "bad" commits were done on master, they have NOT been shared with anyone and 13d7 represents the last "good" commit: 假设你的“糟糕”提交是在master上完成的,那么它们并没有与任何人共享,而13d7代表了最后一次“好”的提交:

git checkout -b my_bad_branch git checkout -b my_bad_branch
//create a new branch that contains everything we've done thusfar //创建一个新的分支,其中包含我们所做的一切
git checkout master git checkout master
git reset --hard 13d7 git reset --hard 13d7
//reset the head pointer on master to point to the known good commit //重置master上的头指针以指向已知的良好提交

Or, if they have been shared: 或者,如果他们已被分享:

git checkout -b my_bad_branch git checkout -b my_bad_branch
git checkout master git checkout master
git revert 13d7 git revert 13d7
//create new commits which undo the bad commits after 13d7 //创建新的提交,在13d7之后撤消不良提交
git push git push
//make sure everyone gets master that fixes the bad commits //确保每个人都获得了修复错误提交的主人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM