Created
November 28, 2025 20:33
-
-
Save alexpyattaev/202bc65b62dd5115a53849f382fc21ee to your computer and use it in GitHub Desktop.
Updates master of your local clone of an upstream repo.
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
| #!/usr/bin/fish | |
| # Update this to what you need | |
| set repos /home/$USER/repo1 /home/$USER/repo2 | |
| for repo in $repos | |
| cd $repo | |
| git fetch upstream | |
| # Get upstream's default branch name | |
| # Get all lines from `git remote show upstream` | |
| set lines (git remote show upstream) | |
| # Look for the line that starts with ' HEAD branch:' | |
| for line in $lines | |
| if string match -r '^\s*HEAD branch:' $line > /dev/null | |
| set default_branch (string split ': ' -- $line)[2] | |
| break | |
| end | |
| end | |
| if test -z "$default_branch" | |
| echo "Failed to determine upstream default branch in $repo" | |
| continue | |
| end | |
| echo "Updating $default_branch in $repo" | |
| set current_branch (git symbolic-ref --quiet --short HEAD) | |
| if test "$current_branch" = "$default_branch" | |
| if git diff --quiet --exit-code | |
| echo "$default_branch checked out and clean, force-updating" | |
| git reset --hard upstream/$default_branch; or continue | |
| else | |
| echo "Save local changes to stash to be safe" | |
| git stash push -u -m "Auto-stash before syncing with upstream"; or continue | |
| echo "Move branch pointer to upstream branch" | |
| git reset --hard upstream/$default_branch; or begin | |
| echo "Reset failure, try to pop stash back to avoid data loss" | |
| git stash pop; or echo "WARNING: Failed to pop stash" >&2 | |
| send_error_to_wall "Repo $repo left in broken state, please fix manually!" | |
| exit 1 | |
| end | |
| echo "Restore local changes" | |
| git stash pop; or echo "No local changes to restore" | |
| end | |
| else | |
| git branch -f $default_branch upstream/$default_branch; or continue | |
| end | |
| echo "updating origin" | |
| git push origin $default_branch | |
| end | |
| function send_error_to_wall | |
| if test (count $argv) -gt 0 | |
| # If arguments provided, join them as message | |
| set message $argv | |
| else | |
| # Otherwise read message from stdin | |
| set message (cat) | |
| end | |
| if test -z "$message" | |
| echo "No message provided to send_error_to_wall" >&2 | |
| return 1 | |
| end | |
| echo "=== CRON JOB ERROR ===" | |
| echo $message | wall | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment