gitlocalreset

How to reset a local Git repository

Summary: This wiki page shows how to reset a local Git repository.
Date: 5 August 2025

Sometimes I run into a situation where I need to reset my local Git repository to a clean state. For me, this happens sometimes after a failed merge. We'll handle the following topics:

  • Optional: Save your current changes
  • Resetting the local repository
  • Optional: Cleanup
  • Optional: Reapply saved changes

Optional: Save your current changes

If you have local changes that you want to keep, you can save them using git stash. This command temporarily shelves your changes so you can reapply them later.

git stash push -m "New script to reset local repository"

If you prefer working with VS Code, you can also use the built-in Git features to stash changes. Just open the Source Control panel, click on the ellipsis (…) menu, and select “Stash” → “Stash”. VS Code will prompt you to enter a message for the stash.

Create a branch for your changes

Another option is to create a new branch for your current changes:

git commit -a -m "New script to reset local repository"
git branch feature/reset-local-repo-script

Resetting the local repository

To reset your local repository to match the remote repository, you can use the following command, this will discard all local changes and reset your branch to match the remote branch.

git fetch origin
git reset --hard origin/main

- Origin refers to the remote repository. You can check this with git remote. - Main is your local branch. You can check this with git branch.

Cleanup

After resetting your local repository, you may want to clean up any untracked files or directories that were not removed by the reset command. You can do this with:

git clean -fd

From the Git documentation, you could also add the -x option to remove ignored files, for example build artifacts or temporary files.

Optional: Reapply saved changes

If you stashed your changes earlier, you can reapply them using:

# View the list of stashes
git stash list
# Apply the most recent stash
git stash apply
# Or apply a specific stash by name
git stash apply stash@{0}
# If you want to apply the latest stash and remove the stash afterwards
git stash pop

From VS Code, you can also reapply stashed changes by going to the Source Control panel, clicking on the ellipsis (…) menu, and selecting “Stash” → “Apply Stash”. You can choose which stash to apply if you have multiple stashes.

Checkout your new branch

If you created a new branch for your changes, you can switch to that branch and continue working on your changes:

git checkout feature/reset-local-repo-script
gitlocalreset.txt · Last modified: by 127.0.0.1

Exception: Git command failed to perform periodic pull: From https://dev.azure.com/getshiftingcom/Documentation/_git/knowledge * branch main -> FETCH_HEAD fatal

Exception: Git command failed to perform periodic pull: From https://dev.azure.com/getshiftingcom/Documentation/_git/knowledge * branch main -> FETCH_HEAD fatal

An unforeseen error has occured. This is most likely a bug somewhere. It might be a problem in the gitbacked plugin.

More info has been written to the DokuWiki error log.