To git checkout without overwriting data
How can you
git-checkout without overwriting the data?
- Should keep merge in the commit history?
- Git: best way to remove all changes from a given file for one branch
- how to find git merge commits which merges into current branch
- Access different branches during a git merge conflict
- How to merge branches with different case on windows in GIT?
- git merge -Xignore-space-change by default
git checkout master
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
I have always run after the command separately
git merge new-feature.
This seems to be apparently unnecessary if Git merges at checkout.
2 Solutions collect form web for “To git checkout without overwriting data”
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.
Git does a 2-way merge of uncomitted changes when switching branches (using
git checkout <branch>), but ordinarily it does only trivial (tree-level) merge.
git-stash solution by Karl Voigtland, you can give additional options to git checkout, choosing one of the following options:
Tell git to try harder to merge uncomitted changes into branch you switch to with
--mergeoption. With this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.
Tell git to overwrite uncomitted changes, throwing away local changes with
-foption. Warning: uncomitted changes will be lost!