puzzl
puzzl

Reputation: 831

Git checkout an old branch: any way to not delete newly created files?

Here's the situation. We've got:

branch_old
branch_new

Branch_new contains a newly created file, DeveloperOptions. This file is part of the repo (so that it gets pulled down with new installs), however, a script then runs on the developer machine when they compile the project that adds git --assume-unchanged to that file. The developer is then allowed to specify their own personal "options" for compilation by modifying that file (mainly output log levels and output customization) in a way that will be ignored by git and thus never committed/pushed/changed on remote.

Here's the problem: when we checkout from branch_new (that has the file) to branch_old (that doesn't have the file), git won't allow the checkout because "your working tree contains changes that would be overwritten by checkout." Now, if we branched branch_new to branch_very_new, we could bounce back and forth endlessly with no problems. But on the old branch, because the file didn't exist, git is trying to delete it from the system in the checkout, thus causing the warning.

Is there any way around this? Alternatively, is there any decent way for git to ignore all future changes to a file that is tracked? That's really all we are going for here.

Upvotes: 3

Views: 111

Answers (1)

Schleis
Schleis

Reputation: 43700

Git is very careful to not overwrite changes on you. So you need to write an alias or script for changing branches so that your users changes are not overwritten.

git update-index --no-assume-unchanged <config file>
git stash //Put it in the stash to change branches
git checkout <branchname>
git stash pop //Bring back your changes
git update-index --assume-unchanged <config file>

Your assume-unchanged prevents git from tracking the changes. So it is treating it sort of like an untracked file, your other branch has the old changes and checking out that branch means that git will want to apply those changes (this also happens if the branch is tracking a file that is untracked in your branch). Git isn't sure how to resolve the differences so it stops your checkout to prevent losing anything.

Upvotes: 2

Related Questions