Reset and sync local respository 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

One Comment

  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

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.