3

My workflow with new branches is:

git checkout -b <new branch name>
// making changes, commit the changes
git push -u origin <new branch name>

Is there any way to push the local branch to a new remote branch without writing the name of the branch again (local and remote branch will have the same name)? Everyday I make at least 1 feature branch with really long name and at the end of the day I have to go back to JIRA board to get project IDs and other things required for the convention. I'm just curious if there is some hack to get the local branch name and pass it directly to git push -u origin <local branch name>

Nikolai
  • 555
  • 7
  • 17
  • 1
    If your branch is configured with an upstream, `git push` is sufficient. – matt Apr 13 '20 at 17:14
  • 1
    Does this answer your question? [Why do I need to do \`--set-upstream\` all the time?](https://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time) – mkrieger1 Apr 13 '20 at 17:14
  • Might also want to read https://stackoverflow.com/questions/948354/default-behavior-of-git-push-without-a-branch-specified – matt Apr 13 '20 at 17:16
  • Thanks to both of you. I know that I have to write it once per branch, but I was thinking about a shortcut for creating the remote branch with the exact name as the local branch. It happens to copy something else or if I write something by convention, by accident to click enter while writing the name of the remote branch. Then I need to go and delete the remote. IDK. I wanted some trick to get the local branch's name while creating the remote branch to automate the process of writing the same name of the branch again. – Nikolai Apr 13 '20 at 17:27
  • "but I was thinking about a shortcut for creating the remote branch with the exact name as the local branch" But the remote branch always automatically has the same exact name as the local branch, so what's the problem? If `push.default` is `current`, you just say `git push -u` and you're all set. – matt Apr 13 '20 at 17:45
  • You _never_ get to tell the remote what name to give the branch, that is not what `git push -u origin branchname` means, you _are_ giving the local branch _already_ – matt Apr 13 '20 at 17:56

3 Answers3

7

I do this, HEAD automatically refers to your current branch (technically current commit, but don't worry about that)

git push -u origin HEAD

QurakNerd
  • 652
  • 4
  • 11
1

Try git config --global push.default current and then just git push -u origin

jjuraszek
  • 151
  • 1
  • 3
0

To push the current branch and set the remote as upstream, use below command:

git push --set-upstream origin yourBranchName
Bayram Binbir
  • 1,531
  • 11
  • 11