stop copying files between git branches, switch to git worktrees  

So this goes without saying but if you’re new to learning git been tirelessly in the weeds of some project where you need to copy files from one branch over to another, look no further.

In quite a few instances, especially in the early days, I’ve gone with the good ol’ copy-paste route between git branches. Let’s face it. Attempting the git rebase -i HEAD~@#$@#$ or git merge can be quite daunting for newcomers including the host of file conflicts you’ll need to be able to resolve. Or the merge might happen successfully and you’d have realized copying partial files was the way to go but wanted to test if partial copies would still lead to a successful build.

where in the version queue do you stand? #

Before you attempt a diff to preview files differences between the two branches, a quick way to know where all the branches stand next to each other can be quite useful. Using the CLI tool tig, you can view which branches were merged into the current branch, which ones branched off your current, in addition to where the remotes sit in comparison. Install tig via homebrew – brew install tig and enter tig. Here’s a preview from the OSS github desktop app* tool.

GitUp is also an excellent tool but I’m too old-fashioned. I use the desktop app for various advanced use cases which I’ll write about in a future post.

1__desktop__tig__zsh_.png

The names and the timestamps of commit logs are self-explanatory. The lines are particularly interesting as the leftmost with Ms represents my local master and others in relation including remotes marked with {upstream/master} or {origin/master}. So what’s interesting? The comparison branches [spell-checking-in-summary-branch] and [feature/history-mode...] are pretty far apart which helps to give foresight as there might be tons of changes. You can also try git diff <another-branch> --name-only to list out the files that are different.

introducing worktrees #

A worktree basically is opening two branches of the same repository. You’ll need to give the worktree apath and it’s recommended to not checkout the same branch in the new worktree. You can open an existing branch, or start a new branch.

git worktree add -b <new-branch-name> <new-filepath> <cloned-branch>

1____Dropbox_Forks_desktop__zsh_.png
creating a new branch small-fix with new worktree filepath branched of master

git worktree add <new-filepath> <cloned-branch>

1____Dropbox_Forks_worktree__git_.png
creating a new worktree branching into master

To explore try git worktree -h or ask me here. If this has helped or entertained in anyway don’t forget to punch some ⚫️.

*If you’re looking to contribute, they welcome newcomers contribute on help wanted issues.

 
7
Kudos
 
7
Kudos

Now read this

what early startups value

Not all startups are created equal. Paul Graham defines a venture or entity that has the potential to grow fast. Often, people conflate the term to mean something that raises capital for growth. And if it doesn’t, it’s bootstrapped. Most... Continue →