Last active
December 14, 2025 10:59
-
-
Save davestewart/57e92fc81c21097ada8dbb5a91b90603 to your computer and use it in GitHub Desktop.
GitHub update and force-push only changed files from previous commit (add to your bash profile and run as `gup`)
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
| # Git Update-and-Push (gup) | |
| # Amends the last commit with any changes to files included in that commit and force-pushes the updated commit. | |
| # Usage: | |
| # gitup # Amend last commit with changes to files in that commit and force-push | |
| # gitup -s # Show status of files in last commit without amending; 🔥 indicates changes | |
| gup() { | |
| # Check for flags | |
| local status_only=false | |
| if [[ "$1" == "-s" || "$1" == "--status" ]]; then | |
| status_only=true | |
| fi | |
| # Show status and exit if requested | |
| if [[ "$status_only" == true ]]; then | |
| echo | |
| git diff-tree --no-commit-id --name-only -r HEAD | while read -r file; do | |
| if git diff --name-only | grep -q "^${file}$"; then | |
| echo "🔥 $file" | |
| else | |
| echo " $file" | |
| fi | |
| done | |
| echo | |
| return 0 | |
| fi | |
| # Get files from last commit and stage them if modified | |
| git diff-tree --no-commit-id --name-only -r HEAD | xargs -r git add -- | |
| # Skip if nothing was staged | |
| if git diff --cached --quiet; then | |
| echo "No changes to amend from last commit" | |
| return 1 | |
| fi | |
| # Show what will be amended | |
| echo | |
| echo "Files to amend:" | |
| git diff --cached --name-only | |
| echo | |
| # Amend and force push with lease | |
| git commit --amend --no-edit && git push --force-with-lease | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment