2

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

jbk
  • 1,911
  • 19
  • 36
  • 4
    `git add .` is considered harmful, and will eventually result in adding files or changes you didn't mean to add. Paired with an automatic `git commit -m` is particularly problematic. You should instead use `git add -p` or `git add -i`, and take the time to review the changes you're adding. – user229044 Jan 15 '22 at 17:58
  • 3
    `git commit -am` still works. Note that it's roughly equivalent to using `git add -u`, *not* `git add .`; these two are very different. If you want to use two separate Git commands, you need the `!` form of the alias. I personally recommend avoiding `git commit -a` (with or without `-m`) and being very careful with `git add .` (see @meagar's comment). – torek Jan 15 '22 at 17:59
  • 1
    Thanks for the word of warning on `git add` @meagar. And thanks for the `git commit` info @torek. All points noted and I shall now read up and learn more about these git flags and amend my workflow. On reflection and in light of your info, i think that perhaps I was previously using `git add .` then `git commit -am`, not an alias as i thought I may have been. I'll leave this post here in case it might help guide anyone else use git more safely. – jbk Jan 15 '22 at 19:00

1 Answers1

2

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 ("$@").
chepner
  • 497,756
  • 71
  • 530
  • 681
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250