问题描述
我在git repo上做了一些更改并做了一些提交.这是我要执行的操作,说我的git历史记录看起来像HEAD-> c1-> c2-> c3-> c4.问题1)现在,如果我还原c1,它是否也还原所有c2,c3,c4?我的理解是,它仅还原c1,而将c2,c3,c4保持不变.2)现在我要还原所有的c1,c2,c3和c4,然后移回HEAD.最好的方法是什么?
I did some changes on my git repo and did some commits. Here is what I want to do, say my git history looks as HEAD --> c1 --> c2 --> c3 --> c4.Questions1) Now if I revert c1, does it revert all c2, c3, c4 as well ? My understanding it reverts only c1 and leave c2, c3, c4 as is.2) Now I want to revert all c1, c2, c3 and c4 and move back to HEAD. What is the best way to do this.
推荐答案
还原将创建一个提交,该提交将撤消要还原的修订的更改.
Revert will create a commit that will undo the changes of the revision you want to revert.
因此,如果您这样做:
git revert c1
您将结束:
它只会还原提交更改.
因此,对于您要删除对HEAD的四个提交的情况:
So, for your scenario where you want to remove four commits previos to the HEAD:
与以下相同:
如果要删除当前提交之前的四个提交.你可以做
If you want to remove the four commits before the current one. you can do
git revert HEAD~5..HEAD~1
系统将提示您为每次还原创建提交消息,并可能解决冲突.
You will be prompted to create the commit messages for each revert, and perhaps to solve conflicts.
如果您未按下所做的更改,我将保持历史记录清洁的方式将是:
In case you HAVE NOT PUSHED your changes, what I would do to keep the history log clean would be:
撤消当前提交并保持代码更改
Undo the current commit and keep the code changes
git reset --soft HEAD~1
存储代码更改
git stash
撤消最后四次提交并清除代码更改
Undo the last four commits and erase code changes
git reset --hard HEAD~4
确保删除所有创建的文件和目录
Make sure I delete any created files and directories
git clean -di
从顶部提交重新应用更改,这是我想要保留的
Re apply the changes from the top commit which is the one I wanted to keep
git stash pop
*修复冲突,以防万一没有冲突时.提交我的更改
*Fix conflicts in case there will beWhen there are no conflicts. Commit my changes
git commit -am "My new changes"
如果准备推动,请推动.
If ready to push, push.
git push
仅当您未进行更改时,此操作.
这篇关于Git:恢复较旧的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!