1

I have an App.config file that is modified locally but I do not want to commit the changes:

On branch test
Your branch is up-to-date with 'origin/test'.

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:   Test/App.config

no changes added to commit (use "git add" and/or "git commit -a")

So according to another related question I executed the following command:

git update-index --assume-unchanged Test/App.config

So that git status gives the following output:

On branch test
Your branch is up-to-date with 'origin/test'.

nothing to commit, working directory clean

Everything seems fine, except when I try to switch to another branch. The output when I executed git checkout test2:

error: Your local changes to the following files would be overwritten by checkout:
        Test/App.Config

My question is, is there any way to switch branch while keeping the locally modified file? Or is git stash the only solution?

Community
  • 1
  • 1
rexcfnghk
  • 14,435
  • 1
  • 30
  • 57
  • 2
    If you want to keep the file unmodified when switching branches, absolutely use `git stash`. – Ry- May 22 '15 at 07:03

2 Answers2

1

Your problem is that the file App.config has been modified in the other branch.

Because the file has been modified, you MUST tell git how to handle the issue (commit or stash).

Maybe using a git ignore could be an other solution for you ?

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • Actually `App.config` does not exist in the other branch, but is it related to the reason why I am getting the error? – rexcfnghk May 22 '15 at 07:09
  • @rexcfnghk Yes, if App.config existed in the other branch, you could've switched – Antzi May 22 '15 at 07:16
1

Git assumes that the file is unchanged so it will not show it in the git status.

As a good scm tool git must ensure that you do not accedentially loose changes. If you switch to the other branch the file Test/App.config would be overridden. That's why git refuses the checkout and tells you that there are changes.

I would suggest to not use assume unchanged. Instead stash the local changes, checkout another branch and pop the stash.

In order to fix your repo you should do

 git update-index --no-assume-unchanged Test/App.config
 git stash
 git checkout test2
 git stash pop
René Link
  • 48,224
  • 13
  • 108
  • 140