简体   繁体   English

Git签出修订版本而不删除不受影响的文件

[英]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 ,而不删除mysecondfilemythirdfile

Solution first 解决方案优先

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> --

Breakdown of commands used 使用的命令明细

  • git ls-tree will list the content of a tree object git ls-tree将列出树对象的内容
    This means it will show all the files that were present in the repository at a certain commit 这意味着它将显示特定提交时存储库中存在的所有文件
    • --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检出文件,而不更改分支

Bonus: Show what you changed in those files 奖励:显示您在这些文件中所做的更改

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-treegit checkout-indexgit 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的先前版本,并保持mysecondfilemythirdfile的当前状态-只需使用git方式即可,简单明了:

  1. Save mysecondfile and mythirdfile somewhere. mysecondfilemythirdfile保存在某个地方。
  2. Run git revert [you latest commit sha-1] (sha-1 could be found by git log) 运行git revert [您最新提交的sha-1](sha-1可以通过git log找到)
  3. Copy mysecondfile and mythirdfile back then make a new commit. 复制mysecondfilemythirdfile,然后进行新提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM