13

My usual git workflow is

<make changes>
git add FILES
git status
git commit -m "some remarks"

where I need the git status just to check if I added the correct files.

Is there a way to tell git to automatically display the status after each git add?

flonk
  • 3,726
  • 3
  • 24
  • 37
  • You could probably create an alias that executes `add` first, and then `status`. E.g. `git config --global alias.ad "!git add $1; git status"`, but I’m not sure if you can keep supporting all the `add` parameters that way. – poke Jul 31 '13 at 11:34

4 Answers4

14

When run with -v (verbose) option, git add outputs the name of files that were added:

» git add -v hello?
add 'hello1'
add 'hello2'
CharlesB
  • 86,532
  • 28
  • 194
  • 218
13

You can use an alias:

[alias]
    gitadd = !sh -c 'git add -- "$@" && git status' --

make changes
$ git gitadd FILES
$ git commit -m "some remarks"

Since git aliases work from the repository root1, you could modify the alias to make it work from any other directory:

[alias]
    gitadd = !sh -c 'cd "$1" && shift && git add -- "$@" && git status' -- 

Now invoke it by saying

git gitadd $PWD file1 file2 ...

1: Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • 1
    The `$@` was what I was looking for! Thanks ^^ – poke Jul 31 '13 at 11:43
  • 1
    it would be `git gitadd` with that alias, imho – mnagel Jul 31 '13 at 12:13
  • @devnull I added the above lines to ~/.gitconfig but when typing `git gitadd somefile` it says `Nothing specified, nothing added. Maybe you wanted to say 'git add .'?` – flonk Jul 31 '13 at 13:15
  • @flonk That's odd. What if you say `$*` instead of `$@` in the alias. – devnull Jul 31 '13 at 13:16
  • @devnull Ooops, unfortunately it only works on the repository's root folder. i.e. `git gitadd somedir/somefile` works, but `git gitadd somefile` from inside `somedir`fails telling me `fatal: pathspec 'somefile' did not match any files` – flonk Sep 17 '13 at 13:13
  • @devnull What is even more strange, when doing `git gitadd somedir/somefile` from *inside* `somedir`, it works. Your solution seems to require filenames relative to the repository root. – flonk Dec 20 '13 at 09:29
  • @devnull couldn't one tell the alias to `cd` to the place from which it was called before performing `git add ...`? – flonk Dec 20 '13 at 13:36
  • 1
    @flonk Figured a workaround. Modify the alias to `gitadd = !sh -c 'cd $1 && shift && git add -- "$@" && git status' --` and invoke it by saying `git gitadd $PWD file1 file2 ...`. – devnull Dec 20 '13 at 14:27
3

Here's what I have using the answer at https://stackoverflow.com/a/26243454 and combining it with devnull's answer:

[alias]
    sadd = !sh -c 'cd -- ${GIT_PREFIX:-.} && git add -- "$@" && git status' --

This way you don't need to pass in the working directory manually.

Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72
0

You can do it via creating an alias that executes add first and then status using git config commnad ..

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105