13

When working on patch fixes for a specific, tracked issue, our workflow looks like:

1. git checkout patch; git pull upstream patch; # make sure we're up-to-date 2. git checkout -b issue-435-db-integrity-exception 3. # code some awesome 4. git commit -am "Fixes issue #435" 5. git push -u origin issue-435-db-integrity-exception 

Then we open a pull request from origin/435 to upstream/patch, so that the code review can take place on GitHub/Bitbucket. Then we just start over from step #1.

But, though it may sound a bit whiny, it would be great if we didn't have to explicitly name the remote branch we want to create:

git push -u origin issue-435-db-integrity-exception 

It's not a lot of fun to type that branch name all over again, and I disagree with changing it to just 435 or something more compact.

Is there a way to (1) force Git to push the current branch to a similarly named branch, creating it if necessary without explicitly naming it? Not globally, just an on-the-spot kind of flag.

Or, is it possible to (2) access the current branch in a Git alias, and write something like:

[alias] pnew = push -u origin $(git symbolic-ref --short HEAD) 

(But this doesn't work - it thinks that the --short option is meant for push)

2 Answers 2

23

You could use the following:

# For Git 1.8.5+ git push origin -u @ # For older versions of Git git push origin -u HEAD 

By using @ or HEAD, Git will push the currently checked-out branch to origin, and if the branch doesn't exist on origin yet, it will create it.

So, in your example, if you have issue-435-db-integrity-exception checked out, then git push origin @ will make a new branch with the same name on origin.

Sign up to request clarification or add additional context in comments.

3 Comments

@CraigOtis you could even make it into an alias if you want, something like git config --global alias.magic "push origin -u head", then the usage would be git magic!
Using @ doesn't work for me, but head works. Maybe something might have changed
@ works in git version 2.33.1
0

Just to add to @user456814's answer, if you are using PowerShell you need to escape the @ with a backtick:

# For Git on Powershell git push origin -u `@ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.