<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Archit Chandra</title>
    <description>The latest articles on DEV Community by Archit Chandra (@architchandra).</description>
    <link>https://dev.to/architchandra</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1061868%2F9da11661-6439-4f1f-a96c-449c9617d439.jpeg</url>
      <title>DEV Community: Archit Chandra</title>
      <link>https://dev.to/architchandra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/architchandra"/>
    <language>en</language>
    <item>
      <title>A Side Effect of Storing a Git Repository in iCloud Drive</title>
      <dc:creator>Archit Chandra</dc:creator>
      <pubDate>Thu, 10 Aug 2023 13:25:52 +0000</pubDate>
      <link>https://dev.to/architchandra/a-side-effect-of-storing-a-git-repository-in-icloud-drive-7ed</link>
      <guid>https://dev.to/architchandra/a-side-effect-of-storing-a-git-repository-in-icloud-drive-7ed</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of my digital garden on &lt;a href="https://architchandra.com/articles/a-side-effect-of-storing-a-git-repository-in-icloud-drive/"&gt;architchandra.com&lt;/a&gt; and will keep evolving with time.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;I've encountered some weird issues when trying to commit changes for my notes repository which is stored in a folder synced via iCloud. This article references iCloud but the same issues can also arise with other file sync services like Google Drive, Dropbox, OneDrive, etc.&lt;/p&gt;

&lt;p&gt;I'm documenting my experience here in case someone ends up in my position in the future. I wouldn't put it past myself to forget my lesson and run into the same issue again (say thanks, future Archit).&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Context First
&lt;/h2&gt;

&lt;p&gt;I use &lt;a href="https://obsidian.md/"&gt;Obsidian&lt;/a&gt; to create notes as Markdown files on my computer and use the &lt;a href="https://github.com/denolehov/obsidian-git"&gt;Obsidian Git&lt;/a&gt; plugin to version control the changes via Git. The Obsidian vault in which I store my notes is stored in a folder &lt;a href="https://help.obsidian.md/Getting+started/Sync+your+notes+across+devices#iCloud+Drive"&gt;synced using iCloud Drive&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have this setup because I need to access my notes on my iPhone  and two computers with the &lt;em&gt;same Git credentials&lt;/em&gt;—my Mac Mini (daily driver) and my older yet portable MacBook Air (secondary computer). The logic is that iCloud will allow my notes to be accessible on all these devices seamlessly (courtesy of the Apple ecosystem) and Git will allow me to version-control my notes in a familiar and friendly way.&lt;/p&gt;

&lt;p&gt;This works fine on most occasions but dives into a hellhole every once in a while.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem (Fatal: Bad Object)
&lt;/h2&gt;

&lt;p&gt;Sometimes, when I try to pull from or push to a remote repository, I get an error along the lines of &lt;code&gt;fatal: bad object refs/heads/main 2&lt;/code&gt;. This is generally followed by a message that reads &lt;code&gt;error: &amp;lt;remote-repo-name&amp;gt;.git did not send all necessary objects&lt;/code&gt;. I've also seen an error message reading &lt;code&gt;fatal: Could not parse object &amp;lt;commit-hash&amp;gt;&lt;/code&gt; in such circumstances a couple of times.&lt;/p&gt;

&lt;p&gt;I'm not a Git pro, so I got stumped by it the first time till I read the error message properly. And then suddenly, I went "What is this &lt;code&gt;2&lt;/code&gt; doing in there?". It turns out that sometimes iCloud Drive can't properly reconcile the changes made on the same file on two different devices—iCloud's equivalent of a merge conflict. In such cases, it looks like iCloud Drive just adds a numbered suffix (like a &lt;code&gt;2&lt;/code&gt;) to the file modified more recently. Sometimes, there's just one suffixed file (the one without the suffix is missing or was renamed). Don't know why that happens but it creates the same kind of issue 🤷🏽‍♂️.&lt;/p&gt;

&lt;p&gt;This might not be a huge problem in most circumstances but in my case, Git itself stops working. And the biggest purpose of using file-based notes gets thrown down the drain.&lt;/p&gt;

&lt;p&gt;According to the error message, the files in question are in the &lt;code&gt;.git/refs/heads&lt;/code&gt; folder, which is how Git keeps track of its &lt;a href="https://initialcommit.com/blog/what-is-git-head"&gt;HEAD reference&lt;/a&gt;. From what I understand, this situation violates one or both of the following rules around HEAD references:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only one HEAD reference per branch is allowed in a repository (looking at normal repositories that we mortals use, not the fancy ones with &lt;a href="https://git-scm.com/docs/git-worktree"&gt;multiple working trees&lt;/a&gt;). Git fails because it now encounters two HEADs.&lt;/li&gt;
&lt;li&gt;Branch names and commit SHAs (possible HEAD references) &lt;a href="https://stackoverflow.com/questions/6619073/why-cant-a-branch-name-contain-the-space-char"&gt;do not allow spaces&lt;/a&gt;. Git fails because it encounters a space character in a HEAD reference.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Fix That Works for Me
&lt;/h2&gt;

&lt;p&gt;I would suggest taking care of a few things before tinkering with anything:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make a copy of the &lt;code&gt;.git&lt;/code&gt; folder in its current erroneous state so that you can get back to it if you manage to screw things up (fairly easy).&lt;/li&gt;
&lt;li&gt;Check the state of the repository on all synced devices. If there are any uncommitted changes, make a note of them. Also, make a copy of any change that you would not like to lose. In my case, the most common culprit is the &lt;code&gt;.obsidian/workspace.json&lt;/code&gt; file. The most common kind of change in it is information on open and recent tabs, which I don't care about much and can hence afford to lose—so I don't take a backup of this file.&lt;/li&gt;
&lt;li&gt;Choose one computer on which you'll make changes and prevent any other synced computers from making changes while you're fixing things. You don't want your fixes to get corrupted by any conflicts getting generated from another device. For me, that means that I'll quit Obsidian on my secondary computer and only then make fixes on my primary computer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the fix itself, you want to have exactly one &lt;em&gt;ref&lt;/em&gt; file per branch. If there's just one file (the one that has the suffix), removing the suffix from the filename should do it. If there are two files, delete the one with the suffix.&lt;/p&gt;

&lt;p&gt;Sometimes, your latest change might have to be committed again. If any changes from another device are not showing up, use your backup of those changes to commit them again. After that, wait for about a minute for iCloud to sync all the changes and then open the other device to cross-check that everything is working there as expected.&lt;/p&gt;

&lt;p&gt;Update: I recently decided to stop tracking the &lt;code&gt;.obsidian/workspace.json&lt;/code&gt; file in Git. That has significantly reduced the frequency of such errors in iCloud.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;While &lt;a href="https://stackoverflow.com/questions/35853139/can-git-and-icloud-drive-be-effectively-used-together"&gt;many&lt;/a&gt; &lt;a href="https://stackoverflow.com/a/59557443"&gt;people&lt;/a&gt; say and &lt;a href="https://support.apple.com/en-us/HT201104"&gt;Apple itself hints&lt;/a&gt; (suggests avoiding &lt;em&gt;app&lt;/em&gt; folders) that it's a bad idea to keep version-controlled files in iCloud Drive, I have been using this setup more or less without hassle for the last six-seven months. It provides an essential service for my notes workflow—Git gives version control and iCloud gives syncing across devices. It's a combo that I wouldn't get easily or for free otherwise. There are, of course, services like &lt;a href="https://obsidian.md/sync"&gt;Obsidian Sync&lt;/a&gt; to do the same thing but they're too expensive for someone sitting in a developing country and too opaque to trust blindly. Hopefully, it will keep working fine in the future too. 🤞🏽&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://architchandra.com/articles/a-side-effect-of-storing-a-git-repository-in-icloud-drive/"&gt;architchandra.com&lt;/a&gt; on July 19, 2023.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>icloud</category>
      <category>googledrive</category>
      <category>obsidian</category>
    </item>
    <item>
      <title>How to Specify a Node Version in Netlify Builds</title>
      <dc:creator>Archit Chandra</dc:creator>
      <pubDate>Sun, 09 Apr 2023 14:29:17 +0000</pubDate>
      <link>https://dev.to/architchandra/how-to-specify-a-node-version-in-netlify-builds-3mjo</link>
      <guid>https://dev.to/architchandra/how-to-specify-a-node-version-in-netlify-builds-3mjo</guid>
      <description>&lt;p&gt;Note: This article is part of my digital garden on &lt;a href="https://architchandra.com/articles/how-to-specify-a-node-version-in-netlify-builds/"&gt;architchandra.com&lt;/a&gt; and will keep evolving with time.&lt;/p&gt;




&lt;p&gt;I keep coming across situations where I need to tell Netlify about the Node version to use while building my sites (eg. when building &lt;a href="http://countmywords.in"&gt;countmywords.in&lt;/a&gt;). Multiple times now, I’ve not been able to find the correct documentation related to this quickly. Guessing that I’m not the only one facing this issue, I decided to write a quick article to offer a solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Default Node Version
&lt;/h2&gt;

&lt;p&gt;Netlify creates a &lt;a href="https://www.docker.com/resources/what-container/"&gt;Docker container&lt;/a&gt; to set up your site behind the scenes. The &lt;a href="https://hub.docker.com/r/netlify/build"&gt;build image&lt;/a&gt; used to create this container defines the software versions installed (including Node).&lt;/p&gt;

&lt;p&gt;If you don’t specify a Node version explicitly, Netlify uses the &lt;a href="https://docs.netlify.com/configure-builds/available-software-at-build-time/#languages"&gt;default Node version&lt;/a&gt; that comes pre-installed in its Docker container. Once you build a site using the default Node version, Netlify locks or &lt;em&gt;pins&lt;/em&gt; your site to that Node version so that your site’s builds continue using the same Node version even when the default Node version in Netlify’s Docker build image changes with time. Your deployment workflow will thus not break when Netlify updates its build image to use a newer version of Node as the default.&lt;/p&gt;

&lt;p&gt;For example, imagine that it’s late 2021 and you are using Node &lt;code&gt;16&lt;/code&gt; to build your Eleventy site on Netlify. Node &lt;code&gt;16&lt;/code&gt; is the latest version and it makes sense for Netlify to include it as the default Node version in its containers. Now, fast forward to March 2023. Netlify has updated the software versions included in its containers and Node &lt;code&gt;18&lt;/code&gt; is now its default Node version. For some reason you were not able to update your code during this time and your site still requires Node &lt;code&gt;16&lt;/code&gt;. However, when you trigger a build on Netlify, your build still goes through without any errors. Because Netlify had pinned your site to use Node &lt;code&gt;16&lt;/code&gt; (to match the Node version used in the previous build), it used Node &lt;code&gt;16&lt;/code&gt; in the current build rather than Node &lt;code&gt;18&lt;/code&gt; (which would only be used in new projects).&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicitly Specify a Node Version
&lt;/h2&gt;

&lt;p&gt;There are two main methods to specify a Node version in Netlify builds:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Set an environment variable called &lt;code&gt;NODE_VERSION&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Create a &lt;code&gt;.node-version&lt;/code&gt; or &lt;code&gt;.nvmrc&lt;/code&gt; file that is stored in the base directory of your code repository.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can specify the version information as the version number (full or partial) for any Node release or a string that &lt;a href="https://github.com/nvm-sh/nvm"&gt;nvm&lt;/a&gt; accepts. Some examples are &lt;code&gt;16&lt;/code&gt;, &lt;code&gt;16.2&lt;/code&gt;, &lt;code&gt;18.5.0&lt;/code&gt;, &lt;code&gt;node&lt;/code&gt; and &lt;code&gt;lts/fermium&lt;/code&gt;. You can read more about the supported inputs on nvm’s &lt;a href="https://github.com/nvm-sh/nvm#nvmrc"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 1: Set a NODE_VERSION Environment Variable
&lt;/h3&gt;

&lt;p&gt;Netlify allows you to set &lt;a href="https://docs.netlify.com/environment-variables/overview/"&gt;environment variables&lt;/a&gt; to configure your site’s build and functionality. Among other things, you can use them to specify the versions of some &lt;a href="https://docs.netlify.com/configure-builds/available-software-at-build-time/"&gt;software available during the build&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Netlify supports two kinds of environment variables- &lt;em&gt;site&lt;/em&gt; and &lt;em&gt;shared&lt;/em&gt;. &lt;a href="https://docs.netlify.com/environment-variables/get-started/#site-environment-variables"&gt;Site environment variables&lt;/a&gt; are set at the site-level whereas &lt;a href="https://docs.netlify.com/environment-variables/get-started/#shared-environment-variables"&gt;shared environment variables&lt;/a&gt; are set at the team-level and all the sites owned by a team has access to them. In this article, &lt;strong&gt;you will use a site environment variable&lt;/strong&gt; to set the Node version for a particular site’s build.&lt;/p&gt;

&lt;p&gt;Further, Netlify offers &lt;a href="https://docs.netlify.com/environment-variables/overview/#configuration-options-and-limitations"&gt;multiple methods&lt;/a&gt; to set environment variables. To reduce decision fatigue, you will use their UI to set site environment variables. To do so, select a site from the Netlify dashboard and go to &lt;strong&gt;Site settings -&amp;gt; Environment variables&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 2: Create a Configuration File Supported by a Node Version Manager
&lt;/h3&gt;

&lt;p&gt;Node version managers allow you to install multiple Node versions on a machine and switch to a particular one based on your project’s needs. Netlify’s build containers come installed with a Node version manager that can be configured using a file.&lt;/p&gt;

&lt;p&gt;You can specify a Node version by creating a &lt;code&gt;.node-version&lt;/code&gt; or &lt;code&gt;.nvmrc&lt;/code&gt; file with the version information and adding it to your base directory.&lt;/p&gt;

&lt;p&gt;An aside on these files. The &lt;code&gt;.nvmrc&lt;/code&gt; file is fairly well-known these days as nvm has become the most popular version manager for Node. The &lt;code&gt;.node-version&lt;/code&gt; file is a similar configuration file that is supported by a few &lt;a href="https://stackoverflow.com/questions/27425852/what-uses-respects-the-node-version-file"&gt;other version managers&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at architchandra.com on March 28, 2023.&lt;/p&gt;

</description>
      <category>netlify</category>
      <category>webdev</category>
      <category>jamstack</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
