Search code examples
gitsvntortoisegit

get a clean file copy from git


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.


Solution

  • 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