If you’ve ever had to reset a git repository, you know that it can be a pain. But with a few simple commands, you can completely reset your repository, including any untracked files. First, make sure you have the latest version of git installed on your computer. (If you don’t have git yet, you can get it here.) Next, open up a terminal window and type the following command: git init This will create a new git repository in your current directory. (If this is not your first time using git, this command will also create an initial commit.) Now we’re going to use the reset command to completely reset our repository. To do this, type the following command: git reset –hard HEAD^ ..
Saving Your Changes (Git Stash)
Before you fire off a command to reset your repository, you should make sure you’re not losing data you want to save.
Git provides a few different kinds of resets. Soft and Mixed resets will reset the repository back to the state it was in at a certain commit (often the HEAD of a branch), but will keep your local changes that you haven’t yet committed. Hard resets, on the other hand, are destructive, and will throw away changes that haven’t been committed yet.
Often a hard reset is needed to properly clean the repository. If you want though, you can “stash” your changes, which will take all uncommitted changes and store them locally. You can pop the stash open with “stash apply” at a later time.
You can also make a new branch, commit the changes, and then reset back to master. This would keep your changes in the commit history forever, and could also be sent to remote source control to be shared with your coworkers.
Performing a Reset (Git Reset)
First, you’ll need to fetch the latest state of the remote repository, usually “origin,” and then checkout the master branch (or whichever one you’re resetting to).
You can also check out and reset to an individual commit using its ID, e.g., git checkout 342c47a4.
Then, perform a hard reset (unless you want to keep changes with a soft reset). Keep in mind this operation is destructive to any unsaved changes.
You can reset to a local commit instead of origin/master, but most of the time you’ll be resetting to the state of the remote.
Resetting Untracked Files (Git Clean)
However, git reset is usually not enough. Resetting in Git only resets files that are actually tracked by Git. This includes code and resources that receive changes.
However, there are also files like packages, local config, build artifacts/output, log files, and other transitory items that aren’t stored in the Git repository (and ignored in .gitignore). To clean these up, and bring your local repo to 100% parity with the state of the remote, you can run git clean:
You can actually run this command without running git reset, which may actually be what you want. If you don’t want to effect your code files, but want to clear up your builds, logs, and packages to start over, git clean may be all you need.
Giving Up Instead: Cloning A New Repo
While the above is the “clean way” to do it with official Git commands, doing a complete reset isn’t very far off from just nuking your local repository folder and cloning a brand new one. There’s no shame in doing that if you plan to reset everything anyway:
Keep in mind that this can only reset back to the state of your remote repository, not to a local commit. This will clone the master branch by default, but you can switch to the branch of your choise with git switch or clone it from the start with git clone -b