2

So I trying to push changes from my local branch to the remote branch with the same name. And im trying to do something like this.

git push origin "git rev-parse --abbrev-ref HEAD" 

The command inside the quotes return the name of the current checked out branch. I'm not sure if this is possible.

Essentially, i'm building an alias that would add, commit and push for me, and it would look something like this:

git config --global alias.fire '!f() { git add -A && git commit -m "$1" && git push origin "git rev-parse --abbrev-ref HEAD"; }; f' 

right now, im just stuck at trying to get the name of the currently checked out branch and use it in another command. Anyone got any suggestion or different approach? Thanks

EDIT: I'm using windows

3
  • What os are you using? Commented Jul 12, 2017 at 20:42
  • @klutt i'm using windows Commented Jul 12, 2017 at 20:44
  • Did my solution work? Commented Jul 12, 2017 at 21:14

1 Answer 1

6

As a commenter said, you can use bash in Windows if you have Git installed, so try that command.

git push origin $(git rev-parse --abbrev-ref HEAD | tail -n 1) 

The output from git rev-parse --abbrev-ref HEAD is

--abrev-ref 4de0cb2e9a86c02ff853630efbae0d2ae777ff47 

I'm piping this into tail -n 1 which is a command to print the last line of input. Putting everything inside $( ... ) takes the output from the command and use as an argument.

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

4 Comments

git for windows has a bash where it's possible to run this.
@Edmundo So what you are saying is that I just wasted 20 minutes of my life learning Powershell? :D
I wouldn't go as far as to say waste... perhaps "not invested wisely" would be more accurate. :-)
this is great. I was able to create alias newpush to push newly created branch. ``` git config alias.newpush "!git push origin $(git rev-parse --abbrev-ref HEAD)" ```

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.