gitgerritsimple-git

Edit remote reference in Git Config


I work with Gerritt and need some help in automating a manual step in our code check in process. Here is the background. When I clony my repo afresh, here is what I see in my Git config (inside .git folder).

[remote "origin"]
    url = https://user@git.some.server.corp/contentpipeline.git
    fetch = +refs/heads/*:refs/remotes/origin/*

We make the following change manually in the git config. (Ref 2nd line)

[remote "origin"]
    push = +refs/heads/MYBRANCH:refs/for/MYBRANCH
    url = https://user@git.some.server.corp/contentpipeline.git
    fetch = +refs/heads/*:refs/remotes/origin/*

The line with push is added so that the change is not directly pushed to MYBRANCH but is treated as 'Push for Review'.

I have 2 questions:

  1. Can this manual addition be automated using a git CLI command.
  2. We are using a NOde JS framework simple-git (https://www.npmjs.com/package/simple-git?activeTab=readme) to automate this process. Can this be done using simple-git as well.

Thanks a lot, Prabal


Solution

  • Any setting in git config can be added using git config [path.to.setting] [value].

    In your case:

    git config remote.origin.push +refs/heads/MYBRANCH:refs/for/MYBRANCH
    

    Check git help config for all the nitty gritty details of how to edit your configuration.

    simple-git is just a wrapper on top of git command line tool, so yes, you can run the exact same command using simple-git -- I will let you refer to the documentation to find out how.


    for the sake of illustration:

    # after running:
    git config foo.bar.baz Hello
    
    # I get:
    cat .git/config | tail -2
    # [foo "bar"]
    #   baz = Hello
    

    Also, as stated in the comments, if you want to set a refspec that works for all branches (not just MYBRANCH), you can use :

    git config remote.origin.push "+refs/heads/*:refs/for/*"