git 在提交中还原对文件的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2620775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Revert changes to a file in a commit
提问by Lakshman Prasad
I want to revert changes made by a particular commit to a given file only.
我只想恢复对给定文件的特定提交所做的更改。
Can I use git revert command for that?
我可以使用 git revert 命令吗?
Any other simple way to do it?
还有其他简单的方法吗?
回答by mgalgs
回答by tee
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
:
例如,您想badfile.txt
在 commit 中还原 1 个文件 ( ) 中的更改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:
1)开始交互式rebase:
git rebase -i aaa111
2) Mark the problem commit for edit in the editor by changing pick
to e
(for edit):
2)通过更改pick
为e
(用于编辑)在编辑器中标记问题提交进行编辑:
e aaa222
pick aaa333
3) Revert changes to the bad file:
3) 恢复对坏文件的更改:
git show -- badfile.txt | git apply -R
4) Add the changes & amend the commit:
4)添加更改并修改提交:
git add badfile.txt
git commit --amend
5) Finish the rebase:
5)完成rebase:
git rebase --continue
回答by VonC
git revert
is for all file contents within a commits.
git revert
用于提交中的所有文件内容。
For a single file, you can script it:
对于单个文件,您可以编写脚本:
#!/bin/bash
function output_help {
echo "usage: git-revert-single-file <sha1> <file>"
}
sha1=
file=
if [[ $sha1 ]]; then
git diff $sha1..$sha1^ -- $file | patch -p1
else
output_help
fi
(From the git-shell-scriptsutilities from smtlaissezfaire)
(来自smtlaissezfaire的git-shell-scripts实用程序)
Note:
笔记:
another way is described hereif 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.
git checkout
有一些文件选项,从 HEAD 修改文件,覆盖您的更改。
Dropped.on.Capricamentions 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.
您可以为 git 添加别名,以便您可以执行
git revert-file <hash> <file-loc>
并恢复该特定文件。
看到这个要点。
[alias]
revert-file = !sh /home/some-user/git-file-revert.sh
回答by Dan Moulding
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:
我会简单地使用该--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
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.
第一个git-reset
“取消暂存”所有文件,这样我们就可以只添加我们想要恢复的文件。最后git-reset --hard
删除了我们不想保留的剩余文件还原。
回答by Forrest
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.
文件消失了,提交哈希、消息等是相同的。
回答by Bharath T S
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 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).
上面的命令将恢复更改(因此您将获得与 HEAD 相同的文件)。
git commit
(Pass --amend
to amend commit.)
(通过--amend
修改提交。)
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.
应从进行提交的分支开始执行上述步骤。
回答by Imran Sahil
You can follow this procedure:
您可以按照以下步骤操作:
git revert -n <*commit*>
(-n
revert all the changes but won't commit them)git add <*filename*>
(name of the file/s you want to revert & commit)git commit -m 'reverted message'
(add a message for reverting)- after committing discard the other files changes so the files stay updated with the changes you committed before the revert
git revert -n <*commit*>
(-n
还原所有更改但不会提交它们)git add <*filename*>
(要还原和提交的文件的名称)git commit -m 'reverted message'
(添加回复消息)- 提交后丢弃其他文件更改,以便文件在还原之前提交的更改保持更新
回答by Gabeeka
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 notice that the file will be added to the staging area.
请注意,该文件将被添加到暂存区。
git checkout <prev_commit_hash> -- <path_to_your_file>
Hope it helps :)
希望能帮助到你 :)