6

In a svn working directory, I can

  1. make some changes in files in the working directory
  2. remove all the changes I made by 'rm all the source files in that directory, but keep my .svn directory)
  3. retrieve what the trunk supposed to by using 'svn update' and svn will download all the source back to my working directory.

Is there an equivalent command in git? can you please tell me how to do that?

Jim Puls
  • 79,175
  • 10
  • 73
  • 78
n179911
  • 19,547
  • 46
  • 120
  • 162

3 Answers3

9
git reset --hard

which is a synonym for "git reset --hard HEAD" should be what you are looking for.

Use it with caution as it would effectively delete any current change in your working directory. See "Undoing in Git"

Note: as Charles Bailey mentions in the comment, and in his answer, this would not removed untracked files.
For that git clean is the right tool. See his answer for more, but also the SO question "How do you remove untracked files from your git working copy?", or the Git-Ready article "cleaning up untracked files".

So if we wanted to do a major cleanup:

$ git clean -n -d -x

That command would clean up files listed under the project’s .gitignore file as well as removing other files and directories that aren’t necessary.
As always use precaution when running git clean, and make sure to double check what you’re really deleting.

Note, you might want to move your untracked files instead.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It may be worth noting that this doesn't remove untracked (either new or ignored) files which the OP's svn recipe would. – CB Bailey Jul 22 '09 at 06:48
4
  1. You want to throw away all of the changes to tracked files in your working directory
  2. You are at the root of your working directory

    $ git checkout .

This will recursively checkout (restore) every file that is being tracked from the last commit in the current branch (head).

Tate Johnson
  • 3,910
  • 1
  • 23
  • 21
2

Assuming that you are at the root of your working copy.

You want to do a clean to remove any untracked files, do a dry run first to make sure that it's not going to clobber anything first. (You may want to investigate the directory (-d) and do use ignore rules (-x) options as well.)

git clean -n

Then run it 'for real'.

git clean -f

Then check out fresh unmodified files from the index for all your tracked files.

git checkout .
CB Bailey
  • 755,051
  • 104
  • 632
  • 656