Pergunta

it happened more than once to leave the office having only saved a stash of uncommitted code.

So, if I wanted to keep working on the same feature, I had to SSH to my host, create a disposable branch, commit the stash and push the new branch to the development repository.

Now I wonder if there is a cleaner, more practical solution for "copying/exporting" a git stash to another local repository. I have already ruled out SCP'ing the local repository on my office machine, because I could have some work in progress on my laptop, too.

P.S.: my question looks like the continuation of this one

Foi útil?

Solução

You can use git stash show to make a patch and then git apply it on your home machine.

Outras dicas

The stash is saved as a special type of commit, and so it is available in the git object database. But, the reference to that commit is outside of the namespace that is typically made available for fetching.

You could make the most recent stash available for fetching by running the following on the source repository:

git symbolic-ref refs/heads/exported-stash refs/stash

This will create a branch named exported-stash which can be fetched like any other branch. You can use a different name if you like, but avoid using stash since that will result in some annoying warnings about the name being ambiguous with the actual stash. After fetching from the remote it can be applied on another repository with:

git stash apply origin/exported-stash

(assuming that it was fetched from the remote origin).

After making changes you could even stash those locally and push the updated stash back to the origin with:

git push origin +stash:exported-stash

The + turns this into a forced push, but in this case that is actually safe. The old version of the stash on the origin will just be moved to stash@{1} since the list of stashes is saved as a log of references.

You could save a patch and send that to yourself.

git diff > patchfile

Then you can apply it with...

git apply --stat patchfile

As pointed out by Carl Norum, git can be used to create a patch. However, the command is a bit tricky. You'll need to call

git diff stash@{0}^ stash@{0} > patch

to create a patch (note that your shell may need you to wrap the stash's name in quotation marks). That creates a patch for all differences of your stash's parent and your stash itself.

Then, on your other machine, you can apply the patch with

git apply patch

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top