-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-co-remote-branch.sh
executable file
·65 lines (59 loc) · 2.11 KB
/
git-co-remote-branch.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
62
63
64
#!/bin/sh
########################################################################
# git-co-remote-branch.sh: Git Remote Branch Checkout Script
#
# Description:
# This script simplifies the process of checking out a remote branch in Git.
# It automatically creates a new local branch that tracks the specified remote branch.
# This is useful for quickly switching to and working on branches that exist
# on the remote repository but not yet locally. It checks for Git installation
# before proceeding.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.2 2024-01-07
# Updated command existence and execution permission checks
# using a common function for enhanced reliability and maintainability.
# v1.1 2023-12-07
# Added check for Git installation.
# v1.0 2016-01-26
# Initial release. Implements functionality to checkout a remote branch in Git,
# creating a corresponding local branch that tracks it.
#
# Usage:
# Run the script with the name of the remote branch:
# ./git-co-remote-branch.sh <branch>
#
# Example:
# ./git-co-remote-branch.sh feature-branch
# This will create and switch to a local branch called 'feature-branch'
# that tracks 'origin/feature-branch'.
#
########################################################################
check_commands() {
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Git is not installed. This script requires Git to checkout remote branches. Please install Git and try again."
exit 127
elif ! [ -x "$(command -v "$cmd")" ]; then
echo "Error: Command '$cmd' is not executable. Please check the permissions."
exit 126
fi
done
}
# Check if Git is installed
check_commands git
main() {
if [ -n "$1" ]; then
check_git_installed
git checkout -b $1 origin/$1
else
echo "usage: git-co-remote-branch <branch>"
exit 1
fi
}
main $*