"value":"\n <p>I've recently started trialing Git worktrees again as part of my development workflow.<\/p>\n\n<p>If you are unfamiliar with Git worktrees, they allow you to have muliple branches of a repository checked out at the same time in different directories.<\/p>\n\n<p>For example, this is what I see within my local checkout of my website repository:<\/p>\n\n<pre><code>.\n\u251c\u2500\u2500 config\n\u251c\u2500\u2500 HEAD\n\u251c\u2500\u2500 main\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 ansible\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 nginx\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 README.md\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 website\n\u251c\u2500\u2500 new-post\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 ansible\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 nginx\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 README.md\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 website\n\u251c\u2500\u2500 objects\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 info\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 pack\n\u251c\u2500\u2500 packed-refs\n\u251c\u2500\u2500 refs\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 heads\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 tags\n\u2514\u2500\u2500 worktrees\n \u251c\u2500\u2500 main\n \u2514\u2500\u2500 new-post\n<\/code><\/pre>\n\n<p>The first thing that you'll notice is, because it's a bare clone, it looks a little different to a what you usually see in a Git repository.<\/p>\n\n<p>Each worktree has it's own directory, so my \"main\" branch inside the <code>main<\/code> directory.<\/p>\n\n<p>If I need to work on a different branch, such as <code>new-post<\/code>, then I can create a new worktree, move into that directory and start working. I don't need to commit or stash any in-progress work and switch branches.<\/p>\n\n<h2 id=\"complications-with-docker-compose\">Complications with Docker Compose<\/h2>\n\n<p>I use Docker and Docker Compose for my projects, and this caused some issues for me the last time that I tried using worktrees.<\/p>\n\n<p>By default, Docker Compose will use the name of the directory that the Compose file is in to name its containers. If the directory name is \"oliverdavies-uk\", then the containers will be <code>oliverdavies-uk-web_1<\/code>, <code>oliverdavies-uk-db_1<\/code> etc.<\/p>\n\n<p>This doesn't work so well if the directory is a worktree called \"main\" or \"master\" as you'll have containers called <code>main_web_1<\/code> or <code>master_db_1<\/code>.<\/p>\n\n<p>The way to solve this is to use the <code>COMPOSE_PROJECT_NAME<\/code> environment variable.<\/p>\n\n<p>If you prefix Docker Compose commands with <code>COMPOSE_PROJECT_NAME=your-project<\/code>, or add it to an <code>.env<\/code> file (Docker Compose will load this automatically), then this will override the prefix in the container names to be <code>your-project-{service}<\/code>.<\/p>\n\n<h2 id=\"container-names-per-worktree\">Container names per worktree<\/h2>\n\n<p>Whilst you could use the same Compose project name within all of your worktrees, I prefer to include the worktree name as a suffix - something like <code>my-project-main<\/code> or <code>my-project-staging<\/code> - and keep these stored in an <code>.env<\/code> file in each worktree's directory.<\/p>\n\n<p>As each worktree now has unique container names, I can have multiple instances of a project running at the same time, and each worktree will have it's own separate data - meaning that I can make changes and test something in one worktree without affecting any others.<\/p>\n\n<p>You can also use the <code>COMPOSE_PROJECT_NAME<\/code> variable inside Docker Compose files.<\/p>\n\n<p>For example, if you use Traefik and needed to override the host URL for a service, the string will be interpolated and the project name would be injected as you'd expect.<\/p>\n\n<pre><code class=\"language-yaml\">labels:\n - \"traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(\n `${COMPOSE_PROJECT_NAME}.docker.localhost`,\n `admin.${COMPOSE_PROJECT_NAME}.docker.localhost`\n )\"\n<\/code><\/pre>\n\n<p>ThismeansthatTraefikwouldcontinuetouse
"format":"full_html",
"processed":"\n <p>I've recently started trialing Git worktrees again as part of my development workflow.<\/p>\n\n<p>If you are unfamiliar with Git worktrees, they allow you to have muliple branches of a repository checked out at the same time in different directories.<\/p>\n\n<p>For example, this is what I see within my local checkout of my website repository:<\/p>\n\n<pre><code>.\n\u251c\u2500\u2500 config\n\u251c\u2500\u2500 HEAD\n\u251c\u2500\u2500 main\n\u2502 \u251c\u2500\u2500 ansible\n\u2502 \u251c\u2500\u2500 nginx\n\u2502 \u251c\u2500\u2500 README.md\n\u2502 \u2514\u2500\u2500 website\n\u251c\u2500\u2500 new-post\n\u2502 \u251c\u2500\u2500 ansible\n\u2502 \u251c\u2500\u2500 nginx\n\u2502 \u251c\u2500\u2500 README.md\n\u2502 \u2514\u2500\u2500 website\n\u251c\u2500\u2500 objects\n\u2502 \u251c\u2500\u2500 info\n\u2502 \u2514\u2500\u2500 pack\n\u251c\u2500\u2500 packed-refs\n\u251c\u2500\u2500 refs\n\u2502 \u251c\u2500\u2500 heads\n\u2502 \u2514\u2500\u2500 tags\n\u2514\u2500\u2500 worktrees\n \u251c\u2500\u2500 main\n \u2514\u2500\u2500 new-post\n<\/code><\/pre>\n\n<p>The first thing that you'll notice is, because it's a bare clone, it looks a little different to a what you usually see in a Git repository.<\/p>\n\n<p>Each worktree has it's own directory, so my \"main\" branch inside the <code>main<\/code> directory.<\/p>\n\n<p>If I need to work on a different branch, such as <code>new-post<\/code>, then I can create a new worktree, move into that directory and start working. I don't need to commit or stash any in-progress work and switch branches.<\/p>\n\n<h2 id=\"complications-with-docker-compose\">Complications with Docker Compose<\/h2>\n\n<p>I use Docker and Docker Compose for my projects, and this caused some issues for me the last time that I tried using worktrees.<\/p>\n\n<p>By default, Docker Compose will use the name of the directory that the Compose file is in to name its containers. If the directory name is \"oliverdavies-uk\", then the containers will be <code>oliverdavies-uk-web_1<\/code>, <code>oliverdavies-uk-db_1<\/code> etc.<\/p>\n\n<p>This doesn't work so well if the directory is a worktree called \"main\" or \"master\" as you'll have containers called <code>main_web_1<\/code> or <code>master_db_1<\/code>.<\/p>\n\n<p>The way to solve this is to use the <code>COMPOSE_PROJECT_NAME<\/code> environment variable.<\/p>\n\n<p>If you prefix Docker Compose commands with <code>COMPOSE_PROJECT_NAME=your-project<\/code>, or add it to an <code>.env<\/code> file (Docker Compose will load this automatically), then this will override the prefix in the container names to be <code>your-project-{service}<\/code>.<\/p>\n\n<h2 id=\"container-names-per-worktree\">Container names per worktree<\/h2>\n\n<p>Whilst you could use the same Compose project name within all of your worktrees, I prefer to include the worktree name as a suffix - something like <code>my-project-main<\/code> or <code>my-project-staging<\/code> - and keep these stored in an <code>.env<\/code> file in each worktree's directory.<\/p>\n\n<p>As each worktree now has unique container names, I can have multiple instances of a project running at the same time, and each worktree will have it's own separate data - meaning that I can make changes and test something in one worktree without affecting any others.<\/p>\n\n<p>You can also use the <code>COMPOSE_PROJECT_NAME<\/code> variable inside Docker Compose files.<\/p>\n\n<p>For example, if you use Traefik and needed to override the host URL for a service, the string will be interpolated and the project name would be injected as you'd expect.<\/p>\n\n<pre><code class=\"language-yaml\">labels:\n - \"traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(\n `${COMPOSE_PROJECT_NAME}.docker.localhost`,\n `admin.${COMPOSE_PROJECT_NAME}.docker.localhost`\n )\"\n<\/code><\/pre>\n\n<p>ThismeansthatTraefikwouldcontinueto