6

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
onknows
  • 6,151
  • 12
  • 65
  • 109
  • What is the output of `git status`? What was the exact `git reset` command that you used? – Code-Apprentice Jul 20 '18 at 16:31
  • 1
    @Code-Apprentice - onknows already included the output of `git status` in the question. – JDB Jul 20 '18 at 16:34
  • @JDB OIC...I missed it because I was expecting more... – Code-Apprentice Jul 20 '18 at 16:36
  • Try `git reset --hard HEAD`. Although, if JDB is correct that your files are in .gitignore, then it probably still won't work. – Code-Apprentice Jul 20 '18 at 16:37
  • Also... can't tell for sure, but it looks like *maybe* your `.gitignore` is ignoring itself? That's probably not a good idea. `.gitignore` is meant to be included in the repo and shared with other engineers: https://stackoverflow.com/a/10177000/211627 – JDB Jul 20 '18 at 16:44
  • 1
    Your `git status` output says `On branch DIC-98-rhel-lb0` (note the name). Your `git push` command says to push based on the name `mybranch` (note the *different* name). – torek Jul 20 '18 at 18:04
  • @torek - How would that be possible other than the OP didn't do a thorough job scrubbing the code example? – JDB Jul 20 '18 at 18:11
  • @JDB: That is not entirely clear to me, but it makes me suspicious that something else went wrong during the write-up of the question... – torek Jul 20 '18 at 18:12

1 Answers1

4

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

JDB
  • 25,172
  • 5
  • 72
  • 123