TIL what bare git repositories and git worktrees are
by @0xi4o
Git has this feature called bare repositories. After a normal clone, along with our project files, there's a `.git` hidden directory. Going into the .git directory, we can see all the folders and files that git keeps to do our version control.
A bare repo, doesn't have our project files and only has the folders and files in the above image. When we clone a repo, if we add `--bare` to the clone command, we make a bare repo.
Why do this?
For one, we can clone off of a bare repo:
A bare repo, doesn't have our project files and only has the folders and files in the above image. When we clone a repo, if we add `--bare` to the clone command, we make a bare repo.
Why do this?
For one, we can clone off of a bare repo:
git clone --bare /home/ilango/aurelius aurelius_2
Note that the I'm cloning a repo from a local folder. This is useful for collaboration when we have a shared storage in a local network and don't want to host our project in the cloud.
What made me more interested in bare repos is worktrees. Git worktrees are one of the lesser known but coolest features. It allows us to have unfinished work in multiple branches at the same time, without having to use `git stash`.
For example, let's say you're in `feature-branch-1`, and are made aware of a bug in `main`. You have unstaged work in feature-branch-1, so you run `git stash`, checkout to main, fix the bug, come back to the feature branch and run `git stash pop`. With worktrees, we don't need to do all this.
Instead of a normal clone, do a bare clone, and run
git worktree add main main
Note: first "main" is the folder name, second "main" is the branch name. Also note, the main folder in the image. That's my main branch worktree.
This creates a main folder in your bare repo. Changing to this repo we can see that the main folder contains our main branch! We can do this for as many branches as we want and switch between them with different changes. We don't need bare repositories for using git worktrees but it keeps my folder structure cleaner.
More details here: https://git-scm.com/docs/git-worktree