138

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 ?

2

1 Answer 1

211

Simply:

git push origin feature:feature

Or shorter:

git push origin feature
5
  • 5
    Is it necessary to write feature:feature or could you just write git push origin feature ?
    – Glemi
    Nov 13, 2019 at 16:29
  • 6
    @Glemi No it's optional; checkout the refspec bit in the manual.
    – trojanfoe
    Dec 24, 2019 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, 2020 at 8:46
  • If you have git hooks (pre-push) setup, which branch runs the git hooks: the branch you're on, or the branch you're pushing? I believe its the former. Example: if I'm on feature branch but want to push master to origin without having to checkout master first. I believe git hooks will run for feature and not for master.
    – nodebase
    Dec 7, 2022 at 15:20
  • @nodebase It probably depends how you write the hook script; I am no expert on git hooks but I found an example that stops force pushes to master.
    – trojanfoe
    Dec 7, 2022 at 17:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.