Once you know the hash of the stash commit you dropped, you can apply it as a stash:
git stash apply $stash_hash
Or, you can create a separate branch for it with
git branch recovered $stash_hash
After that, you can do whatever you want with all the normal tools. When you’re done, just blow the branch away.
Finding the hash
If you have only just popped it and the terminal is still open, you will still have the hash value printed by git stash pop
on screen (thanks, Dolda).
Otherwise, you can find it using this for Linux, Unix or Git Bash for Windows:
git fsck --no-reflog | awk '/dangling commit/ {print $3}'
...or using Powershell for Windows:
git fsck --no-reflog | select-string 'dangling commit' | foreach { $_.ToString().Split(" ")[2] }
This will show you all the commits at the tips of your commit graph which are no longer referenced from any branch or tag – every lost commit, including every stash commit you’ve ever created, will be somewhere in that graph.
The easiest way to find the stash commit you want is probably to pass that list to gitk
:
gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
...or see the answer from emragins if using Powershell for Windows.
This will launch a repository browser showing you every single commit in the repository ever, regardless of whether it is reachable or not.
You can replace gitk
there with something like git log --graph --oneline --decorate
if you prefer a nice graph on the console over a separate GUI app.
To spot stash commits, look for commit messages of this form:
WIP on somebranch: commithash Some old commit message
Note: The commit message will only be in this form (starting with "WIP on") if you did not supply a message when you did git stash
.
Probably the simplest way to achieve this is with git archive
. If you really need just the expanded tree you can do something like this.
git archive master | tar -x -C /somewhere/else
Most of the time that I need to 'export' something from git, I want a compressed archive in any case so I do something like this.
git archive master | bzip2 >source-tree.tar.bz2
ZIP archive:
git archive --format zip --output /full/path/to/zipfile.zip master
git help archive
for more details, it's quite flexible.
Be aware that even though the archive will not contain the .git directory, it will, however, contain other hidden git-specific files like .gitignore, .gitattributes, etc. If you don't want them in the archive, make sure you use the export-ignore attribute in a .gitattributes file and commit this before doing your archive. Read more...
Note: If you are interested in exporting the index, the command is
git checkout-index -a -f --prefix=/destination/path/
(See Greg's answer for more details)
Best Answer
Another difference between a bare and non-bare repository is that a bare repository does not have a default remote origin repository:
From the manual page for
git clone --bare
:Presumably, when it creates a bare repository, Git assumes that the bare repository will serve as the origin repository for several remote users, so it does not create the default remote origin. What this means is that basic
git pull
andgit push
operations won't work since Git assumes that without a workspace, you don't intend to commit any changes to the bare repository: