恢复提交中对文件的更改

发布于 2024-08-28 16:41:59 字数 77 浏览 6 评论 0原文

我只想恢复特定提交对给定文件所做的更改。

我可以使用 git revert 命令吗?

还有其他简单的方法吗?

I want to revert changes made by a particular commit to a given file only.

Can I use git revert command for that?

Any other simple way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

慕烟庭风 2024-09-04 16:41:59

我见过的最干净的方法是描述此处

git show some_commit_sha1 -- some_file.c | git apply -R

与 VonC 的响应类似,但使用 git showgit apply

The cleanest way I've seen of doing this is described here

git show some_commit_sha1 -- some_file.c | git apply -R

Similar to VonC's response but using git show and git apply.

策马西风 2024-09-04 16:41:59

假设可以更改提交历史记录,以下是在早期提交中恢复单个文件中的更改的工作流程:

例如,您想要在提交中恢复 1 个文件 (badfile.txt) 中的更改aaa222

aaa333 Good commit
aaa222 Problem commit containing badfile.txt
aaa111 Base commit

在基础提交上重新建立基础,修改有问题的提交,&继续。

1) 开始交互式变基:

git rebase -i aaa111

2) 通过将 pick 更改为 e (用于编辑),在编辑器中标记问题提交以进行编辑:

e aaa222
pick aaa333

3) 恢复对错误文件的更改:

git show -- badfile.txt | git apply -R

4)添加更改&修改提交:

git add badfile.txt
git commit --amend

5)完成变基:

git rebase --continue

Assuming it is ok to change the commit history, here's a workflow to revert changes in a single file in an earlier commit:

For example, you want to revert changes in 1 file (badfile.txt) in commit aaa222:

aaa333 Good commit
aaa222 Problem commit containing badfile.txt
aaa111 Base commit

Rebase on the base commit, amend the problem commit, & continue.

1) Start interactive rebase:

git rebase -i aaa111

2) Mark the problem commit for edit in the editor by changing pick to e (for edit):

e aaa222
pick aaa333

3) Revert changes to the bad file:

git show -- badfile.txt | git apply -R

4) Add the changes & amend the commit:

git add badfile.txt
git commit --amend

5) Finish the rebase:

git rebase --continue
合久必婚 2024-09-04 16:41:59

更简单:

git reset HEAD^ path/to/file/to/revert

然后

git commit --amend   

git push -f

文件就消失了,提交哈希、消息等是相同的

Much simpler:

git reset HEAD^ path/to/file/to/revert

then

git commit --amend   

and then

git push -f

the file is gone and commit hash, message, etc is the same.

jJeQQOZ5 2024-09-04 16:41:59

git 恢复适用于提交中的所有文件内容。

对于单个文件,您可以编写脚本:(

#!/bin/bash

function output_help {
    echo "usage: git-revert-single-file <sha1> <file>"
}

sha1=$1
file=$2

if [[ $sha1 ]]; then
git diff $sha1..$sha1^ -- $file | patch -p1
else
output_help
fi

来自 git-shell-scripts 实用程序://github.com/smtlaissezfaire" rel="noreferrer">smtlaissezfaire)


注意:

另一种方法是 如果您尚未提交当前的修改,请参见此处

git checkout -- filename

git checkout 对文件有一些选项,修改文件来自 HEAD,覆盖您的更改。


Dropped.on.Caprica 提及在评论中

您可以向 git 添加别名,以便可以执行 git revert-file; 并恢复该特定文件。
请参阅此要点

[alias]
  revert-file = !sh /home/some-user/git-file-revert.sh

git revert is for all file contents within a commits.

For a single file, you can script it:

#!/bin/bash

function output_help {
    echo "usage: git-revert-single-file <sha1> <file>"
}

sha1=$1
file=$2

if [[ $sha1 ]]; then
git diff $sha1..$sha1^ -- $file | patch -p1
else
output_help
fi

(From the git-shell-scripts utilities from smtlaissezfaire)


Note:

another way is described here if you have yet to commit your current modification.

git checkout -- filename

git checkout has some options for a file, modifying the file from HEAD, overwriting your change.


Dropped.on.Caprica mentions in the comments:

You can add an alias to git so you can do git revert-file <hash> <file-loc> and have that specific file be reverted.
See this gist.

[alias]
  revert-file = !sh /home/some-user/git-file-revert.sh
峩卟喜欢 2024-09-04 16:41:59

如果您想重置上次提交后对文件的更改,这就是我通常使用的方法。我认为这是最简单的解决方案。

请注意,该文件将被添加到暂存区域。

git checkout <prev_commit_hash> -- <path_to_your_file>

希望它有帮助:)

If you'd like to reset the changes on a file from your last commit, this is what I'm usually using. I think this is the simplest solution.

Please note that the file will be added to the staging area.

git checkout <prev_commit_hash> -- <path_to_your_file>

Hope it helps :)

鲜血染红嫁衣 2024-09-04 16:41:59

我只需使用 --no-commit 选项来 git-revert,然后在最终提交之前删除您不想从索引中恢复的文件。下面的示例展示了如何轻松还原第二个最近提交中对 foo.c 的更改:

$ git revert --no-commit HEAD~1
$ git reset HEAD
$ git add foo.c
$ git commit -m "Reverting recent change to foo.c"
$ git reset --hard HEAD

第一个 git-reset“取消暂存”所有文件,以便我们可以仅添加回该文件我们想要恢复的文件。最后的 git-reset --hard 删除了我们不想保留的剩余文件恢复。

I would simply use the --no-commit option to git-revert and then remove the files you don't want reverted from the index before finally committing it. Here's an example showing how to easily revert just the changes to foo.c in the second most recent commit:

$ git revert --no-commit HEAD~1
$ git reset HEAD
$ git add foo.c
$ git commit -m "Reverting recent change to foo.c"
$ git reset --hard HEAD

The first git-reset "unstages" all files, so that we can then add back just the one file we want reverted. The final git-reset --hard gets rid of the remaining file reverts that we don't want to keep.

红焚 2024-09-04 16:41:59
git reset HEAD^ path/to/file/to/revert/in/commit

上面的命令将从提交中取出文件,但它会反映在 git status 中。

git checkout path/to/file/to/revert/in/commit

上面的命令将恢复更改(因此您将获得与 HEAD 相同的文件)。

git commit

(通过 --amend 来修改提交。)

git push

这样,已经在提交中的文件将被删除并恢复。

应从进行提交的分支开始执行上述步骤。

git reset HEAD^ path/to/file/to/revert/in/commit

The above command will take file out of commit, but it will reflect in git status.

git checkout path/to/file/to/revert/in/commit

The above command will revert the changes (as a result you get file same as HEAD).

git commit

(Pass --amend to amend commit.)

git push

With this, the file which is already in the commit is removed and reverted.

The above steps should be followed from the the branch where the commit is made.

〃温暖了心ぐ 2024-09-04 16:41:59

您可以按照以下过程操作:

  1. git revert -n <*commit*> (-n 恢复所有更改,但不会
    提交它们)
  2. git add <*filename*> (你想要恢复和提交的文件的名称)
  3. git commit -m 'reverted message' (添加一条用于恢复的消息)
  4. 提交后丢弃其他文件更改,以便文件保留
    使用您在恢复之前提交的更改进行更新

You can follow this procedure:

  1. git revert -n <*commit*> (-n revert all the changes but won't
    commit them)
  2. git add <*filename*> (name of the file/s you want to revert & commit)
  3. git commit -m 'reverted message' (add a message for reverting)
  4. after committing discard the other files changes so the files stay
    updated with the changes you committed before the revert
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文