118

I want to push a branch (not the current) without having to check it out first, how can I achieve that ?

this is how I'd do:

#currently in master
git checkout feature
git push origin feature
git checkout master

but checking out feature can cause conflicts, can't I just push another branch than the current one ?

BiAiB
  • 11,493
  • 7
  • 40
  • 59
  • Possibly related with a different solution: https://stackoverflow.com/questions/51342767/push-a-git-branch-to-remote-without-checking-the-branch-out – Xun Yang Dec 10 '18 at 09:56
  • 7
    Note there is a risk associated with this practice: If you have [push hooks](https://githooks.com/), they will run on your current branch instead of the branch you want to push. – Xun Yang Dec 10 '18 at 09:59

1 Answers1

183

Simply:

git push origin feature:feature

Or shorter:

git push origin feature
Lorenz Meyer
  • 18,330
  • 22
  • 72
  • 114
trojanfoe
  • 118,129
  • 19
  • 204
  • 237
  • 5
    Is it necessary to write `feature:feature` or could you just write `git push origin feature` ? – Glemi Nov 13 '19 at 16:29
  • 5
    @Glemi No it's optional; checkout the refspec bit in [the manual](https://git-scm.com/docs/git-push). – trojanfoe Dec 24 '19 at 12:52
  • 3
    It doesnt work for me,, saying error: src refspec dev does not match any // error: failed to push some refs – toioioi Oct 31 '20 at 08:46