reset
local
branch
repository
head
git

Reset local repository branch to be just like remote repository HEAD

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want a local branch to become exactly the same as its remote counterpart, the standard solution is to fetch the latest remote state and hard-reset the local branch to that remote-tracking ref. The command sequence is short, but it is destructive because it can discard local commits and working-tree changes.

The Basic Reset Sequence

If your branch is main, the core workflow is:

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

This does three separate things:

  • 'git fetch origin updates the remote-tracking references'
  • 'git switch main moves you to the branch you want to align'
  • 'git reset --hard origin/main makes the branch, index, and tracked working tree match the remote branch exactly'

After that, the local main branch points to the same commit as origin/main.

A More Generic Upstream Form

If your local branch already tracks the correct upstream branch, you can also reset to the upstream reference directly.

bash
git fetch origin
git reset --hard @{u}

@{u} means "the upstream of the current branch". This is convenient when you are already on the right branch and do not want to hardcode the branch name.

What --hard Actually Removes

git reset --hard is not only a history operation. It also overwrites tracked-file changes in your index and working tree.

That means you should inspect the branch before resetting:

bash
git status
git log --oneline --decorate --graph --max-count=10

If you see local work you might need later, save it first. Typical options are:

  • 'git stash'
  • a temporary backup branch
  • a regular commit if the work is worth keeping

Creating a backup branch is cheap and often safer than relying on memory.

bash
git branch backup-before-reset

Untracked Files Need Separate Cleanup

A hard reset does not remove untracked files or directories. If you also need the working tree to match the remote checkout exactly, you may need:

bash
git clean -fd

Preview first if you are not sure:

bash
git clean -nd

That step is also destructive, so treat it with the same caution as reset --hard.

What About Remote HEAD?

If you literally mean the branch pointed to by the remote's default HEAD, Git usually records that as origin/HEAD, which points to something like origin/main.

You can inspect it with:

bash
git symbolic-ref refs/remotes/origin/HEAD

In most practical cases, though, resetting to the explicit branch name or to @{u} is clearer.

A Backup Branch Is Often the Safest Habit

When there is any uncertainty, creating a temporary branch before resetting is cheap insurance. It costs almost nothing, and it gives you an easy recovery point if you realize later that the local commits or worktree state contained something worth keeping. Destructive Git commands are much less stressful when recovery is one ref away.

Common Pitfalls

A common mistake is running git reset --hard before checking whether local commits or edits need to be preserved.

Another mistake is assuming the command also deletes untracked files. It does not; that is what git clean is for.

It is also easy to reset the wrong branch if you forget to switch first. Always confirm the current branch before destructive commands.

Summary

  • Use git fetch plus git reset --hard origin/branch to make a local branch match the remote branch.
  • '@{u} is a convenient shortcut when the current branch tracks the correct upstream.'
  • 'reset --hard discards tracked local changes.'
  • 'git clean -fd is separate and removes untracked files.'
  • Check status or create a backup branch before resetting if there is any chance you need the local work.

Course illustration
Course illustration

All Rights Reserved.