switching branches with uncommitted changes in git

I am working on my master branch and have some uncommitted changes inside the master branch. I have already created a feature branch and I am able to switch to my feature branch using the git checkout feature_branch command. I have couple of doubts here which I am listing below:

  1. Why am I seeing the uncommitted changes in my branch when I have made my changes inside my master branch. If I commit my changes in the master branch and switch to my feature branch, I do not see the committed changes specific to master. I am expecting the same behavior for uncommitted changes also.
  2. I came across the stash command in Git and there it was mentioned that if you have any uncommitted changes inside the master branch, and would like to switch to some other branch, git will not allow you and hence you need to stash the changes inside the master branch and then only switch to the development branch. I did not face this issue. Am I missing something here and if so, what is the significance of using the stash command?

  • Git Commit Error
  • Unpacker error Git-pushing from bare repo to staging server
  • How do I show more 'recent repositories' in Git GUI?
  • Git instaweb with multiple repositories not working
  • How can I have a graphical 'diff' for git when working locally and also use diff via SSH?
  • Git, revert or reset to a certain commit
  • Have SourceTree detect a rename even if the original file remains
  • find git commits with no author
  • Update forked Github repo with all branches
  • use Winmerge inside of Git to file diff
  • How to I “move” my commits from “no branch” to an actual branch?
  • Error (internetKeychainItemForServer:withUsername:path:port:protocol:) - The specified item could not be found in the keychain
  • One Solution collect form web for “switching branches with uncommitted changes in git”

    If I commit my changes in the master branch and switch to my feature branch, I do not see the committed changes specific to master. I am expecting the same behavior for uncommitted changes also.

    That’s not a sensible behavior. Uncommitted changes are by definition not made to a branch. Branches are pointers to commits, and there is no commit associated with uncommitted changes. They are made to your working directory and have nothing tying them to any branch. They aren’t even associated with Git. Until you commit (or stash) them, Git does absolutely nothing to track them, and it will never try to manage them for you.

    I came across the stash command in Git and there it was mentioned that if you have any uncommitted changes inside the master branch, and would like to switch to some other branch, git will not allow you and hence you need to stash the changes inside the master branch …

    That’s wrong. Stashes don’t exist inside any branch. There is one global pool of stashes.

    … I did not face this issue.

    You will only be prevented from switching branches if doing so would cause your uncommitted changes to be overwritten.

    Am I missing something here and if so, what is the significance of using the stash command?

    You only need to worry about stashing your changes if they affect any files that would be changed by switching branches. If you have modifications to a file that is identical between two branches, switch from one branch to the other will not require a stash. If the file is different on your other branch, Git will not let you switch branches, as this would destroy your uncomitted changes.

    It is safe to try switching branches, and if Git warns you that “uncommitted changes would be overwritten” and refuses to switch branches, you can stash your changes and try again.

    Git Baby is a git and github fan, let's start git clone.