Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing branches in git results in changed files

Tags:

git

branch

When I am switching between branches in git, I am seeing that files are changed even though no updates have been made. This has just started happening recently.

$ git status
# On branch dev
nothing to commit (working directory clean)


$ git checkout master
Switched to branch 'master'


$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   App.Core/ViewModels/CriteriaViewModel.cs
#       modified:   App.Core/ViewModels/UserManagement/AccountTagsViewModel.cs
#       modified:   App.Core/ViewModels/UserManagement/TagViewModel.cs
#
no changes added to commit (use "git add" and/or "git commit -a")

These are files that I changed on the dev branch, but then added and committed. Any ideas as to what I'm doing that would cause this?

like image 311
Joe Avatar asked Mar 19 '14 14:03

Joe


People also ask

Does changing branch change the files?

(Note that the new branch will have exactly the same files and history as the old one.) When you run git checkout xxx to switch branches, files from the new branch are extracted from the "object database", which is the archive kept under . git/objects/ that contains compressed originals of every file in every commit.

What happens when you switch git branches?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.

How can I see which files have been changed in a branch?

To get the list of files modified (and committed!) in the current branch you can use the shortest console command using standard git: git diff --name-only master... If your local "master" branch is outdated (behind the remote), add a remote name (assuming it is "origin"): git diff --name-only origin/master...

What happens to untracked files when switching branches?

Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!


1 Answers

You can check if the difference is in the eol (end of line) characters in those "changed" files.

If it is, make sure your core.autocrlf is set to false:

git config core.autocrlf false

That would avoid any automatic conversion on checkout.

See "git replacing LF with CRLF" for more.

If you need to enforce a consistent eol, use .gitattributes directives, as in "Fixing the line-endings in a Git repository using the .gitattributes file".

like image 161
VonC Avatar answered Sep 20 '22 03:09

VonC