Home > Software design >  correct syntax for a .gitconfig alias to git add all files and then git commit -m?
correct syntax for a .gitconfig alias to git add all files and then git commit -m?

Time:01-18

I've lost my .gitconfig aliases in an OS reload on my machine and I failing to set up and alias for a command that I used to run on my old machine.

I used to run git commit -am 'commit message here' in terminal and it would add all files for me, git add ., and then git commit -m for me with the string given to the alias -am command passed in as the commit message. How should I correctly set up and alias to recreate this behaviour on my new machine?

I've played around with various syntax, such as:

am = "git add . && git commit -m"

or am = "!git add . && git commit -m"

But these are not working so must be incorrect. Can anyone advise as to how best to do this? Thanks

CodePudding user response:

If you really want to use git add ., you can define a .bashrc function or a git alias.

But as noted, especially with git add ., it is always best to do a git status and double-check what you are actually adding to your index, before making any commit.

You can therefore consider another alias to Automatically show status after git add.

[alias]
    sadd = !f() { cd -- "${GIT_PREFIX:-.}" && git add -- "$@" && git status; }; f

If GIT_PREFIX is not defined (because you are executing the alias from the root folder of the repo), it will be replaced by '.'.
When you execute an alias from the root folder, GIT_PREFIX is not defined. Hence the need to use '.' (current folder).

  • Once you are at the Git repository root folder, git add -- "$@" will add everything (if no parameters passed), or only what you pass as parameters ("$@").
  •  Tags:  
  • Related