Reset local repository branch to be just like remote repository HEAD
Ever made local changes that you want to discard completely and start fresh from the remote version? Learn how to safely reset your local branch to match the remote repository’s HEAD without keeping any local commits.
If you want to reset your local branch so that it mirrors the remote branch exactly—discarding any local changes or commits—you can do that with a few simple Git commands. This is especially helpful if your local repo has become messy, or you're troubleshooting and just want a clean slate.
Here’s how you can do it:
Steps to Reset Local Branch to Remote HEAD
Make sure you're on the branch you want to reset:
git checkout your-branch-name
Fetch the latest changes from the remote:
git fetch origin
Reset your branch to match the remote’s HEAD:
git reset --hard origin/your-branch-name
This command will completely align your local branch with the remote branch and discard any local changes or commits.
(Optional) Clean untracked files and directories:
git clean -fd
Use this if you also want to remove files that Git isn’t tracking (like test files or build artifacts).
Important Notes
- This process is destructive — any uncommitted changes or local commits will be lost.
- Double-check if you need to keep any work before using --hard and clean.
Use Case
This is often used in scenarios where:
- You're switching between different tasks.
- Your branch has gone out of sync.
- You're starting fresh from the remote state.
Always be cautious and make a backup if you’re unsure about losing local work!