Search code examples
gitcloud9-ide

How to undo the changes I made in a git branch?


After creating a new git branch, I removed some files. Then I decided I wanted to discard these changes and removed the branch without merging. However the files are not back. What should I do?

The steps I followed:

  • git checkout -b newbranche
  • I'm working in the IDE Cloud9 and removed some files, including a model and migration files.
  • git checkout master and git branch -D newbranche
  • I thought this should undo all the changes/deleted files. I restarted the IDE and also refreshed its explorer (there where you see all the files and folders) but the files I removed are no longer there. How can this be since I didn't merge the changes?
  • While in the master branch, if I enter git status I get an overview of all the deleted files under "Changes not staged for commit". It also says: use "git checkout -- ..." to discard changes in working directory.

How can I fully undo the changes/deleted files I made in the 'newbranche' branch?


Solution

  • I'd say it is a case where you need to be very careful (this will not delete untracked added files, but will undo any modifying changes or deletions)

    git fetch
    git reset --hard origin/master