Created
January 9, 2026 19:51
-
-
Save vzaliva/55a910cacf3e1775bde5249d6dc977a7 to your computer and use it in GitHub Desktop.
git backup via stash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # git-snapshot: Backup changes only if they exist, without clearing workspace | |
| # 1. Set the backup message (default to timestamp) | |
| MESSAGE="${1:-"Snapshot backup $(date '+%Y-%m-%d %H:%M:%S')"}" | |
| # 2. Try to create a stash commit object | |
| # 'create' returns a hash if changes exist; otherwise, it returns nothing. | |
| STASH_HASH=$(git stash create "$MESSAGE") | |
| # 3. Handling: Exit if there are no uncommitted changes | |
| if [ -z "$STASH_HASH" ]; then | |
| echo "No uncommitted changes detected. Stash not created." | |
| exit 0 | |
| fi | |
| # 4. Store the hash into the stash reflog to make it permanent | |
| git stash store -m "$MESSAGE" "$STASH_HASH" | |
| echo "Backup saved: $MESSAGE" | |
| echo "Check your backups with 'git stash list'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment