How do I completely clean a git working directory?

In mercurial, I am used to being able to do hg up 00 to completely clean out the working directory.

This resets the working directory to it’s state immediately after having performed the hg init.

  • Pushing a commit in git when local repo has not already merged origin HEAD
  • Merge a Pull request which is one commit behind
  • Git pull gives conflicts with octopus strategy
  • Changing Git remote URL updates fetch but not push
  • How to use --color-words with git add --patch?
  • Using Vundle behind Proxy
  • Is there any equivalent in git?

    Note that hg up 00 is not the same as hg up 0, which updates to the first revision committed, instead it is equivalent to hg up null, just a couple of characters shorter to type. *8′)

    Also note that I don’t want to just do an rm -rf * in the root of the git working directory, as then git status will show all of the files as having been removed. I just want to update the repository to it’s state prior to the first commit. Alas my searches through the web and the git manuals haven’t furnished me with the info I need to work out how to do it in git.

    As background, I want to leave the repository in place so that I can easily checkout a different commit later. One reason I want to do this is that I have a very large repository (actually a git svn repo) and I don’t want to have to ever have to re-clone it again (it took days to complete and you aren’t supposed to clone a gitsvn repo). I do want to be able to free up the space taken by the working copy when I don’t need it though.

  • Unable to stop tracking JetBrains .idea files
  • How to access git remote repo since IP address changed
  • '<Branch>' is already checked out at '</other/location>' in git worktrees
  • Latest commit hash of subdirectory
  • Why are my .csproj files getting messed up after a git rebase?
  • How do you get git repos name in some git repository?
  • 3 Solutions collect form web for “How do I completely clean a git working directory?”

    The equivalent of the null revision is called bare repository in git.
    This search gives a rough overview of the topic, different ways of converting a non bare into a bare repo can be found here.

    Unlike hg, there is no easy way to switch between a bare and a non-bare repository. Also you should not push into a non-bare repository, as a push will change the index of git, but does not update the working tree. See Working with git from 2 laptops with no bare repo for details.

    This seems like an unusual thing to want to do, git branches start with an initial commit that is usually non-empty. You can’t “checkout” or “reset” to before this initial commit with usual git commands.

    You can reset the currently checkout branch to an “uncreated” state ready for a new initial commit with this command.

    git updated-ref -d "$(git symbolic-ref HEAD)"

    This deletes the currently checked out branch.

    You can then remove all files from the index with this command (assuming that you are at the root of your repository).

    git rm -r --cached .

    You may need a -f switch if you have unstaged changes.

    Finally you can clean all files from your working tree with this command. Warning: potentially dangerous.

    git clean -fd # WARNING, dangerous

    These are solutions that come to my mind:

    1- You can remove .git directory from your working directory and there will be no git repository in your working directory.

    2- You can reset to initial commit(the initial commit will still remain) by issuing:

    git reset --hard `git rev-list HEAD | tail -n 1`
    Git Baby is a git and github fan, let's start git clone.