-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-clone-worktrees.sh
48 lines (40 loc) · 1.04 KB
/
git-clone-worktrees.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
#!/usr/bin/env bash
set -e
# Examples of call:
# git-clone-bare-for-worktrees [email protected]:name/repo.git
# => Clones to a /repo directory
#
# git-clone-bare-for-worktrees [email protected]:name/repo.git my-repo
# => Clones to a /my-repo directory
url=$1
basename=${url##*/}
name=${2:-${basename%.*}}
if [ -z "$url" ]; then
echo "Usage: git-clone-bare-for-worktrees.sh <url> [<name>]"
exit 1
fi
if [ -d "$name" ]; then
echo "Directory $name already exists"
exit 1
fi
if [ -z "$name" ]; then
echo "Name is required"
exit 1
fi
mkdir $name
cd "$name"
# Moves all the administrative git files (a.k.a $GIT_DIR) under .bare directory.
#
# Plan is to create worktrees as siblings of this directory.
# Example targeted structure:
# .bare
# main
# new-awesome-feature
# hotfix-bug-12
# ...
git clone --bare "$url" .bare
echo "gitdir: ./.bare" > .git
# Explicitly sets the remote origin fetch so we can fetch remote branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# Gets all branches from origin
git fetch origin