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.
Checkout old commitsSince this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.
When the <tree-ish> is given, overwrite both the index and the working tree with the contents at the <tree-ish> . The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out.
No - it only changes the repository, tags snd remote heads - it never changes the working tree, local branches or the index. git pull can change local branches, the local tree and the index. It won't overwrite charges but may do a merge, a rebase or fail if there are conflicting changes.
If the version control system doesn't allow you to check out a branch for whatever reason, you can add the -f or — force flag to tell Git to force checkout and switch branches. That's possible even when you still have some unstaged changes, which means that the working tree index differs from HEAD.
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.
Besides 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 -m
/ --merge
option. 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 -f
option. Warning: uncomitted changes will be lost!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With