Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

Answer from Dan Moulding on Stack Overflow
Top answer
1 of 16
10340

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

2 of 16
748

First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:

git reset --hard @{u}

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).

Next, as needed, use git clean to remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull
🌐
Reddit
reddit.com › r/git › how to make my local git branch exactly match github?
r/git on Reddit: How to make my local Git branch exactly match GitHub?
October 8, 2025 -

Hi everyone,

I’m a beginner with Git and I’ve gotten my local main branch a bit messed up. Here’s roughly what I did step by step:

  1. Made git pull, git add file, made changes to the file. And then I made a commit locally on main.

  2. Realized I shouldn’t have committed in this branch.

  3. Tried to undo it using commands like git reset --hard HEAD~1. And I did it few times because I wasn't sure if it was even working. Everything was done locally. I haven’t pushed anything.

  4. I also staged and unstaged some changes in my file.

  5. Now my local branch is messy, and I’m worried that running commands that interact with GitHub could break things.

So, I just want to completely overwrite my local branch and make it exactly match the main branch on GitHub, discarding all Local commits and changes.

What’s the safest way to do this? I don’t care about losing any local work. I just want my branch to match the remote perfectly.

Discussions

I did a hard reset on my local repo, now it won't sync with remote.
If you know you want to overwrite your remote you need to run git push --force and it will overwrite the remote with what’s on local. ⚠️ This is isn’t reversible. ⚠️ More on reddit.com
🌐 r/git
8
7
August 28, 2022
How to make my local Git branch exactly match GitHub?
`git reset --hard origin/main` is the command you want. The problem with `git reset --hard HEAD~1` is that is removes the last commit each time, so if you ran it 3 times you would have removed 2 extra commits from your local main. More on reddit.com
🌐 r/git
24
27
October 8, 2025
[Git] How to 'git reset' a remote branch?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
13
5
February 12, 2024
How to restore local repo to be the same as remote repo
You're not on a separate branch by any chance? git fetch git reset --hard origin/main Substitute "main" with whatever your default branch is called. This will delete anything in your working folder that isn't on the remote repo, be aware of that before you do this. More on reddit.com
🌐 r/git
5
4
November 19, 2021
People also ask

What is the primary difference between git reset --hard HEAD and git reset --hard origin/main
ANS: git reset --hard HEAD resets the current branch pointer and working directory to the state of the last committed state on your current local branch. git reset --hard origin/main resets your current branch to match the exact commit hash recorded for the remote branch named main in the origin remote.
🌐
sqlpey.com
sqlpey.com › git › force-local-git-to-match-remote
How to Force Local Git Repository to Match Remote State - …
Can I remove untracked files that are listed in my gitignore
ANS: Yes, the command git clean -fdx is specifically used to remove untracked files, directories (-d), and files ignored by .gitignore (-x) from your working directory.
🌐
sqlpey.com
sqlpey.com › git › force-local-git-to-match-remote
How to Force Local Git Repository to Match Remote State - …
Is resetting to origin/HEAD reliable
ANS: Resetting to origin/HEAD synchronizes your local branch with whatever the remote repository defines as its default branch (e.g., origin/main), regardless of which local branch you are currently on. It is generally reliable if the remote configuration is standard.
🌐
sqlpey.com
sqlpey.com › git › force-local-git-to-match-remote
How to Force Local Git Repository to Match Remote State - …
🌐
GitHub
gist.github.com › 44419227ae5eac116ace
Reset local repository to be just like remote repository HEAD · GitHub
Save qingl97/44419227ae5eac116ace to your computer and use it in GitHub Desktop. ... Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch). Note that the first example assumes that the remote repo's name is "origin" and that the branch named "master" in the remote repo matches the currently checked-out branch in your local repo.
🌐
sqlpey
sqlpey.com › git › force-local-git-to-match-remote
How to Force Local Git Repository to Match Remote State - …
July 29, 2025 - # 1. Download the latest remote references git fetch origin # 2. Force the local branch to match the remote branch state (e.g., origin/master) git reset --hard origin/master
🌐
HatchJS
hatchjs.com › home › how to force your local git repository to match the remote repository
How to Force Your Local Git Repository to Match the Remote Repository
January 5, 2024 - ... | Column 1 | Column 2 | Column 3 | |—|—|—| | Command | `git fetch –all && git reset –hard origin/master` | Description | | Example | `git fetch –all && git reset –hard origin/master` | This command will force the local repository ...
Find elsewhere
🌐
Medium
medium.com › @python-javascript-php-html-css › syncing-local-git-branch-with-remote-repository-head-72451d0f17ec
Local Git Branch and Remote Repository HEAD Syncing
August 24, 2024 - `git reset — soft` does not change ... area and the working directory to match the HEAD. ... No, `git reset — hard` affects only your local repository. To update remote branches, you would need to use `git push` with the `-f` option to force the update...
🌐
DEV Community
dev.to › shawon › how-to-reset-a-remote-git-repository-to-match-your-local-state-a-careful-approach-4ld0
How to Reset a Remote Git Repository to Match Your Local State: A Careful Approach - DEV Community
September 17, 2025 - Force push carefully: Rewriting history can cause confusion if done without care, so use --force-with-lease whenever possible. Prepare collaborators to reset their local branches: Anyone who pulled the previous commits will need to reset or reclone to align with the rewritten history. Resetting a remote Git repository to match a clean local state involves:
🌐
Linux Hint
linuxhint.com › make-git-local-same-as-remote
How to make git local same as remote – Linux Hint
This section lists down the steps to keep updating the local repository time by time; whenever the remote repository commits changes; one can follow the steps in this section to apply those changes in the local repository as well: We are taking here a GitHub project as an example, and we want to clone that project to our machine.
🌐
DataCamp
datacamp.com › tutorial › git-pull-force
Git Pull Force: How to Overwrite a Local Branch With Remote | DataCamp
August 6, 2024 - In this article, we learn that these are different things. If you're new to Git, consider reading our Git push and pull tutorial first. We can overwrite our local state with the remote state by using the commands listed below, substituting <branch_name> with the name of your branch.
🌐
Andrewwebster
blog.andrewwebster.dev › 2018 › 10 › git-forcing-local-to-match-remote.html
Answers I Couldn't Find Anywhere Else: Git: Forcing Local to Match Remote
Here's the super easy way to fix ... These simple steps will 1) create a new branch called new-branch-with-my-changes on the local and remote repositories, and 2) overwrite the local master branch to match the remote master branch....
🌐
Continuously Merging
articles.mergify.com › git-overwrite-local-branch-with-remote
Git Overwrite Local Branch with Remote Safely
August 26, 2025 - The git fetch and git reset --hard origin/branch-name workflow is a 100% local operation. You are simply pulling down the latest information from the remote server and forcing your local copy to match it. Nothing gets pushed back up.
🌐
30 Seconds of Code
30secondsofcode.org › home › git › repository › reset master to match remote
Git - Reset your local master branch to match remote - 30 seconds of code
March 31, 2024 - After that, you can switch to the master branch using git checkout master and reset it to match the one on the remote using git reset --hard origin/master. # Syntax # git fetch origin # git checkout master # git reset --hard origin/master git ...
🌐
Reddit
reddit.com › r/git › i did a hard reset on my local repo, now it won't sync with remote.
r/git on Reddit: I did a hard reset on my local repo, now it won't sync with remote.
August 28, 2022 -

I might need some conceptual explanation here, because I think I made a mistake.

I have only some basic notions about how git works and I'm still learning programming, so getting good with it is not my priority atm.

So, I always thought that my REMOTE means == MAIN, but stored in the cloud. It appears I was wrong.

I made some test commits that I intended to revert and pushed them, but then I decided it would be bad to have those listed in the history and that just reverting them wouldn't do. So I followed this SO answer on how to remove commits from history, which basically tells you to hard reset the last commit and then push the changes to remote.

I understand this might mess things up if someone else is working on the repo, but this one is private.

Anyways, as I said, I assumed remote and main are the same thing, so I tried to push my changes and be done with it, but now it complains:

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Not only that, I noticed now that there are 2 branches on github: remote and main, and the remote branch is 1 commit behind. No idea what is wrong about my assumptions here, as I said, I always thought remote == main, but residing on the cloud. Not sure now.

🌐
Graphite
graphite.com › guides › git-hard-reset-remote
Git hard reset to remote - Graphite
git fetch will fetch all of the changes that you do not have yet from the remote repository without merging those changes into your local branch. ... This will help you see if there are any changes that will be discarded by a hard reset. If you see any changes under the header Changes not staged for commit: these changes will be discarded upon a hard reset. Hard reset your local branch to match the remote branch:
🌐
Medium
medium.com › @python-javascript-php-html-css › how-to-align-the-remote-head-with-the-local-branch-3946d1411491
How to Align the Remote Head with the Local Branch
September 16, 2024 - In this guide, we will explore the steps necessary to reset your local branch so that it mirrors the remote repository’s HEAD. This will ensure that any local changes are discarded, and your branch is in perfect sync with the remote. ... The provided scripts help to reset your local Git branch to match the remote repository’s HEAD.
🌐
Codecademy
codecademy.com › article › force-git-pull
How to Force Git Pull to Overwrite Local Changes in Git | Codecademy
One of the most effective ways to force git pull and ensure our local branch is an exact match with the remote branch is by using the git reset --hard command.
🌐
GitHub
gist.github.com › ddeveloperr › 97e7866b3da8042f1ea6324d337a1836
How to force “git push” to overwrite remote repo files WITH LOCAL files
[Hack] easy way is to change url directly from the source code, open .git/config file in your favorite editor or nano , vim. ... [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = git@github.com:lifeeric/uploadToS3.git # here paste your url fetch = +refs/heads/*:refs/remotes/origin/* [branch "main"] remote = origin merge = refs/heads/main [pull] rebase = false
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-reset-a-git-branch-to-a-remote-repository
How to Reset a Git Branch to a Remote Repository? - GeeksforGeeks
May 22, 2024 - To reset your branch to match the remote branch, use the git reset command. The --hard option discards all local changes and sets your branch to be identical to the remote branch.