-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_to_both_remotes.sh
executable file
·61 lines (47 loc) · 1.44 KB
/
push_to_both_remotes.sh
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
#!/bin/bash
# Check if a commit message is supplied
if [ -z "$1" ]; then
echo "Error: Commit message is empty. Usage: ./push_to_both_remotes.sh [commit_message]"
exit 1
fi
# Check if in a Git repository
if [ ! -d .git ]; then
echo "Error: This directory has not been initialized with Git."
exit 1
fi
# Determine the current branch
current_branch=$(git symbolic-ref --short HEAD 2>/dev/null)
if [ -z "$current_branch" ]; then
echo "Error: Not currently on any branch."
exit 1
fi
# Add all changes to the staging area
git add .
# Check if there are staged changes
staged_changes=$(git diff --name-only --cached)
if [ -z "$staged_changes" ]; then
echo "Error: No changes staged for commit."
exit 1
fi
# Initialize second remote if it doesn't exist
if ! git remote get-url "second-remote" > /dev/null 2>&1; then
echo "Initializing second remote."
read -p "Enter the URL for the second remote repository: " second_remote_url
git remote add second-remote $second_remote_url
fi
# Commit changes using the provided message
git commit -m "$1"
# Push changes to 'origin'
git push origin $current_branch
if [ $? -ne 0 ]; then
echo "Error: Failed to push to origin."
exit 1
fi
# Push changes to 'second-remote'
git push second-remote $current_branch
if [ $? -ne 0 ]; then
echo "Error: Failed to push to second-remote."
exit 1
fi
# Print message indicating that the push operation is complete
echo "Pushed to both repositories successfully."