0

I have two working branches: master and features. I did git checkout features and worked on my features branch. I liked the changes but didn't commit anything.

I then went back to my master branch (git checkout master) and upon doing a git status, all of the changes that I had in the staging area of my features branch were also in my master branch. This was further proven when I ran my app from the master branch and the changes were there.

The question is how could've the branches exchanged info without a merge? The purpose of using a branch in the first place was that if I messed up, I could've simply deleted the branch, no worries. What has happened is that changes that I made on the features branch automatically transferred to the master branch without me manually doing so.

lightspeed
  • 313
  • 4
  • 11
  • 1
    Possible duplicate of [Modified files in a git branch are spilling over into another branch](https://stackoverflow.com/questions/246275/modified-files-in-a-git-branch-are-spilling-over-into-another-branch) – phd Jul 19 '18 at 18:02

2 Answers2

1

From the docs:

git checkout <branch>

To prepare for working on <branch>, switch to it by updating the index and the files 
in the working tree, and by pointing HEAD at the branch. Local modifications to the 
files in the working tree are kept, so that they can be committed to the <branch>.

So there you go. Your local modifications were kept, and moved over to the new branch, because no merge was required.

If your changes clashed and required a merge, this wouldn't have been possible. In that case you get a message:

error: Your local changes to the following files would be overwritten by checkout:
        <your files here>
Please, commit your changes or stash them before you can switch branches.
Aborting
Lunivore
  • 17,277
  • 4
  • 47
  • 92
  • What if I didn't want to keep the local changes? The point of using a branch in the first place was that if I messed up, I could've simply deleted the branch, no worries. What has happened now is that changes that I made on the `features` branch automatically transferred to the `master` branch without me manually doing so. – lightspeed Jul 19 '18 at 15:31
  • 2
    @MiddleZ By not committing them you *did not put them on a branch*, you just have them in your 'working tree'. To discard them, you could (1) commit them to the branch and discard the branch, or (2) `git checkout ` or (3) `git reset --hard ` them. Git works very hard not to destroy your work, as implied by the second block of this answer. Read [**this**](https://stackoverflow.com/a/3690796/114900) to better understand git's workflow. – msanford Jul 19 '18 at 15:41
0

If the uncommited changes you had in your staging area (when on branch features) concern files (or maybe even parts of files - I'm not certain here) that have not been modified between features and master branch (i.e. these files are the same in both branches), git checkout simply does not touch the changes and lets you change the branch (otherwise, git would probably complain).

What you want to do now is:

  1. git checkout features
  2. git commit
Tomasz Linkowski
  • 4,386
  • 23
  • 38