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
git fetch origin git reset --hard origin/master git clean -f -d
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$
You may also add as an alias in your .gitconfig file:
Then you can type:
or
Brilliant
Thank you
Thanks a ton. You save my life (almost) with this.
Awesome, thanks! Thought I was going to have to start my local version over from scratch after getting out of sync.
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!
[…] blog post suggests those aliases (for master branch only, but you can adapt/extend […]
[…] git fetch origin && git reset –hard origin/master && git clean -f -dReference 1 […]
[…] Kaynak shell shell […]
[…] blog post suggests those aliases (for master branch only, but you can adapt/extend […]
[…] blog post suggests those aliases (for master branch only, but you can adapt/extend […]
[…] blog post suggests those aliases (for master branch only, but you can adapt/extend […]
[…] blog post suggests those aliases (for master branch only, but you can adapt/extend […]
[…] 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 […]