[英]Git checkout a revision without deleting unaffected files
I want to revert to a previous revision without deleting files, a visual representation of this would be: 我想恢复到以前的版本而不删除文件,对此的直观表示是:
3 files; 3个文件;
myfile
mysecondfile
mythirdfile
Now this is currently origin/master
and my working copy. 现在,这是当前的
origin/master
和我的工作副本。
What I want to do is revert to a previous revision, lets say DEADBEEF
, 我想做的是还原到先前的修订,比如说
DEADBEEF
,
DEADBEEF
revision only has one file; DEADBEEF
修订版只有一个文件。
myfile
What I want to do is overwrite my local myfile
with the previous revision without deleting mysecondfile
or mythirdfile
. 我想要做的是用以前的版本覆盖本地
myfile
,而不删除mysecondfile
或mythirdfile
。
If you have the possibility to use the xargs
command line command that is an easy task. 如果您有可能使用
xargs
命令行命令,那么这很容易。
$ git ls-tree --name-only -r <tree-ish> . | xargs git checkout <tree-ish> --
git ls-tree
will list the content of a tree object git ls-tree
将列出树对象的内容 --name-only
will restrict the output to the (relative, use --full-name
for absolute paths) path of the files --name-only
将输出限制为文件的路径(相对路径,使用--full-name
表示绝对路径) -r
will show all files recursively -r
将递归显示所有文件 |
will pipe the output of one command to the next xargs
is used here to give the result of ls-tree
as arguments to git checkout
xargs
在这里用于给出ls-tree
的结果作为git checkout
参数 git checkout <tree-ish> --
will (in this form) checkout files from the given tree-ish
without changing branches git checkout <tree-ish> --
将(以这种形式)从给定的tree-ish
检出文件,而不更改分支 To view the changes that you did since <tree-ish>
use: 要查看自
<tree-ish>
所做的更改,请使用:
$ git diff --staged -R
If the command is used without -R
you would see the changes as if you changed everything to the old state. 如果不使用命令
-R
,如果你改变了一切旧的状态,你会看到的变化。
To create a new commit with the content of an old commit (and your current files), try a combination of git read-tree
, git checkout-index
and git update-index
: 要使用旧提交(以及当前文件)的内容创建新提交,请尝试使用
git read-tree
, git checkout-index
和git update-index
:
git read-tree DEADBEEF
git checkout-index -f -a
git update-index -q --refresh
Then commit. 然后提交。
Meaning: no need to use shell trick with xargs
: git has the tools you need. 含义:无需在
xargs
上使用shell技巧:git提供了所需的工具。
git read-tree DEADBEEF
will restore the index from that old commit (which will make all other files "untracked" or "modified", since they were not part of that old commit DEADBEEF
) git read-tree DEADBEEF
将从该旧提交中恢复索引(这将使所有其他文件“未跟踪”或“已修改”,因为它们不属于该旧提交DEADBEEF
) git checkout-index -f -a
will force the working tree to reflect that index (again, without modifying the files which were not part of the index) git checkout-index -f -a
将强制工作树反映该索引(同样,无需修改不属于索引的文件) git update-index -q --refresh
will make sure index and working tree are in sync. git update-index -q --refresh
将确保索引和工作树同步。 Finally add and commit: you will get all your other files plus the content of the old commit. 最后添加并提交:您将获得所有其他文件以及旧提交的内容。
For the situation you've given there, the command is 对于您在此处给出的情况,命令是
git checkout deadbeef -- .
which resets every file in commit deadbeef to its contents as of that commit. 这会将提交Deadbeef中的每个文件重置为该提交时的内容。
If mr_sudaca's answer doesn't satisfy your desire I'm afraid there's no such possibility in git. 如果mr_sudaca的答案不能满足您的需求,恐怕git中没有这种可能性。
If you want your master HEAD to be pointed to myfile previous revision and keep mysecondfile and mythirdfile current state - just make it the git way, clear and easy: 如果您希望将主HEAD指向myfile的先前版本,并保持mysecondfile和mythirdfile的当前状态-只需使用git方式即可,简单明了:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.