0

I have a problem with Git for Windows.

I have two branches in one project: master and membership. The problem is that, when I modify a file in the membership branch and switch back to master, I still have the modification. The membership branch is a local branch.

If I commit when the master is the active branch, changes are committed to master. If I push when membership is the active branch, a remote branch is created for membership (which is what I want).

I have tried switching branches but it didn't worked.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
erkan demir
  • 1,386
  • 4
  • 20
  • 38
  • Did you commit your changes to `membership` before switching to `master`? Otherwise it is expected behavior. – musiKk Nov 21 '14 at 10:08
  • I'm his associate. The problem is, usually git asks to commit before switching branches. This time it's not asking for some reason. – Sefa Nov 21 '14 at 10:24
  • 1
    @vgSefa I think newer git versions are smart about that in that they don't ask if the checkout does not involve conflicts. It saves you a stash-unstash-bracket around the checkout. – musiKk Nov 21 '14 at 10:26
  • 1
    @vgSefa Ah, that's simple: Git asks, if it cannot preserve those changes while switching branches. (If the affected files are different between the current and new branch.) –  Nov 21 '14 at 10:26

1 Answers1

2

You still see local modifications after switching branches because Git is not malicious. It does not have seemingly-safe commands that secretly destroy your data. The commands that destroy your data are the commands that you might reasonably expect to destroy your data.

If you want to completely get rid of local uncommitted modifications, there's a separate command just for that: git reset --hard. To also get rid of uncommitted newly created directories and files, use git clean -df.

You can do that before or after you switch branches.

If you don't want to get rid of your local uncommitted modifications, if you just don't want them to be part of your new branch, commit or stash them before switching branches.

  • Upvoted for the word 'malicious'... And the brilliant explanation. – Andy J Nov 21 '14 at 10:16
  • I noticed that stashing can cause tracked but uncommitted files to 'disappear'. Isn't this is a bit dangerous if you forget you have a stash? – Andy J Nov 21 '14 at 10:23
  • @AndyJ0076 It's no more dangerous than if you forget on which branch you committed them, isn't it? The files are still there, and Git can tell you all available branches, tags and other refs, including stashes. –  Nov 21 '14 at 10:25
  • yeah, to me it's a feature, enabling me to patch things when I'm in the middle of some work, finish the work, then do something with the patch afterward on another branch. – ryenus Nov 22 '14 at 00:33