如何将文件恢复到以前的版本而不覆盖当前更改?
我在尝试将文件恢复到以前的提交时遇到问题,我知道我可以使用 git checkout 恢复单个文件,但问题是我想在该文件中进行更改所以我想知道如何在单个文件的先前提交和当前 HEAD 之间进行某种“合并”?我尝试使用 git reset sha-of-my-commit path/to/my/file ,但它将以前的版本放入暂存区域,同时将最新版本保留在我的工作目录中,不确定如何合并其后的两个文件。
我现在所做的只是 git diff ..sha-of-my-commit path/to/my/file 并复制/粘贴缺失的行,但我相信一定有更好的方法这样做对吗?
I'm having a problem trying to revert a file to a previous commit, I know I can use git checkout
to revert a single file but the problem is I have changes in that file I'd like to keep so I was wondering how to do some sort of "merge" between a previous commit and the current HEAD for a single file? I tried using git reset sha-of-my-commit path/to/my/file
but it puts the previous version in the staging area while keeping the latest version on my working directory not sure how to merge both files after it.
What I did for now was just git diff ..sha-of-my-commit path/to/my/file
and just copy/pasted the missing lines but I believe there must be a better way to do this right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的意思是更改在您的工作树中(未提交):
如果您提交了一些更改,那么您仍然可以执行此操作,只需多做一点工作。假设您的历史记录如下所示:
您想要 A 处的版本,加上 B 处的更改。然后执行以下操作:
请注意,任何存储应用程序,由于它是合并操作,都可能导致合并冲突;您当然应该在继续之前解决这些问题!
Assuming you mean the changes are in your work tree (not committed):
If you had committed some changes, then you can still do it, with a little more work. Suppose your history looks like this:
where you want the version at A, plus the changes from B on. Then do this:
Note that any stash application, since it's a mergey operation, could result in merge conflicts; you should of course resolve those before moving on!