Adam
Adam

Reputation: 3013

Supply argument to git command in bash

I do not know bash very well, I currently have this alias:

alias pushAndTrackBranch="git push -u origin" #Append branchname

Where I type my git branch name manually afterwards .I want to automatically use the current branch so I found:

git branch | grep \* | cut -d ' ' -f2

And tried to combining them as such:

git branch | grep \* | cut -d ' ' -f2 | git push -u origin
git push -u origin | git branch | grep \* | cut -d ' ' -f2
git branch | grep \* | cut -d ' ' -f2 | pushAndTrackBranch
git branch | grep \* | cut -d ' ' -f2 | echo | pushAndTrackBranch

Didn't luck myself into an answer with pipes so I thought as a start in bash_profile I'd assign the branch name to a variable and print it:

function pushAndTrack {
  myBranch=$(grep \* | cut -d ' ' -f2)
  echo myBranch
}

Above is my latest incarnation but it is not correct. How do I fit these two things together? And should I even save the branch name in a variable?

Edit: I see in my function attempt that I forgot part of the command I attempted to save in the variable. It should of course have been:

function pushAndTrack {
  myBranch=$(git branch | grep \* | cut -d ' ' -f2)
  echo $myBranch
}

Upvotes: 0

Views: 89

Answers (1)

Jonathon McMurray
Jonathon McMurray

Reputation: 2991

git push -u origin $(git branch | grep '\*' | cut -d ' ' -f2)

This should do the job. Piping passes the output of previous command to stdin for the next command, rather than as a command line arg.

You may also be able to use xargs (man page)

Upvotes: 2

Related Questions