Search code examples
bashgitnpmpushupstream-branch

Git push upstream with dynamic branch name inside an npm script


Is it possible to run an npm script containing a git push command with the upstream option based on the current branch ?

As an example, I would like to be able to run a command npm run push.
This command will do something like git push -u origin ${current-branch} where $current-branch will be replaced by the local current git branch.

I know that this is possible to achieve it by creating a script, but I would like to know if there is already something provided by npm or git to achieve this with the minimal code requirement.

Thanks for the help !

Solution:

Vlad274's solution works.

Steps:

  • Add a new file .gitconfig on the root of your repository
  • Apply the configuration with git config --local include.path ../.gitconfig
  • You will be able to use a new git command which will push with the origin by using the default local branch name

Solution

  • I do this with a git alias, but I assume the same commands would work via npm.

    In ~/.gitconfig:

    [alias]
        branch-name = "!git rev-parse --abbrev-ref HEAD"
        pub = "!f(){ git push -u origin $(git branch-name); };f"