In svn, sometimes I delete a local file then use 'svn update' to get a clean copy. How can I do it in git? git pull/fetch didn't work. The only way I figured out is use diff which is incontinent. Thanks. I'm using tortoiseGit.

有帮助吗?

解决方案

Files are staged and committed differently in git and svn. The gist of the difference from git’s perspective is that with git a file is versioned through a staging area called an index, from there it goes as part of a commit to the local graph of commits that your branch points to, and from there it is propagated to the remote graph of commits when you push the branch to the remote server.

While acknowledging that “latest” is somewhat misleading, to simplify a bit, there are four “latest” file versions: the one in the working tree, the one in the index, the one in the local branch and the one in the remote branch. And the local and the remote branch are not necessarily in sync. So when you say that you want a clean copy of a file you have to refine the question first - and answer to yourself which of the four you are after.

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

其他提示

Try using git checkout:

git checkout <filename>

Note that if you are in the middle of a merge or rebase, and want to resolve a conflict you need to add --ours or --theirs to let git know which <filename> you want.

e.g.:

git checkout --ours <filename>

If you want a filename as it appeared elsewhere in the history (or as it appears on another branch)

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

Note the -- separates git REF names from filenames.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top