Default behavior of “git push” without a branch specified

前端 未结 12 1329
眼角桃花
眼角桃花 2020-11-22 05:32

I use the following command to push to my remote branch:

git push origin sandbox

If I say

git push origin

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 06:20

    Rather than using aliases, I prefer creating git-XXX scripts so I can source control them more easily (our devs all have a certain source controlled dir on their path for this type of thing).

    This script (called git-setpush) will set the config value for remote.origin.push value to something that will only push the current branch:

    #!/bin/bash -eu
    
    CURRENT_BRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
    NEW_PUSH_REF=HEAD:refs/for/$CURRENT_BRANCH
    
    echo "setting remote.origin.push to $NEW_PUSH_REF"
    git config remote.origin.push $NEW_PUSH_REF
    

    note, as we're using Gerrit, it sets the target to refs/for/XXX to push into a review branch. It also assumes origin is your remote name.

    Invoke it after checking out a branch with

    git checkout your-branch
    git setpush
    

    It could obviously be adapted to also do the checkout, but I like scripts to do one thing and do it well

提交回复
热议问题