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

Vibol
  • 615
  • 1
  • 8
  • 25

1 Answers1

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.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 1
    git for windows has a bash where it's possible to run this. – eftshift0 Jul 12 '17 at 21:05
  • @Edmundo So what you are saying is that I just wasted 20 minutes of my life learning Powershell? :D – klutt Jul 12 '17 at 21:07
  • I wouldn't go as far as to say _waste_... perhaps "not invested wisely" would be more accurate. :-) – eftshift0 Jul 12 '17 at 21:14
  • 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)" ``` – hasan.in Jun 02 '22 at 20:02