在提交中将更改还原为文件
我想仅将特定提交所做的更改还原为给定文件。
我可以使用git revert命令吗?
任何其他简单的方法来做到这一点?
这里描述了我见过的最干净的方法
git show some_commit_sha1 -- some_file.c | git apply -R
类似于VonC的回应,但使用git show
和git apply
。
git revert
是针对提交中的所有文件内容。
对于单个文件,您可以编写脚本:
#!/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
(来自smtlaissezfaire的git-shell-scripts实用程序)
注意:
如果您尚未提交当前修改,则在此处介绍另一种方法。
git checkout -- filename
git checkout
有一些文件选项,从HEAD修改文件,覆盖你的更改。
Dropped.on.Caprica在评论中提到:
你可以给git添加一个别名,这样你就可以执行git revert-file <hash> <file-loc>
并且恢复该特定文件。
看到这个要点。
[alias]
revert-file = !sh /home/some-user/git-file-revert.sh
假设可以更改提交历史记录,下面是一个工作流程,用于在早期提交中恢复单个文件中的更改:
例如,您想要在提交aaa222
恢复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)完成rebase:
git rebase --continue
链接地址: http://www.djcxy.com/p/8985.html