如何将文件恢复到以前的版本而不覆盖当前更改?

发布于 2025-01-02 09:50:15 字数 299 浏览 0 评论 0原文

我在尝试将文件恢复到以前的提交时遇到问题,我知道我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2025-01-09 09:50:15

假设您的意思是更改在您的工作树中(未提交):

git stash
git checkout previous-commit path/to/file
git stash pop

如果您提交了一些更改,那么您仍然可以执行此操作,只需多做一点工作。假设您的历史记录如下所示:

- x - A - x - x - x - B - x - x (HEAD)

您想要 A 处的版本,加上 B 处的更改。然后执行以下操作:

git stash
git checkout B path/to/file
git stash
git checkout A path/to/file
git stash pop
git stash pop

请注意,任何存储应用程序,由于它是合并操作,都可能导致合并冲突;您当然应该在继续之前解决这些问题!

Assuming you mean the changes are in your work tree (not committed):

git stash
git checkout previous-commit path/to/file
git stash pop

If you had committed some changes, then you can still do it, with a little more work. Suppose your history looks like this:

- x - A - x - x - x - B - x - x (HEAD)

where you want the version at A, plus the changes from B on. Then do this:

git stash
git checkout B path/to/file
git stash
git checkout A path/to/file
git stash pop
git stash pop

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文