0

I'm trying to create a git script cause I'm sick of the workflow and headaches that come with if I forget something.

I've looked at this

Paths with -a does not make sense for bash function

and this

git fatal error Path with a does not make sense

but neither of them seem to apply to me as my code does have modified changes and I do have a string guard around my variable.

I think this might be a product of my lack of familiarity with bash.

This is what I've tried,

function push(){
    BR=`branch | grep \* | cut -d ' ' -f2`
    git commit -am "$BR" " $1"
    git push origin "$BR"
}

The first line is using another command I have that shortens git branch to just branch, so I don't need to add 'git'

I've also tried removing the string around the "$BR" and just keeping it as $BR, which gives me the same error.

I'm getting an error with the commit that says

fatal: Paths with -a does not make sense.

But I know I have modified files that are already tracked by git (by running git diff) so am a bit confused as to why this is showing up

When I try removing the " $1" and keeping everything else the same it works, so I think it has to do with my actual commit message?

5
  • You are missing "git" BR=git branch | grep * | cut -d ' ' -f2`` Commented Apr 22, 2019 at 18:38
  • 1
    How are you calling your function, i.e. what is the value of $1? Commented Apr 22, 2019 at 18:38
  • @EncryptedWatermelon I have another function that shortens git branch to just branch - will edit the question so others know Commented Apr 22, 2019 at 18:39
  • @0x5453 I am calling my function like this push "commit message" Commented Apr 22, 2019 at 18:39
  • At least, with Git 2.22, this error will be easier to debug: stackoverflow.com/a/55884451/6309 Commented Apr 27, 2019 at 20:07

3 Answers 3

1

You're specifying the branch name as the commit message, and your commit message as a filename. Git is reacting to the fact that you are saying "Commit all files but only this file".

If you want to specify both the branch name and your message as the commit message, you have to pass them as a single argument:

git commit -am "$BR $1"
Sign up to request clarification or add additional context in comments.

1 Comment

yup just got it as you posted. Thanks! Will check as correct in 4 min
1

Looking at the docs for git commit (git help commit), when you add the -m flag, it expects the next argument to be the commit message.

-m <msg>, --message=<msg>
    Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

Therefore, git commit -am "$BR" " $1" is incorrect, since it would interpret your branch name as the commit message, and then all remaining args as some path specifier.

Try this instead:

function push(){
    local br=`branch | grep \* | cut -d ' ' -f2`
    git commit -a "$br" -m "$1"
    git push origin "$br"
}

2 Comments

Actually the branch name needs to be a part of the commit message (one of the reasons I'm trying to do this -- sick of forgetting and needing amend previous commits)
I figured it out with your help. I changed the line to git commit -am "$BR $1"
0

The commit message is just one argument so try as follow:

git commit -am "$BR $1"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.