Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git allowing me to switch branches without committing changes

Tags:

git

I am just learning Git, going through a tutorial. I am in branch seo_title and I have uncommitted changes to file mission.html. I did git checkout master expecting to get the warning about Changes not staged for commit, no changes added, etc, however instead it went ahead and switched branches with the message:

M       mission.html Switched to branch 'master' 

Then when I did git diff mission.html it showed me that the working directory still contains the changes I made while I had the other branch checked out. What am I missing? For what it's worth, I am using Git Bash on Windows.

EDIT: the changes to mission.html have not been added to staging index either.

EDIT 2: I thought the top voted answer was correct, but upon further investigation it doesn't match the behavior I am seeing. Here is a fuller description of what I am doing:

top_directory(master) > git branch new_branch_1 top_directory(master) > git branch new_branch_2 top_directory(master) > git checkout new_branch_1 

(open notepad++ and modify resources.html, save)

top_directory(master) > git status # On branch new_branch_1 # Changes not staged for commit: #   (use "git add <file>..." to update what will be committed #   (use "git checkout -- <file>..." to discard changes in wo # #       modified:   resources.html # no changes added to commit (use "git add" and/or "git commit top_directory(new_branch_1) > git checkout new_branch_2 

This is where I expect git to object and tell me to stash or commit since new_branch_1 and new_branch_2 have different versions of resources.html, but it just switches to the new branch without a warning and it brings the uncommitted changes along:

M       resources.html Switched to branch 'new_branch_2' top_directory(new_branch_2) > git status # On branch new_branch_2 # Changes not staged for commit: #   (use "git add <file>..." to update what will be committed) #   (use "git checkout -- <file>..." to discard changes in working directory) # #       modified:   resources.html # no changes added to commit (use "git add" and/or "git commit -a") 

Is there a mode or a setting that would make it behave this way instead of warning? Or am I still misunderstanding the scenario?

EDIT 3: I get it now. The top rated answer was right, see my last comment on that answer.

like image 980
burgerB Avatar asked Apr 19 '13 20:04

burgerB


People also ask

How do you force a branch to switch?

You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it. The file new_file.


1 Answers

The different behaviour you saw from the last time you tried to switch branches with local changes and now is due to different file changes.

So, let's say we have a branch called 'readme' where you have committed some changes to a file, let's say README.md.

Now, you have switched back to master. You do some work on other files (not README.md). Now you have local changes. If you try to switch back to the 'readme' branch without committing your changes, it will let you. Why? Because switching to the 'readme' branch won't override any of your local changes.

If, however you make a modification to the README.md file on the master branch, then when you try to do a

git checkout readme  

you will encounter

error: Your local changes to the following files would be overwritten by checkout: README.md Please, commit your changes or stash them before you can switch branches. 

because you have changes to README.md that would require a merge.

like image 142
Nabeelah Ali Avatar answered Oct 21 '22 05:10

Nabeelah Ali