Search code examples
gitgit-reset

How can I reset a Git repository to its newly-cloned state?


Is there a git statement that has the same result as a fresh git clone?

So even if there are extra branches, extra files, other files, local tags anything... which command can accomplish this?


Solution

  • Your best option to remove all local artifacts is to make a fresh clone.

    Otherwise, you have to run different commands to perform the individual components of the cleanup.

    To get rid of all untracked files, use git clean, with any number of flags turned on to wipe out everything:

    git clean -fd
    

    Now reset all your branches to the remote version (git reset) or delete them if they are local-only (git branch):

    git checkout master
    git reset --hard origin/master
    
    git branch -d local-branch
    

    The same goes for tags. You can delete all local tags like this:

    git tag -d $(git tag -l)
    

    You can do a lot of these operations manually as well, by manipulating the contents of the .git folder, if you know what you are doing.

    As you can see, git clone is by far the easier option.