Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to run git push conditionally

I want to push changed files using:

git add . 
git commit -m 'build: maintenance' 
git push

but I want to ignore these commands if package-lock.json file is the only changed file

how to configure it to push the changed files only if there is at least any file other than package-lock.json

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and it would be nice to get it to run in cross-platforms

solved by creating a js script and here is the solution

import { execSync } from 'node:child_process';

/**
 * push changed files, ignore if package-lock.json is the only changed file
 */
export function push() {
  let changed = execSync(
    'git add . && git diff-index --cached --name-only HEAD'
  )
    .toString()
    .split('\n')
    .filter((el) => el.trim() !== '' && el.trim() !== 'package-lock.json');

  if (changed.length > 0) {
    execSync("git commit -m 'build: maintenance' && git push");
  }
}

thank you guys for your appreciated help <3

>Solution :

This lists the files with staged changes:

git diff-index --cached --name-only HEAD

Assuming you’re using the bash shell, you can check whether there’s anything there besides package-lock.json:

git add .
if git diff-index --cached --name-only HEAD | grep -vsxF package-lock.json; then
    git commit -m 'build: maintenance'
    git push
fi

I’m assuming that package-lock.json is in the root; adjust to taste.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading