-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_push.sh
More file actions
51 lines (41 loc) · 1.35 KB
/
git_push.sh
File metadata and controls
51 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
# Usage: ./git_push.sh "your commit message"
# Stages all changes (including local deletions), commits, and pushes.
# Files deleted locally are automatically removed from the remote branch on push.
set -euo pipefail
if [ $# -eq 0 ]; then
echo "Error: please provide a commit message."
echo "Usage: $0 \"your commit message\""
exit 1
fi
COMMIT_MSG="$1"
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
REMOTE="origin"
# Stage everything: new files, modifications, and deletions
git add -A
# Check if there is anything to commit
if git diff --cached --quiet; then
echo "Nothing to commit on branch '$BRANCH'."
exit 0
fi
# Show what will be committed
echo "=== Changes to be committed ==="
git diff --cached --stat
echo ""
# Highlight any deletions explicitly
DELETED="$(git diff --cached --name-only --diff-filter=D)"
if [ -n "$DELETED" ]; then
echo "=== Files deleted locally (will be removed from remote) ==="
echo "$DELETED"
echo ""
fi
git commit -m "$COMMIT_MSG"
# Push; set upstream automatically if this branch has no remote tracking yet
if git rev-parse --abbrev-ref --symbolic-full-name "@{u}" &>/dev/null; then
git push "$REMOTE" "$BRANCH"
else
echo "No upstream set for '$BRANCH'; pushing and setting upstream..."
git push -u "$REMOTE" "$BRANCH"
fi
echo ""
echo "Done. Branch '$BRANCH' pushed to $REMOTE."