Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?

Git

Git Problem Overview


I'm really new to git and I've been trying to understand why git keeps showing whatever I changed in one branch in another branch when I run git checkout to switch between branches First I tried not using git add and didn't work. However, I tried then using git add, but didn't fix the problem. I'm not using git commit yet.

This is basically what I'm doing:

$ git clone <a_repository>  
$ git branch  
* master  
$ git branch testing  
$ git checkout testing  
...edit a file, add a new one, delete...  
$ git status  
    # On branch testing  
    # Changed but not updated:  
    #   (use "git add/rm <file>..." to update what will be committed)  
    #   (use "git checkout -- <file>..." to discard changes in working directory)  
    #  
    #       deleted:    file1.txt  
    #  
    # Untracked files:  
    #   (use "git add <file>..." to include in what will be committed)  
    #  
    #       file2.txt  
no changes added to commit (use "git add" and/or "git commit -a")  
$ git branch  
  master  
* testing  
$ git checkout master  
D       file1.txt  
Switched to branch 'master'  
$ git status  
    # On branch master  
    # Changed but not updated:  
    #   (use "git add/rm <file>..." to update what will be committed)  
    #   (use "git checkout -- <file>..." to discard changes in working directory)  
    #  
    #       deleted:    file1.txt  
    #  
    # Untracked files:  
    #   (use "git add <file>..." to include in what will be committed)  
    #  
    #       file2.txt  
no changes added to commit (use "git add" and/or "git commit -a")  
  

I thought that, while using branches, whatever you do in one branch, it's invisible to all the other branches. Is not that the reason of creating branches?

I tried using "git add" but the changes are visible in both branches. Do I need to run "git commit" before switching between branches to avoid this?

Git Solutions


Solution 1 - Git

Switching branches carries uncommitted changes with you. Either commit first, run git checkout . to undo them, or run git stash before switching. (You can get your changes back with git stash apply)

Solution 2 - Git

Short answer: yes, you need to commit. Make sure you do it on the right branch, though!

A branch is a pointer to a commit. When you commit with a branch checked out, the branch advances to point to that new commit. When you check out a branch, you're checking out the commit it points to. (You can think of commits as snapshots of your work tree.)

So, if you have changes you haven't committed, they're going to be unaffected by switching branches. Of course, if switching branches is incompatible with your changes, git checkout will simply refuse to do it.

git add is a command for staging changes, which you will then commit. It does not record those changes into the history of the repository. It simply places them into a staging area (the index); git commit then uses the contents of that staging area to create commits.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJPZView Question on Stackoverflow
Solution 1 - GitSean Clark HessView Answer on Stackoverflow
Solution 2 - GitCascabelView Answer on Stackoverflow