-
Notifications
You must be signed in to change notification settings - Fork 32
/
reset_to_origin.sh
executable file
·61 lines (48 loc) · 1.49 KB
/
reset_to_origin.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
#!/usr/bin/env bash
# Script Name: reset_to_origin.sh
# Description: Resets the local repository to match the remote repository.
# Usage: reset_to_origin.sh branch_name repo_path
# branch_name - the name of the branch to reset.
# repo_path - the path to the repository.
# Example: ./reset_to_origin.sh master .
validate_arguments() {
if [ $# -eq 0 ]; then
echo "You have to specify the branch name!"
exit 1
fi
if [ $# -eq 1 ] || [ $# -eq 2 ]; then
local branch_name="$1"
local is_remote_branch
local is_local_branch
is_remote_branch=$(git branch -r | grep -Fw "$branch_name" > /dev/null)
is_local_branch=$(git branch -l | grep -Fw "$branch_name" > /dev/null)
if [ "$is_remote_branch" -ne 0 ] && [ "$is_local_branch" -ne 0 ]; then
echo "The specified branch doesn't exist."
exit 1
fi
if [ $# -eq 2 ]; then
local working_dir="$2"
if [ ! -d "$working_dir" ]; then
echo "$working_dir is not a directory."
exit 1
fi
fi
else
echo "You can't specify more than 2 parameters!"
exit 1
fi
}
reset_to_origin() {
local branch_name="$1"
local working_dir="${2:-.}"
cd "$working_dir" || exit
git fetch origin
git checkout "$branch_name"
git reset --hard origin/"$branch_name"
git clean -fdx
}
main() {
validate_arguments "$@"
reset_to_origin "$@"
}
main "$@"