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

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)


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.

Tags:

Git