0

I branch a new branch...

git checkout -b loginComponent

I create a new file and make some changes to existing files. I then decide... meh, I don't like what I've done... so my goal is to go back to before I branched anything. So I type...

git checkout master

Expecting the files to revert back to before I branched.

I get the following message:

A       app/login.component.html
A       app/login.component.ts
M       app/webff.component.ts
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

However, this does not happen. The files (login.component.html, login.component.ts) remain and the modifications in webff.component.ts are also still there.

am I missing something? I seem to remember this worked in the past...

lowcrawler
  • 6,777
  • 9
  • 37
  • 79

4 Answers4

3

Changes persist when changing branches. To reset the changes to HEAD, use the following command:

 git reset --hard

Note that this will delete ANY uncommitted changes.

2

Commit your new files to your new branch. Then checkout master.

Or, git reset --hard

Angel Huezo
  • 156
  • 4
  • So if I commit before swtiching, then the files would revert (delete) back upon "git checkout master" – lowcrawler May 17 '16 at 20:03
  • yes, if you commit before switching, the changes will go into the loginComponent branch, so there will be no uncommitted changes to persist coming back. – Gregg May 17 '16 at 20:26
1

'git checkout -f' also restores files in the working directory to repository version.

Gregg
  • 2,444
  • 1
  • 12
  • 21
1

You are missing some basic git knowledge here.

A full and complete answer is here: git checkout carries unstaged files to the new branch

Here is the short summary for you.

Git has something called 3-states.

enter image description here

The 3 states are described above.

The missing part

The working directory and the staging are are SHARED between all your branches (* - there is a way to bypass it and to have clean 3 states per branch)

But in your case the working directory and the staging are shared so when you "move" between branches all the none commited content will remain there unless you handle it.


How to discard my changes?

Read this to understand what is HEAD and then use the reset as described in the answer.
How to move HEAD back to a previous location? (Detached head)

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167