1

I am trying to switch branch. I have created one branch called nilay using:

git checkout nilay

I can switch to this branch very easily. I can also switch to master branch.

I am trying to edit all of my code in my branch nilay, but in my visual code editor, when I am trying to checkout, I am getting this error:

error: Your local changes to the following files would be overwritten by checkout: 

I want to switch my branches; for example when I use:

 git checkout nilay

I want my code of this branch as it is, and when I change it, it should stay there. And when I switch to master using:

 git checkout master

I should see the master code, but when I am switching I am getting errors. What is the best way to switch branches?

Martin
  • 414
  • 7
  • 21
  • 1
    Possible duplicate of [How do I resolve git saying "Commit your changes or stash them before you can merge"?](https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me) – phd Jul 11 '18 at 08:50
  • come on I am asking totally different topic try to answer it instead you are down voting. I am asking about checkout switch branch –  Jul 11 '18 at 09:18
  • this is well worth a read, it was my turning point in understanding how git works and what its error messages mean: http://nfarina.com/post/9868516270/git-is-simpler – Chris K Jul 11 '18 at 09:23
  • I don't see it is a duplicate. The answer might help solving this, but the question is a different one. – Martin May 02 '19 at 17:59

1 Answers1

3

You are switching correctly. However, Git is telling you that you have uncommitted changes on the currently checked out branch, and if you switch branches those changes will be lost.

You can git commit those changes to keep them, or abandon them with git reset --hard, or defer the decision with git stash (and return to those changes later with git stash pop).

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • is there any way we can switch branch without this commit or stash –  Jul 11 '18 at 08:30
  • @user2480754 if the changes can be just discarded, you could run `git checkout -f `. – ElpieKay Jul 11 '18 at 08:35
  • 3
    But why would you want that? The message is there to prevent you from doing something stupid and inadvertently deleting your data, forcing you to make a decision rather than flip a coin whether you really wanted to delete the changes. It's the equivalent of "Do you really want to exit without saving". As @ElpieKay says you can always force-switch, but I bet we'll soon see a question about how "Git erased a week of my work, help"... – Amadan Jul 11 '18 at 08:42
  • @Amadan thanks for the answer, but this is a duplicate question. – evolutionxbox Jul 11 '18 at 13:39