Screen.Recording.2023-12-21.at.4.03.30.PM.mov
Anyone else intimidated by shell scripting? I understand the feeling. To overcome my (irrational) fear of shell scripting I created this little script that allows the user to select a git branch to checkout from a list in their terminal using up and down arrow keys.
First, the user selects the location of the branch they want to checkout from a list of local and remote repositories. Then they can see a list of branches in the selected repo, select the branch they're interested in, go back, or create a new branch locally.
This project challenged me to work in a new syntax, expand my knowledge of the Git CLI, learn to add custom scripts to a bash/zsh profile and experiment with ASCII art.
Big thank you to Alexander Klimetschek for his wizardry creating the script that allows the arrow key functionality. I initially set out to write this myself and quickly learned it is quite a challenging feature to implement in the shell.
Check out Alexander's work here: https://unix.stackexchange.com/a/415155
- Clone this repo into a secure location on your machine.
- ex:
~/.local
- ex:
- Add the path to the
swib
file to yourPATH
in.zshrc
or.bashrc
, depending on which shell you use.-
ex:
SWIB=~/.local/switch-branch PATH=$SWIB:$PATH
-
- From any git repository on your machine enter the command:
swib
-
Use the up and down arrow keys to select the repo containing the branch you would like to switch to and select that repo with the enter key. You will now see a list of all of the branches in that repo.
-
Use the up and down arrow keys to select the branch you would like to switch to in the chosen repo. Switch to that branch by pressing the enter key, or select
<----- back ------
to go back to the repo list.
- At any point press (ctrl + c) to exit
swib
without switching branches.
If you are:
- checking out an existing branch from your local repo:
git checkout <branch_name>
- checking out a new branch locally:
git checkout -b <branch_name>
- switching to a remote branch:
git fetch <remote_repo_name>
&&git switch <branch_name>