Git Branch newbie - How to reverse?

MEM picture MEM · Apr 27, 2011 · Viewed 22.4k times · Source

Not sure if this is a proper way to work with branches.

  • I had this application on structure A.
  • I created a branch Z while I was on that structure A (but I kept my self on the master branch working).
  • While working (on the master branch), I changed that structure and I end up messing the all thing. :)
  • I've never commit nor added those new files to the rep.

Request:

  • I need to revert to the structure existing on the last commit. Revert All Uncommitted changes and also remove new files and folders that may have been added.

How can this be done?

Answer

Dan Breen picture Dan Breen · Apr 27, 2011

If you created a separate branch that got messed up, all you need to do is switch back to the one you were on before: git checkout A (assuming A is the name of the branch you were on... or maybe it's master).

You can delete the bad branch later, if you want, with git branch -d <branchname> and maybe throw in a -f too, if there are unmerged commits.

Edit: If your repository contains lots of bad changes you want to remove, you can use git reset --hard HEAD to undo them. If there are new files (not tracked by git), git clean -fd will remove them all. Obviously, use with discretion as they will not be recoverable by git.