Git: How do I force Git to overwrite local files?

When working with Git and need to update your local repository with changes from the remote server, you may sometimes find yourself in a situation where you want to force Git to overwrite your local files and ensure that your branch matches the remote version 100%. There are several ways to do this, and here are some ways to do it.

Reset your local branch to match the main branch

To fetch changes from the main branch of a Git project and overwrite all local files with the latest changes from main, you can use the following commands. There is no need to run git pull first when using the combination of git fetch and git reset -hard origin/main. The commands work together and achieve the same effect as git pull, but in a more controlled way. Use the


git fetch origin main
git reset --hard origin/main

git fetch origin main: Retrieves the latest changes from the main branch on the remote server.
git reset -hard origin/main: Resets your local repository (including your workspace) to match

All local changes will be lost, including changes that have not yet been added or committed. Make sure you are ready to lose the changes before running these commands.

Alternative: Backup before overwriting:
If you want to back up your changes before overwriting, you can stash them:


git stash save "Backup before overwrite"

Later you can restore the changes with:


git stash apply

Clear unversioned files (optional):

If there are unversioned files (files that are not under version control), you can clear them with the following command. However, it is optional if you need to remove files that are not versioned.


git clean -fd

-f: Forces the command to perform cleanup.
-d: Also removes unversioned folders.

Why not git pull?

git pull combines fetch and merge: When you run git pull, it fetches changes from the remote server and automatically tries to merge them into your current branch.

The command can be summarised in two steps:
Fetch: Fetches the latest changes from the remote server.
Merge: Merges the downloaded changes into your local branch.

This makes git pull for a quick way to keep your codebase up to date, but it can sometimes lead to conflicts, if you want to overwrite your local changes without merging, git reset -hard origin/main is therefore more appropriate because it directly resets your branch to match the remote server.

Why not just git pull with -hard?
While you could technically use git pull with -hard as a shortcut, this is rarely recommended as it can lead to unintended results, especially if conflicts arise. fetch + reset gives you more control over what happens.

Extra tip: one-line reset version

If you are sure you don't need any local changes, you can use this one-line version. This ensures that you always have an up-to-date copy of main from the remote server without any risk of conflict.


git fetch origin main && git reset --hard origin/main

It doesn't work with git fetch origin main, should I use git fetch origin master?

Older projects: Repositories created before 2020 typically use "master" unless it has been changed manually.
New projects: Repositories created with newer versions of Git use "main" as the default name.

How can you find out what the industry is called?
When cloning a repository, you can see the default branch by running the following command. Here, the active branch (often the default) will be marked with an asterisk (*).


git branch -a

My default branch is called master, which command should I run?

If your default branch is called master, you need to do the following to download and overwrite your local copies. So you simply replace hand with master.


git fetch origin master && git reset --hard origin/master

You can also drag changes from a branch with a different name. Replace with the name of your industry branch, e.g. master, lol etc.


git fetch origin <branch-name&gt
git reset --hard origin/

What is the "main" (or "master") in Git?

In Git, "main" (or previously "master") refers to the name of a branch. When you clone (download) a project from a remote server, a local repository is automatically created with one default branch - usually called "main" (or "master"). This branch serves as the starting point or "main branch" of the project's development and is typically the version that other developers expect to build on or work with.