Skip to content

Commit f1422b5

Browse files
committed
Initial release
0 parents  commit f1422b5

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Yankee Maharjan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Git worktree switcher :octocat:
2+
Switch between git worktrees with ease. :zap:
3+
4+
<img src = "https://i.imgur.com/RYuBJnE.gif" width="700" alt="demo of switching between git worktrees" />
5+
6+
## Installation
7+
8+
Make the script executable.
9+
10+
```bash
11+
$ chmod +x wt
12+
```
13+
14+
Copy the binary to any directory in your `$PATH`
15+
16+
```bash
17+
$ sudo cp wt /usr/local/bin
18+
```
19+
20+
## Usage
21+
Switch between worktrees.
22+
You can do a text search to change to the worktree directory.
23+
24+
```bash
25+
$ wt <worktree-name/search-term>: search for worktree names and change directory.
26+
```
27+
28+
> <details><summary><strong>Demo</strong></summary>
29+
> <img src = "https://i.imgur.com/RYuBJnE.gif" width="700" alt="demo of switching between git worktrees" />
30+
31+
</details>
32+
33+
List out all the worktrees.
34+
35+
```bash
36+
wt list: list out all the git worktrees.
37+
```
38+
39+
```bash
40+
wt help: show this help message.
41+
```

wt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# Switch between git worktrees with ease.
4+
5+
args=("$@")
6+
7+
# show worktree list
8+
worktree_list() {
9+
git worktree list
10+
}
11+
12+
help_message(){
13+
echo -e "wt lets you quickly switch between your git worktrees.\n"
14+
echo "Usage:"
15+
echo -e "\twt list: list out all the git worktrees."
16+
echo -e "\twt help: shows this help message."
17+
echo -e "\twt <worktree-name/search-term>: search for worktree names and change to that directory."
18+
}
19+
20+
# If the first argument is "list", show worktree list,
21+
# if it is "help", then show the help message,
22+
# else get worktree path based on user input.
23+
if [ -z ${args[0]} ]; then
24+
help_message
25+
elif [ ${args[0]} == "list" ]; then
26+
worktree_list
27+
elif [ ${args[0]} == "help" ]; then
28+
help_message
29+
else
30+
directory=$(git worktree list | awk '/'"${args[0]}"'/ {print $1}')
31+
fi
32+
33+
# Change worktree based on user argument.
34+
change_worktree() {
35+
echo Changing to worktree at: $directory
36+
cd $directory
37+
exec $(echo $SHELL)
38+
}
39+
40+
# If directory variable is not empty then change worktree
41+
if [ -z $directory ]
42+
then
43+
:
44+
else
45+
change_worktree
46+
fi

0 commit comments

Comments
 (0)