Skip to content

Instantly share code, notes, and snippets.

@SimonEast
Created June 30, 2026 10:56
Show Gist options
  • Select an option

  • Save SimonEast/03f24c2138e2a6f3a6cf4539693e1c3e to your computer and use it in GitHub Desktop.

Select an option

Save SimonEast/03f24c2138e2a6f3a6cf4539693e1c3e to your computer and use it in GitHub Desktop.
commit-and-tag-version Search and Replace Script

commit-and-tag-version Search and Replace Script

commit-and-tag-version is a utility for incrementing (or "bumping") the semantic version number of your software. When run, it looks at your git history and decides whether the next version should be a major, minor or patch increment (or you can override this manually). By default, it is able to modify package.json (for JS/npm modules) and several other common file formats to contain the updated version number, rather than having to do it manually.

But if your version number is stored in files that commit-and-tag version doesn't support, you may need a script like this. In my case, I had some source code which printed out the version number within the program, for example:

$VERSION = "1.2.3-beta"

commit-and-tag-version doesn't come with a search-and-replace module for situations like this, but the following short script adds that ability.

Installation

  1. Make sure you've installed commit-and-tag-version

  2. Download the JS file below to your project. Tweak the search string to match the format that you need.

  3. Update your commit-and-tag version configuration to point to the JS script, and specify which files you want updated.

    In my case, I'm using package.json to configure commit-and-tag version (but there are other methods too), and I'd also like to keep the default behaviour of bumping the version number inside package.json and package-lock.json so I've ensured those are specified, too:

    {
      ...
      "commit-and-tag-version": {
        "bumpFiles": [
          {"filename": "package.json", "type": "json"},
          {"filename": "package-lock.json", "type": "json"},
          {"filename": "file-to-update.txt", "updater": "search-replace-version.js"}
        ]
      }
    }

Testing and Refining Your Settings

  1. Commit any outstanding changes to git before running the following tests, so that you can easily backtrack if something goes wrong.

  2. Run commit-and-tag-version --skip.commit --skip.tag. This will "bump" (increment) the version number in your given files, but not perform a git commit or git tag -- useful while testing. You should see a line like this in the console, which indicates that your file was updated:

    √ bumping version in path/to/file-to-update.txt from 1.0.0 to 1.0.1
    
  3. Check that the file was updated correctly.

    If not, undo the changes, tweak the search string or bumpFiles setting, then repeat step 2.

  4. Commit search-replace-version.js to your repo so that other users can use it to bump the version number

  5. Run the full commit-and-tag-version command next time you're ready to release a new version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment