How To Use Git Worktree To Checkout Multiple Branches At Once

git worktree allows you to checkout multiple branches at the same time. It’s somewhat similar to git checkout -b except that it will create the branch at a new path.

For example, running this command in your repository will create a new example-worktree folder outside of your repository and a new git branch named example-worktree

$~ git worktree add ../example-worktree

If you have an existing branch that you want to use worktree with, you need to run the following command instead

$~ git worktree add ../example-worktree $REPLACE_WITH_EXISTING_BRANCH_NAME

To get a list of worktree

To see all available working trees that you have created, you can run

$~ git worktree list

This will print out the following outputs

/ROOT/go-projects/main-project-folder   14e218b [main]
/ROOT/go-projects/example-worktree      14e218b [example-branch]

The first prefix is the worktree’s folder path and the suffix is the name of the branch it’s on

To remove a worktree

I would recommend you to delete a worktree once you’ve done using it. To delete it use the following command:

$~ git worktree remove $WORKTREE_PATH_NAME

Caveats

One thing that I noticed when using worktree is that you can’t checkout to the same branch that’s already in worktree. For example, I can’t checkout to example-branch from main branch.

Conclusion

git worktree is a pretty useful feature but I would recommend you use it sparingly. It can get confusing if you have too many worktree available. I would recommend you to remove it once you’re done with it.

Hopefully you find this helpful as a quick references.

PHP missing dependencies

I was having some issues with my PHP package installed through asdf yesterday. Something about missing libiconv and when I ran php -v it returns me this response:

No php executable found for php 8.2.0

To fix the issue, I followed this tips on Github issues

Install the optional dependencies:

$~ brew install gmp libsodium imagemagick

Reinstall the possible missing dependencies. brew will just skipped any packages that I’ve already installed.

$~ brew install autoconf automake bison freetype gd gettext icu4c krb5 libedit libiconv libjpeg libpng libxml2 libzip pkg-config re2c zlib

Reinstall latest php with asdf and set it as the default

$~ asdf install php latest
$~ asdf global php latest

I reshimed it just to make sure

$~ asdf reshim php

That’s all

Replacing nvm with asdf to manage NodeJS versions

I’ve been using zsh with antigen as the package manager for years. Over time as I add more customizations, I noticed that my shell startup time has become slow. Sometimes, it would take more than 3 seconds to start up.

I read few months (or a year) ago that nvm, a version manager for nodejs can cause this issue so I thought maybe it’s the right time I should try asdf.

asdf is basically just another tool to manage versions but instead of having to install multiple tools like rbenv for ruby or nvm for nodejs, you can just use asdf and use its plugins to manage all the language versions that you’re using.

Anyway, I’m not going to talk about what’s so good about it. For that, you can read the docs yourself. I’m just going to document the steps I did here.

Removing nvm completely

First of all, I have to completely remove nvm. Here are the steps:

# list out all the nodes version I'm using
$~ nvm ls
# uninstall nodejs
$~ nvm uninstall <NODEJS_VERSION> 
# uninstall nvm with brew (since I install it using Brew)
$~ brew uninstall nvm
# delete related dotfiles and directory
$~ rm -rf ~/.nvm/ ~/.nvmrc

Installing asdf

Then, I installed asdf using Brew

# install asdf using Brew
$~ brew install asdf

Then, I have to add the following in my ~/.zshrc file to load asdf properly.

...
# setup asdf
. /usr/local/opt/asdf/asdf.sh
...

That basically completes the asdf setup. Next I have to install the nodejs plugin. To install the nodejs plugin, I run the following command:

$~ asdf plugin-add nodejs # install nodejs plugin
$~ bash -c '${ASDF_DATA_DIR:=$HOME/.asdf}/plugins/nodejs/bin/import-release-team-keyring # import nodejs release team OpenPGP keys

I got an error when trying to import the keys. Apparently, I don’t have gpg2 installed yet so I have to install it first.

$~ brew install gpg2

After installing gpg2, I can finally run the nodejs installation without any error.

$~ asdf install nodejs 15.8.0 # install nodejs version 15.8.0
$~ asdf global nodejs 15.8.0 # set it as the global version

That’s pretty much it. I finally have a working nodejs environment and I notice that my terminal startup time is a little bit faster now. Nice.

I just need to replace rbenv and pyenv next.

Runners - iOS app to manage Gitlab Runners

I made a thing! It’s an iOS app to remotely manage Gitlab runners. I wrote this app to solve one of the pain points that I frequently face back when I managed the CI/CD machine that hosted the Gitlab runners for my teams.

My coworkers would frequently ask me to check why their pipelines or runners were failing and so in a begrudging manner, I would VNC’d into the machine and checked them.

so…

๐Ÿฅ๐Ÿฅ๐Ÿฅ

A screenshot of Runners app on the App Store

TADAA!

This app solved these problems by providing an easy access for me to:

  • Quickly check the runners status
  • Pause, resume, or delete a runner remotely

Oh! Did I mentioned that this is my first SwiftUI and paid app? SwiftUI is really fun to write but there are some weird bugs that Apple needs to fix still.

Anyway, it’s available now on the App Store for the price of US$1.99 or MYR 7.90.

Do check it out from the links below!

๐Ÿ‘‰ Download it from the AppStore ๐Ÿ‘ˆ

How to list all the git commits history for a specific file

Assuming the following situation:

  • You work on a large project that has an extensive git commits history and a lot of files
  • You need to debug a certain issue that was caused by a certain file
  • You look at the commits history to see which commit is causing the issue but going through it one by one is like finding a needle in a haystack

Well, worry not because you can use the following git log command to solve your issue:

$~ git log --follow -- path/to/file.txt

This will list down all the commits history for that specific file. It will work even if the file was deleted or renamed. However please be aware that it will only work for a single file at a time.