Git Reset Demystified: How to Restore Your Working Directory to Match the Master Remote Branch Exactly

Quick little tutorial here, mostly for my own personal reference, on how to restore your local working directory to match a remote master branch. Take this scenario for instance, you’re working on a project for work, maybe as a junior developer working on your first professional project, and you make a bunch of changes to your working directory that you want to throw away, and restore your project to match the remote master branch. Here are your four magic commands:

$ git checkout master

$ git fetch origin

$ git reset --hard origin/master

$ git clean -df

Similarly, if you want your local working directory running on your personal computer to match a different remote branch other than master, you can run something like:

$ git checkout users/myusername/myfeaturebranch

$ git fetch origin

$ git reset --hard origin/users/myusername/myfeaturebranch

$ git clean -df

 

topherPedersen