Git: Checkout a Single File from a Previous Commit

Often we alter or delete a file that we didn’t really want to. Sometimes this goes unnoticed for a few commits. It’s fairly easy in git to retrieve a specific file from a specific commit.

git checkout <commit> <path>
git checkout f08b32 ./src/main.c
git checkout HEAD~2 ./src/main.c

How to reattach a GIT detached head.

$ git status
HEAD detached at 36bc359
nothing to commit, working tree clean

$ git branch fix-detached
$ git checkout main
$ git merge fix-detached -Xtheirs
$ git branch -d fix-detached

Get all files in directory

Get all files in directory

This code snippet recursively examines a directory, and all it’s subdirectories, to identify files. It returns an array of objects with the fullpath, and the filename as strings.

import FS from 'fs';
import Path from 'path';

 * Recursively retrieve a list of files from the specified directory.
 * @param {String} directory 
 * @returns An array of {fullpath, name} obects.
 function getFiles(directory = "."){
      const dirEntries = FS.readdirSync(directory, { withFileTypes: true });
      const files = => {
      return dirEntry.isDirectory() ? getFiles(resolved) : {fullpath : resolved, name :};
      return files.flat();

  export default getFiles;

The filesystem library for JS provides the synchronous read directory function (line 10). Setting withFileTypes to true in the options, directs readdirSync to return directory entry objects.

The array’s map method passes in each directory entry object into a provided callback function (lines 12-15). The return value of this function is inserted into a new array.

The ternary operator on line 12 is the callback, and works as follows:

If the dirEntry object is not a function then add the fullpath and name to the array. If it is, then recursively call the getFiles function and and add the result to the array. Because the recursive call nests arrays into arrays, we call the array’s flat method which creates a new single 1-dimensional array.

Updating NPM Packages

Updating NPM Packages

  1. Navigate to the root directory of your project and ensure it contains a package.json file:
    • cd /path/to/project
  2. In your project root directory, run the outdated command to view outdated packages:
    • npm outdated
  3. In your project root directory, run the update command:
    • npm update


The way the wanted version is set in the package.json file may affect how updates are decided.

Version numbers are declared as a [major, minor, patch] tuple.

Caret Ranges ^1.2.3

Allows changes that do not modify the left-most non-zero element of the version specification. So 1.3.1 will update to 1.3.2 or 1.4.0. Though, 0.3.0 will update to 0.3.1 but not update to 0.4.0.

Version will update if:

  • ^1.2.3 : 1.2.3 < version < 2.0.0
  • ^0.3.0 : 0.3.0 < version < 0.4.0


Allows ‘X’, ‘x’, or ‘*’ to stand in for a number in the version specification.

Version will update if:

  • “” : any version
    • same as “*” or “x.x.x”
  • 1.x.x : 1.0.0 < version < 2.0.0
    • same as “1”
  • 1.2.x : 1.2.0 < version < 1.3.0
    • same as “1.2”