Why does git keep showing my changes when I switch branches (modified, added, deleted files) whether I run git or not?

I am really new to git and I was trying to understand why git continues to show what I changed in one branch in another branch when I run git checkout to switch between branches. At first I tried not to add and not work using git. However, I tried using git add, but did not fix the problem. I haven't used git commit yet.

This is basically what I am 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 using branches, no matter what you do in one branch, it is invisible to all other branches. Isn't that the reason for 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?

+98
git
Apr 03 2018-11-11T00:
source share
3 answers

Branch switching brings uncommitted changes with you. Migrate first, run git checkout . to undo them, or run git stash before switching. (You can revert your changes with git stash apply )

+116
Apr 03 '11 at 18:00
source share

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

A branch is a pointer to a commit. When you finish committing a branch, the branch moves to a new commit. When you check the branch, you check the commit pointed to. (You may think of committing as snapshots of your work tree.)

So, if you have changes that you have not made, they will not be affected by branch switching. Of course, if the patch branches are incompatible with your changes, git checkout will simply refuse to do this.

git add is a command to change the settings that you then complete. It does not write these changes to the repository history. It simply puts them in an intermediate area (index); git commit then uses the contents of this staging area to create commits.

+24
Apr 03 2018-11-11T00:
source share

Even I ran into the same problem. You must make any changes if you have any. Change the branch and push the new changes. Even delete the node_modules folder if you have not added to gitignore. After receiving the new changes, start npm i. It should work.

-one
Jan 03 '19 at 17:06
source share



All Articles