Reset and sync local repository with remote branch

If you have ever gotten to the point where your local git repository is out of sync with your remote, but you just can’t seem to restore things back to normal, then this command is for you.

This git pro-tip will turn your local repository into a mirror image of the remote of your choice. Simply follow these steps to get back to frustration-free development.

This command will destroy any local changes in your current branch. This tip is part of the guide series, [[Git]].

The command:

Remember to replace origin and master with the remote and branch that you want to synchronize with.

git fetch origin && git reset --hard origin/master && git clean -f -d
Or step-by-step:
git fetch origin
git reset --hard origin/master
git clean -f -d
Your local branch is now an exact copy (commits and all) of the remote branch.

Command output:

Here is an example of running the command on a local clone of the JBoss Forge git repository.

sharkbook:forge lbaxter$ git fetch origin && git reset --hard origin/master && git clean -f -d
HEAD is now at 356cd85 FORGE-680
Removing forge-example-plugin/
Removing plugin-container-api/
Removing plugin-container/
Removing shell/.forge_settings
sharkbook:forge lbaxter$
Submitted by George Gastaldi

13 Comments

  1. George Gastaldi says:

    You may also add as an alias in your .gitconfig file:

    [alias]
        resetorigin = !git fetch origin && git reset --hard origin/master && git clean -f -d
        resetupstream = !git fetch upstream && git reset --hard upstream/master && git clean -f -d

    Then you can type:

    git resetupstream

    or

    git resetorigin
  2. Warwick says:

    Brilliant

    Thank you

  3. Dana says:

    Thanks a ton. You save my life (almost) with this.

  4. Awesome, thanks! Thought I was going to have to start my local version over from scratch after getting out of sync.

  5. Cat says:

    Thank you thank you for posting this! I’ve been looking through Stack Exchange for hours, but this is exactly what I needed and it worked perfectly!

  6. […] blog post suggests those aliases (for master branch only, but you can adapt/extend […]

  7. […] git fetch origin && git reset –hard origin/master && git clean -f -dReference 1 […]

  8. […] blog post suggests those aliases (for master branch only, but you can adapt/extend […]

  9. […] blog post suggests those aliases (for master branch only, but you can adapt/extend […]

  10. […] blog post suggests those aliases (for master branch only, but you can adapt/extend […]

  11. […] blog post suggests those aliases (for master branch only, but you can adapt/extend […]

  12. […] after the reset would ensure a working tree exactly identical to the branch you just reset to. This blog post suggests those aliases (for master branch only, but you can adapt/extend […]

Leave a Comment




Please note: In order to submit code or special characters, wrap it in

[code lang="xml"][/code]
(for your language) - or your tags will be eaten.

Please note: Comment moderation is enabled and may delay your comment from appearing. There is no need to resubmit your comment.