Last active
November 28, 2023 21:42
-
-
Save Malix-off/d54eb352261402b935322052f9ae2231 to your computer and use it in GitHub Desktop.
Git - StashFlow
This file contains 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 | |
# Define the directory for alias hooks | |
hooks_dir=~/.git/aliasHooks | |
# Create the directory for alias hooks if it doesn't exist | |
mkdir "$hooks_dir" | |
# Create or update the scripts for checkout, switch, and branch | |
cat >"$hooks_dir/checkout.sh" <<'EOF' | |
#!/bin/bash | |
current_branch=$(git branch --show-current) | |
git stash push | |
git checkout "$@" | |
if [ $? -eq 0 ]; then | |
git stash pop | |
git checkout "$current_branch" | |
fi | |
EOF | |
cat >"$hooks_dir/switch.sh" <<'EOF' | |
#!/bin/bash | |
current_branch=$(git branch --show-current) | |
git stash push | |
git switch "$@" | |
if [ $? -eq 0 ]; then | |
git stash pop | |
git switch "$current_branch" | |
fi | |
EOF | |
cat >"$hooks_dir/branch.sh" <<'EOF' | |
#!/bin/bash | |
current_branch=$(git branch --show-current) | |
git stash push | |
git branch "$@" | |
if [ $? -eq 0 ]; then | |
git stash pop | |
git switch "$current_branch" | |
fi | |
EOF | |
# Make the scripts executable | |
chmod +x "$hooks_dir/checkout.sh" | |
chmod +x "$hooks_dir/switch.sh" | |
chmod +x "$hooks_dir/branch.sh" | |
# Set up Git aliases calling the original command | |
git config --global alias.checkout '!f() { \ | |
~/.git/aliasHooks/checkout.sh "$@"; \ | |
git checkout "$@"; \ | |
}; f' | |
git config --global alias.switch '!f() { \ | |
~/.git/aliasHooks/switch.sh "$@"; \ | |
git switch "$@"; \ | |
}; f' | |
git config --global alias.branch '!f() { \ | |
~/.git/aliasHooks/branch.sh "$@"; \ | |
git branch "$@"; \ | |
}; f' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment