To git checkout without overwriting data

Léo Léopold Hertz 준영 picture Léo Léopold Hertz 준영 · Aug 18, 2009 · Viewed 41.7k times · Source

How can you git-checkout without overwriting the data?

I run

 git checkout master

I get

error: Entry 'forms/answer.php' would be overwritten by merge. Cannot merge.

This is surprising, since I did not know that Git merges when I git-checkout. I have always run after the command separately git merge new-feature. This seems to be apparently unnecessary if Git merges at checkout.

Answer

Karl Voigtland picture Karl Voigtland · Aug 18, 2009

Git is warning you that forms/answers.php has changes in your working copy or index that have not been committed.

You can use git-stash to save your changes then git-stash apply to restore them.

The common use case of git-stash is that you are working on changes but then must temporarily checkout a different branch to make a bug fix. So you can stash your changes in your index and working copy, checkout the other branch, make the bug fix, commit, checkout the original branch, and git-stash apply to restore your changes and pick-up where you left off.