How to Move a Git Stash from One Directory to Another Repository (Same Remote Project)
Git stashes are a lifesaver for temporarily saving uncommitted changes without cluttering your commit history. But what if you’re working on the same project in two different local directories (e.g., a work laptop and a personal desktop, or separate clones for testing) and need to move a stash from one directory to the other?
Git doesn’t natively support transferring stashes between repositories, even if they share the same remote. However, with a few clever workarounds, you can easily move stashes across directories. In this guide, we’ll explore two reliable methods to achieve this: the Patch Method (simple, ideal for small stashes) and the Temporary Branch Method (more robust for complex changes or potential conflicts).
Table of Contents#
- Prerequisites
- Understanding Git Stashes
- Method 1: Using a Patch File
- Method 2: Using a Temporary Branch (More Reliable)
- Troubleshooting Common Issues
- Verification Steps
- Conclusion
- References
Prerequisites#
Before starting, ensure you have:
- Git installed on both systems (check with
git --version). - Two local directories (repositories) cloned from the same remote Git project (verify with
git remote -vin both directories to confirm the remote URL matches). - Access to the remote repository (to push/pull branches, if using the Temporary Branch Method).
- Basic familiarity with Git commands (e.g.,
git stash,git branch,git push).
Understanding Git Stashes#
A Git stash is a temporary storage area for uncommitted changes (staged or unstaged). Stashes are stored locally in .git/refs/stash and are not synced with the remote by default. To move a stash between directories, we need to “export” it from the source and “import” it into the target.
Method 1: Using a Patch File#
The Patch Method converts the stash into a plain-text patch file, which you can manually transfer and apply to the target directory. This works best for small, simple stashes with minimal conflicts.
Step 1: Identify the Stash in the Source Directory#
First, navigate to the source repository (where the stash currently lives) and list all stashes to find the one you want to move:
# Navigate to the source directory
cd /path/to/source-repo
# List all stashes (most recent first)
git stash listOutput example:
stash@{0}: WIP on main: 1a2b3c4 Fix login bug
stash@{1}: WIP on feature-x: 5d6e7f8 Add new button
Note the stash index (e.g., stash@{1}) for the stash you want to move.
Step 2: Export the Stash as a Patch#
Generate a patch file containing the stash’s changes. Use git stash show -p to output the stash as a patch and redirect it to a file:
# Replace "stash@{1}" with your target stash index
git stash show -p stash@{1} > my-stash.patch-p(patch) flag: Shows the stash as a diff.my-stash.patch: The output file (name it something descriptive).
Step 3: Move the Patch to the Target Directory#
Transfer the my-stash.patch file to the target repository’s directory. Use:
- Local transfer:
cp my-stash.patch /path/to/target-repo/(if on the same machine). - Cloud/USB: Upload to Google Drive, email, or transfer via USB (if on separate machines).
Step 4: Apply the Patch in the Target Directory#
Navigate to the target repository and apply the patch:
# Navigate to the target directory
cd /path/to/target-repo
# Ensure you’re on the correct branch (e.g., main)
git checkout main
# Fetch the latest from the remote to align histories (optional but recommended)
git fetch origin
# Apply the patch
git apply my-stash.patchIf the patch fails to apply:#
If you see errors like error: patch failed, use the --reject flag to create .rej files showing conflicts:
git apply --reject my-stash.patchEdit the .rej files to resolve conflicts, then run git apply again.
Step 5: Verify and Save Changes#
After applying the patch:
- Check changes with
git statusorgit diff. - (Optional) Save the changes as a new stash in the target repo:
git stash push -m "Moved from source repo: Add new button" - Or commit the changes directly:
git add . git commit -m "Moved stash: Add new button"
Method 2: Using a Temporary Branch (More Reliable)#
For larger stashes or when the patch method fails (e.g., due to conflicting commit histories), use a temporary branch. This method pushes the stash to the remote as a branch, then pulls it into the target directory.
Step 1: Create a Temporary Branch from the Stash (Source)#
In the source repository, create a temporary branch from the stash to package its changes:
# Navigate to the source directory
cd /path/to/source-repo
# Replace "stash@{1}" with your stash index; name the branch "temp-stash-branch"
git stash branch temp-stash-branch stash@{1}This creates a new branch (temp-stash-branch) and applies the stash’s changes to it.
Step 2: Push the Temporary Branch to the Remote#
Push the temporary branch to the shared remote so the target repository can access it:
git push -u origin temp-stash-branch-u origin: Sets the remote as the upstream for the branch.
Step 3: Fetch and Checkout the Temporary Branch (Target)#
In the target repository, fetch the temporary branch from the remote and check it out:
# Navigate to the target directory
cd /path/to/target-repo
# Fetch the latest branches from the remote
git fetch origin
# Checkout the temporary branch
git checkout temp-stash-branchStep 4: Integrate Changes into the Target Branch#
Now, merge or cherry-pick the temporary branch’s changes into your desired target branch (e.g., main):
Option A: Merge (simpler, but adds a merge commit):#
# Checkout your target branch (e.g., main)
git checkout main
# Merge the temporary branch
git merge temp-stash-branchOption B: Cherry-Pick (cleaner, for single commits):#
If the temporary branch has only one commit (the stash), cherry-pick it:
# Get the commit hash of the temporary branch (e.g., a1b2c3d)
git log temp-stash-branch -n 1 --format="%H"
# Checkout target branch and cherry-pick the commit
git checkout main
git cherry-pick a1b2c3dStep 5: Clean Up the Temporary Branch#
Delete the temporary branch locally and remotely to avoid clutter:
# In target repo: Delete local temporary branch
git branch -d temp-stash-branch
# Delete remote temporary branch (requires push permissions)
git push origin --delete temp-stash-branchIn the source repo, you can also delete the local temporary branch:
cd /path/to/source-repo
git branch -d temp-stash-branchTroubleshooting Common Issues#
Patch Fails to Apply#
- Cause: The source and target repos have diverged (e.g., different commit histories).
- Fix: Fetch the latest from the remote in both repos first:
# In both source and target repos git checkout main git pull origin main
Stash Contains Binary Files#
- Issue: Patch files don’t handle binary files well.
- Fix: Use the Temporary Branch Method instead (binary files are preserved in Git branches).
Remote Repository Mismatch#
- Issue:
git remote -vshows different remotes in source/target. - Fix: Re-clone the target repo from the correct remote, or update the remote URL:
git remote set-url origin https://github.com/your-username/your-repo.git
Verification Steps#
To confirm the stash was moved successfully:
- Check changes: In the target repo, run
git difforgit showto verify the stash’s changes are present. - Compare with source: If possible, run
git diffin both repos to ensure the changes match. - Test functionality: Build/run the project to confirm the moved changes work as expected.
Conclusion#
Moving a Git stash between directories linked to the same remote is straightforward with either the Patch Method (simple, text-based) or the Temporary Branch Method (robust, conflict-resistant). Use the Patch Method for small stashes and the Temporary Branch Method for larger changes or when conflicts are likely.
Remember: Stashes are local, so always back up critical stashes (e.g., git stash save "Backup before moving") before transferring them!