-1

having a bit of a panic at the moment. I created a newfeature branch of my app. (git branch newfeature)... made some changes (a mess) so decided to switch back to master (git checkout master) but the changes did not revert. I haven't pushed anything remotely yet. How can I safely get back to my master branch? I thought switching back would revert everything?

Lee Casey
  • 183
  • 10
  • 1
    you have to write 2 lines 1) "git checkout master" and 2) "git reset --hard HEAD" this will work please ignore " ". – Dhruv Patel May 14 '23 at 06:53
  • 1
    Thank you it looks like thats reverted the changes. Is it normal for it to leave the additional dart files that were created in the new feature branch. I just delete those manually? – Lee Casey May 14 '23 at 06:58
  • used "git clean -f" which removed the files. Thank you Dhruv – Lee Casey May 14 '23 at 07:06

1 Answers1

-1

Git won’t delete files when you switch branches. It will instead either (1) switch branches and keep your changes or (2) refuse to switch branches because it cannot manage to switch branches while also keeping your changes (it can’t reconcile your changes and the state of the other branch).

It does that because it doesn’t know if those changes were important to you. So you have to undo them yourself:

  • For tracked files: git reset --hard HEAD
  • For untracked files: I would use a trash utility personally but git clean also works

Imagine if you started on a big code change. Maybe you touch a mix of tracked and untracked files. Then half an hour into your session you realize that you need to fix something on another branch. So you switch branches. You wouldn’t want your changes to be undone simply because you forgot to commit your changes.

Guildenstern
  • 2,179
  • 1
  • 17
  • 39