Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

People also ask

Does switching branches change files?

So if you switch to a different branch and there are no files in the commit that were added to different commit you can: Merge the other branch into the current. That brings all changes from the branch being merged — new files a re added, updated files updated, removed files removed.

How does Git know which changes to keep?

Indexing. For every tracked file, Git records information such as its size, creation time and last modification time in a file known as the index. To determine whether a file has changed, Git compares its current stats with those cached in the index. If they match, then Git can skip reading the file again.


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.