Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout fails with because local changes ... without local changes?

Using Git 2.7.4 I made some changes to a branch. Commit and push to the branch.

[user:~/terraform] mybranch ± git status
On branch DIC-98-rhel-lb0
nothing to commit, working directory clean
[user:~/terraform] mybranch ±


[user:~/terraform] mybranch ± git push origin mybranch
Everything up-to-date

When I try to switch to master

[user:~/terraform] mybranch ± git checkout master
error: Your local changes to the following files would be overwritten by checkout:
    azure-openshift/.gitignore
    azure-openshift/bastion.tf
    azure-openshift/bootstrap.sh
    azure-openshift/bootstrap.tfvars
    azure-openshift/cns.tf
    azure-openshift/infra.tf
    azure-openshift/master.tf
    azure-openshift/openshift.auto.tfvars
    azure-openshift/openshift.variables.tf
    azure-openshift/revproxy.tf
Please, commit your changes or stash them before you can switch branches.
Aborting
[user:~/terraform] mybranch 1 ± 

I can also do git reset first, with the same result.

What am I missing?

like image 460
onknows Avatar asked Jul 20 '18 16:07

onknows


People also ask

How do you fix your local changes to the following files would be overwritten by checkout?

The Git “Your local changes to the following files would be overwritten by checkout” error occurs when you make changes on two branches without committing or stashing those changes and try to navigate between the branches. You can fix this issue by either stashing your changes for later or adding them to a commit.

Does git checkout lose local changes?

Force a Checkout Basically, it can be used to throw away local changes.

How do you bypass your local changes to the following files would be overwritten by merge?

The “Your local changes to the following files would be overwritten by merge” error occurs when you try to pull a remote repository to your local machine whose contents conflict with the contents of your local version of the repository. To fix this error, either stash your changes away for later or commit your changes.


1 Answers

Most likely those files are ignored in your current branch (mybranch), but they are part of the master branch (perhaps they were accidentally committed at some point?). So while your current branch is clean, checking out master would overwrite those files and risk losing that data.

If you know that it's safe to overwrite those files, then you can tell git that you're quite sure you want to check out master with the -f flag:

git checkout -f master

If you're not sure, then you can try rebasing or merging master into your issue branch and working through any conflicts, etc.


For example, let's say you accidentally included a file named foo.temp, which is a log file generated when you run builds, in your master branch.

You then create a new branch, remove_foo, and add foo.temp to your .gitignore. You build (thus modifying foo.temp), commit and push your changes to the server.

Everything looks good. git status returns clean.

You then attempt to checkout master and get an error: foo.temp will be overwritten with the version in master. Git is trying to protect your work.

Fortunately, you know that this is a temporary issue, so you use git checkout -f master because you know that it's okay if foo.temp is overwritten.

Eventually, remove_foo will be peer-reviewed and merged into master and this little annoyance will go away.


If you attempted to remove some files which were accidentally included in the repo, then you may find this helpful:

Remove a file from a Git repository without deleting it from the local filesystem

like image 117
JDB Avatar answered Oct 09 '22 15:10

JDB