-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitCreateBranchPushUpstream
executable file
·37 lines (30 loc) · 1.42 KB
/
gitCreateBranchPushUpstream
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
#!/bin/bash
#############################################
# Creates a new branch from develop and #
# pushes it to origin using the script #
# gitPushSetUpstream #
#############################################
source ~/bash-helpers/_colours
currentBranchName=$(git branch | grep \* | cut -d ' ' -f2)
allowedBranches=("main" "develop")
# If the current directory is not develop or main, then we cannot create a new branch
if ! printf '%s\0' "${allowedBranches[@]}" | grep -qwz "$currentBranchName"; then
echo "${RED}Error: You are not on the main or develop branch. You must be on the main or develop branch to create new branches."
echo "You are on branch: $currentBranchName${RESTORE}"
exit 1
fi
# The new branch name we are creating must be passed as variable 1, ensure a name is passed
branchName=$1
if [ -z "$branchName" ]; then
echo "${RED}Error: You must pass a branch name as the first parameter to this script."
exit 1
fi
echo "Creating new branch: ${LMAGENTA}${branchName}${RESTORE}"
git branch "$branchName" && git checkout "$branchName"
# Branch created, use the gitPushSetUpstream script to push the branch to origin as it asks the user to confirm the name
if ! gitPushSetUpstream; then
# If they say no to pushing the branch, delete the local branch automatically
echo "${RED}Incorrect branch name. Deleting local git branch...${RESTORE}"
sleep 1
gitDeleteBranchLocal "$branchName"
fi