Home > front end >  git pop failed with "error: could not restore untracked files from stash"
git pop failed with "error: could not restore untracked files from stash"

Time:09-13

I have done about 2 months work locally against main branch (without committing or staging anything). This included adding new (untracked) files and modifying existing tracked files.

Then I wanted to check something on the main branch without my changes so did:

git stash -all

Expecting it to stash both my tracked and untracked file changes.

I then did

git stash pop

To get my changes back, and got thousands of errors:

.project already exists, no checkout
all/.project already exists, no checkout
all/.settings/org.eclipse.wst.common.component already exists, no checkout
all/target/vault-work/META-INF/MANIFEST.MF already exists, no checkout
all/target/vault-work/META-INF/vault/config.xml already exists, no checkout
all/target/vault-work/META-INF/vault/filter.xml already exists, no checkout
all/target/vault-work/META-INF/vault/properties.xml already exists, no checkout
all/target/vault-work/META-INF/vault/settings.xml already exists, no checkout
core/.project already exists, no checkout
core/target/classes/META-INF/MANIFEST.MF already exists, no checkout
core/target/classes/OSGI-INF/com.xx.aem.core.filters.CorrelationLoggingFilter.xml already exists, no checkout
core/target/classes/OSGI-INF/com.xx.aem.core.filters.LinkHeaderFilter.xml already exists, no checkout
core/target/classes/OSGI-INF/com.xx.aem.core.filters.LoggingFilter.xml already exists, no checkout

and ending in:

ui.content/target/vault-work/META-INF/vault/settings.xml already exists, no checkout
ui.frontend/.project already exists, no checkout
ui.tests/.project already exists, no checkout
error: could not restore untracked files from stash
The stash entry is kept in case you need it again.

Surprisingly, when looking at the state of the local code, it does seem to have created the new files. But none of the changed files are there. So the error says it could not restore untracked files, but it appears to have, but it has not recovered the changes I made to existing files.

Summary:

  1. I had changed files and new (untracked files) from main.
  2. I did "git stash -all"
  3. This got me back to main without my changes.
  4. I then did "git pop", expecting to be back to 1.
  5. got thousands of errors for no apparent reason.
  6. None of my changes to files are there.
  7. My new, untrack files do see to be there, despite it saying it could not restore them.

How do I get back all the changes I did to existing files?

Any help appreciated.

Note: I have recently noticed that git stash --all (which was recommended on a post in order to stash tracked and untracked files) may also (bizarrely) stash ignored files - this could be the crux of the problem.

If I do

git stash show

I get

bob@MacBook-Pro eyas-web % git stash show
 .../aem/core/filters/xx.java |  25   -
 .../models/xxx/response/x.java   |  18 --
 .../xx/aem/core/models/xx/Player.java         |  14   
 .../xx/aem/core/services/xx.java       |  31    
 .../services/impl/xx.java  |   2  -
 .../core/services/impl/xx.java |   6  
 .../aem/core/services/impl/xx.java   | 212                     -
 pom.xml                                            |  18 --
 8 files changed, 282 insertions( ), 44 deletions(-)

I need to figure out how to get the "8 files changed" back from the stash, along side the new untracked files which do seem to have been restored.

One idea might be to try to remove all the ignored files, then do git stash pop again. However, looking at the man pages for "git clean -xdf", it doesn't say if -x also removes the necessary git files as well as the ignored files we added (such as project config files).

Any ideas how to fix this?

I noticed this post: How do you stash an untracked file?

Which says: "git stash -u Warning, doing this will permanently delete your files if you have any directory/ entries in your gitignore file."

This seems very bad, and may be related to my issue.

CodePudding user response:

I then did a build and deployment,

These steps may have created new untracked files in your working tree. And I think those untracked files are blocking git stash pop.

Check working tree status by git status. If you see unwanted untracked files, use git clean to remove them, then git stash pop.

CodePudding user response:

How do I get back all the changes I did to existing files?

I would try git stash branch:

  1. Backup your whole directory (just in case).
  2. git stash branch my-stash-in-a-branch This should bring back everything you've stashed. Even untracked/ignored stuff from git stash --all will be recovered.
  3. Check ignored files carefully. If you merge the new branch to your mainline it will also merge files that would have been ignored in the normal case. This opens a potential leak for secrets you don't want to share. You can just get the .gitignore file from the mainline with something like git checkout master .gitignore to then find all files that should have been ignored with git ls-files -ci --exclude-standard -z. More info here. After that you may want to remove the ignored files from history (another topic).
  4. Now do something like git rebase -i master to get in sync with your mainline.

PS: I cross my fingers for you that your stash is not already empty. Prefer git stash apply over git stash pop in unclear situations.

  • Related