Search code examples
gitgit-alias

Passing an argument to git alias not working


I created the following alias on Windows 10 to issue a commit -am <commit message> command. I used the the $1 token to register an argument, but it is not working.

git config --global alias.cam 'commit -am $1'

Issuing the following command returns the error message below:

git cam "test commit"

Error Message:

fatal: paths 'test commit ...' with -a does not make sense

How do I make this work? I researched how to use arguments in git aliases, but none of the resources offers a simple solution to my problem. Thanks.


Solution

  • This is the rule for Git beginners: do not use git commit -a.

    This is the rule for advanced Git users: do not use git commit -a unless:

    • you would run git add -u before git commit, and
    • you know that the repository is not using a badly written pre-commit script that will misbehave in the presence of git commit -a.

    From your comment:

    @jthill That works, but now it just displays the untracked files when I issue git cam "test commit". Nothing is staged or committed. If I issue the add and commit commands separately, it works fine. Why is that?

    The problem here is that git commit -a is not like git add followed by git commit. It's more like git add -u followed by git commit (but even then, still not exactly the same). Specifically, git add -u will only update files that Git already knows about. The u in -u stands for update, i.e., don't add any untracked files, but do update all tracked ones as appropriate.

    You have an untracked file that you'd like to add. You must use git add without the -u option to do this. (Technically there are several other commands that could get you there, but git add is the one to use.)