-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-new
More file actions
executable file
·67 lines (46 loc) · 1.42 KB
/
git-new
File metadata and controls
executable file
·67 lines (46 loc) · 1.42 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
#!/usr/bin/env bash
source "utils.sh"
branch=$1
git fetch -p
# The branch we're coming off
cb=$(current_branch)
if [[ "${cb}" == "${branch}" ]]; then
writeln "<red>Error</red> Already checked out on branch <yellow>${branch}</yellow>"
exit 1
fi
if [[ "${branch}" == "-" ]]; then
previous_branch=$(previous_branch)
writeln "Switching back to branch <magenta>${previous_branch}</magenta>"
git checkout --quiet -
git pull -p
exit 0
fi
# Does the branch already exist? If yes, switch to it
if git show-ref --quiet "refs/heads/${branch}"; then
writeln "Checking out branch <magenta>${branch}</magenta>"
git checkout --quiet "${branch}"
git pull -p
exit 0
fi
# Does the branch exist in remote? If yes, switch to it
if git show-branch "remotes/origin/${branch}" >/dev/null 2>&1; then
writeln "Checking out branch <magenta>${branch}</magenta>"
git checkout --quiet "${branch}"
git pull -p
exit 0
fi
# The branch doesn't exist so create it if wanted
write "Branch <yellow>${branch}</yellow> does not exist. Do you want to create it? [y/n] "
read -r answer
if [[ "${answer}" != "y" ]]; then
echo "Aborted."
exit 0
fi
writeln "Creating branch <green>${branch}</green>"
git checkout --quiet -b "${branch}"
# Write to .githistory
if [[ "${GIT_INFO_FILE}" != "" ]]; then
repo=$(repo)
# touch "${GIT_INFO_FILE}"
echo "${repo}:${branch}:${cb}" >> "${GIT_INFO_FILE}"
fi