从git获取一个干净的文件副本

问题描述 投票:1回答:2

在svn中,有时我会删除一个本地文件,然后使用'svn update'来获得一个干净的副本。我怎么能在git中做到这一点? git pull / fetch不起作用。我想出的唯一方法是使用差异性的差异。谢谢。我正在使用tortoiseGit。

git svn tortoisegit
2个回答
5
投票

文件在git和svn中以不同方式暂存和提交。与git的观点不同的要点是,使用git,文件通过称为索引的临时区域进行版本化,从那里它作为提交到您的分支指向的本地提交图的一部分,并从那里开始将分支推送到远程服务器时,传播到提交的远程图形。

虽然承认“最新”有点误导,但为了简化,有四个“最新”文件版本:工作树中的一个,索引中的一个,本地分支中的一个和远程分支中的一个。本地和远程分支不一定是同步的。所以,当你说你想要一个文件的干净副本时,你必须先改进问题 - 并回答你自己所追求的四个问题。

Latest version from the index:

$ git checkout -- /path/to/file

Latest version from the local branch (will also discard the file in the index):

$ git checkout HEAD -- /path/to/file

Latest version from the remote branch (as current on the local machine, may be stale):

$ git checkout origin/master -- /path/to/file

Latest version from the remote branch (updated to the most recent, bar race conditions - not stale):

$ git fetch
$ git checkout origin/master -- /path/to/file

Latest version from the remote branch (both updated to most recent, and synchronised with the local branch):

$ git pull
$ git checkout master -- /path/to/file

1
投票

尝试使用git checkout

git checkout <filename>

请注意,如果您正处于合并或rebase的中间,并且想要解决冲突,则需要添加--ours--theirs以让git知道您想要哪个<filename>

e.g:

git checkout --ours <filename>

如果你想要一个文件名,因为它出现在历史的其他地方(或者出现在另一个分支上)

git checkout <branch-name or SHA> -- <filename>

请注意,--将git REF名称与文件名分开。

© www.soinside.com 2019 - 2024. All rights reserved.