-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_branch.sh
More file actions
85 lines (70 loc) · 2.81 KB
/
create_branch.sh
File metadata and controls
85 lines (70 loc) · 2.81 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# --- Configuration ---
# Set the default remote name (usually 'origin')
REMOTE_NAME="origin"
# Set to 'true' if you want to force delete (-D) unmerged stale branches.
# Set to 'false' to use safe delete (-d) which requires branches to be merged.
FORCE_DELETE_STALE=false
# ---------------------
# Check if a branch name was provided as an argument
if [ -z "$1" ]; then
echo "Error: No branch name specified."
echo "Usage: $0 <new-branch-name>"
exit 1
fi
NEW_BRANCH_NAME="$1"
# --- Pruning Section ---
echo "--- Pruning stale branches ---"
# 1. Update from remote and prune remote-tracking branches that no longer exist on the remote
echo "Fetching updates from '$REMOTE_NAME' and pruning remote-tracking refs..."
git fetch --prune "$REMOTE_NAME"
echo "Fetch and prune complete."
echo
# 2. Identify and delete local branches whose upstream is gone
echo "Checking for local branches tracking deleted remote branches..."
# Get list of local branches marked as 'gone' relative to the specified remote
# Use awk to correctly extract the branch name, handling the '*' for the current branch
GONE_BRANCHES=$(git branch -vv | grep "\[$REMOTE_NAME/.*: gone\]" | awk '/^\*/ {print $2} ! /^\*/ {print $1}')
if [ -z "$GONE_BRANCHES" ]; then
echo "No stale local branches found to delete."
else
echo "Found stale local branches:"
echo "$GONE_BRANCHES"
echo
DELETE_CMD="git branch -d"
if [ "$FORCE_DELETE_STALE" = true ]; then
echo "Attempting to force delete (-D) stale local branches..."
DELETE_CMD="git branch -D"
else
echo "Attempting to safely delete (-d) stale local branches (will skip unmerged branches)..."
fi
# Loop through and delete each branch, handling potential errors
echo "$GONE_BRANCHES" | while IFS= read -r branch; do
# Check if the branch to be deleted is the current branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" = "$CURRENT_BRANCH" ]; then
echo "Skipping deletion of '$branch' because it is the current branch."
continue
fi
echo "Deleting local branch '$branch'..."
# Use the chosen delete command (-d or -D)
$DELETE_CMD "$branch"
done
echo "Stale branch cleanup finished."
fi
echo "--- Pruning complete ---"
echo
# --- Branch Creation Section ---
echo "Creating and checking out new branch: '$NEW_BRANCH_NAME'..."
git checkout -b "$NEW_BRANCH_NAME"
# Check if checkout was successful (it might fail if the branch already exists locally)
if [ $? -ne 0 ]; then
echo "Error: Failed to create or checkout branch '$NEW_BRANCH_NAME'."
echo "It might already exist locally."
exit 1
fi
echo ""
echo "Successfully created and switched to branch '$NEW_BRANCH_NAME'."
# Optional: Suggest pushing and setting upstream
# echo "To push and set the upstream: git push -u $REMOTE_NAME $NEW_BRANCH_NAME"
exit 0