22

I have a local stash on a directory.

For some reasons, I had to re-import the same remote project into another directory.

Is there a way to move my stash from my old directory to the new created one since they follow the same remote project and branches ?

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Magnas
  • 869
  • 1
  • 5
  • 18

4 Answers4

14

The steps mentioned here were helpful for me when I faced this same situation which the OP faced. Copy-pasting the steps below, in case the link doesn't work -

In your old repository/directory -

git stash show -p > patchfile

If you have a specific stash you wish to migrate -

git stash show -p stash@{5} > patchfile

In your new repository/directory (make sure you're in the correct directory, or you might face errors while applying the patch) -

git apply /old/project/dir/patchfile
git stash
Dhruv Saraswat
  • 858
  • 9
  • 13
9

You should be able to copy the .git directory from the old repo over the new one. Git stores every object in files within that directory so that should return your stash.

BookOfGreg
  • 3,550
  • 2
  • 42
  • 56
  • 2
    it might be a way to fix this issue, but I would suggest try the other way in comment by @Chris Maes – Mia Feb 02 '16 at 08:53
  • 1
    Oh, yes, it seems to be a cleaner way to fix the issue – Magnas Feb 02 '16 at 10:11
  • 1
    This answer is excessive. Usually one clones a new instance of repo because of problems/inconsistencies in the old and doesn't want to drag `.git` on for this very reason (but only the stash for example). – bloody Sep 21 '22 at 10:28
  • @Magnas would be cool if you can change the accepted answer to not confuse future travellers. – Tejas Kale Aug 25 '23 at 11:48
8

You can use git stash branch to create a branch from your stash:

$ git stash branch <branchname> [<stash>]

This command performs the following:

  • Creates a new branch with <branchname>
  • Switches you to the new branch
  • Applies the specified stash (or the latest stash if omitted)
  • Stages all stashed changes for commit

After you commit and push the changes on this branch, you can then fetch or pull from it.

Derek Lee
  • 3,452
  • 3
  • 30
  • 39
haggai_e
  • 4,689
  • 1
  • 24
  • 37
0

If stash is hard for you, you should push your change to the remote branch and then check out it from the new repository, everything still keeps the same

Hunter 19
  • 19
  • 1