# Improved "git add"

3 messages from 2005-09-18 to 2005-09-18. Participants: Linus Torvalds, Nico -telmich- Schottelius, Petr Baudis.
Thread: https://gitlist.dev/t/1852

## Linus Torvalds, 2005-09-18 18:27

Subject: Improved "git add"
Message-ID: <Pine.LNX.4.58.0509181119040.26803@g5.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0509181119040.26803%40g5.osdl.org

```

This fixes everybodys favourite complaint about "git add", namely that it 
doesn't take directories.

We use "git-ls-files --others" to generate an arbitrary list of filenames, 
and thus also automatically honor ignore-files while we're at it.

Side note: there's a lot of room for improvement here. In particular, if
we have a long list of filenames (importing a big archive), this will just
do a big stupid for-loop and add them one at a time. Maybe it should use

	generate-list | xargs -0 git-update-idex --add --

instead.

Also, I think we should have a default ignore list if we don't find a 
.git/info/exclude file. Ignoring "*.o" and ".*" by default would probably 
be the right thing to do.

But I think this is a good first step.

Use the "-n" flag to just show the list of files to be added without 
adding them. 

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

NOTE! This depends on the previous patch that added "--git-dir" to 
git-rev-parse

diff --git a/git-add.sh b/git-add.sh
--- a/git-add.sh
+++ b/git-add.sh
@@ -1,2 +1,32 @@
 #!/bin/sh
-git-update-index --add -- "$@"
+
+show_only=
+verbose=
+while : ; do
+  case "$1" in
+    -n)
+	show_only=true
+	verbose=true
+	;;
+    -v)
+	verbose=true
+	;;
+    *)
+	break
+	;;
+  esac
+  shift
+done
+
+GIT_DIR=$(git-rev-parse --git-dir) || exit
+global_exclude=
+if [ -f "$GIT_DIR/info/exclude" ]; then
+   global_exclude="--exclude-from=$GIT_DIR/info/exclude"
+fi
+for i in $(git-ls-files --others \
+	$global_exclude --exclude-per-directory=.gitignore \
+	"$@")
+do
+   [ "$verbose" ] && echo "  $i"
+   [ "$show_only" ] || git-update-index --add -- "$i" || exit
+done

```

## Nico -telmich- Schottelius, 2005-09-18 21:29

Subject: Re: Improved "git add"
Message-ID: <20050918212904.GB1463@schottelius.org>
URL: https://gitlist.dev/e/20050918212904.GB1463%40schottelius.org
In-Reply-To: <Pine.LNX.4.58.0509181119040.26803@g5.osdl.org>

```
Linus Torvalds [Sun, Sep 18, 2005 at 11:27:45AM -0700]:
> [git-add/directory fix]

Nice, first cogito and now git itself. You are pretty fast.

> [...]
> Also, I think we should have a default ignore list if we don't find a 
> .git/info/exclude file. Ignoring "*.o" and ".*" by default would probably 
> be the right thing to do.

I do use doc/.warning or similar in some projects files for outputting
a warning when somebody simply types 'make', so he'll get instructions
on what to do.

Other people may even want to include *.o in their tree, because it's
used by their program for another purpose.

I do not think that a VCS should ignore anything from itself. If we
generate a standard .git/info/excludes (or whatever the name of it is)
and write those standard values in there, this is a different thing.

Nico

-- 
Latest project: cconfig (http://nico.schotteli.us/papers/linux/cconfig/)
Open Source nutures open minds and free, creative developers.

```

## Petr Baudis, 2005-09-18 22:17

Subject: Re: Improved "git add"
Message-ID: <20050918221702.GE22391@pasky.or.cz>
URL: https://gitlist.dev/e/20050918221702.GE22391%40pasky.or.cz
In-Reply-To: <20050918212904.GB1463@schottelius.org>

```
Dear diary, on Sun, Sep 18, 2005 at 11:29:04PM CEST, I got a letter
where Nico -telmich- Schottelius <nico-linux-git@schottelius.org> told me that...
> Linus Torvalds [Sun, Sep 18, 2005 at 11:27:45AM -0700]:
> > [git-add/directory fix]
> 
> Nice, first cogito and now git itself. You are pretty fast.
> 
> > [...]
> > Also, I think we should have a default ignore list if we don't find a 
> > .git/info/exclude file. Ignoring "*.o" and ".*" by default would probably 
> > be the right thing to do.
> 
> I do use doc/.warning or similar in some projects files for outputting
> a warning when somebody simply types 'make', so he'll get instructions
> on what to do.
> 
> Other people may even want to include *.o in their tree, because it's
> used by their program for another purpose.
> 
> I do not think that a VCS should ignore anything from itself. If we
> generate a standard .git/info/excludes (or whatever the name of it is)
> and write those standard values in there, this is a different thing.

But .git/info/exclude mostly sucks, you really want these kinds of rules
in .gitignore if anywhere. E.g. CVS by default ignores some stuff and it
seems as a fine strategy for me, and is working out in the vast majority
of cases. You can unignore files by doing '!pattern', so if you want to
throw away all the defaults, just specify '!*'.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
If you want the holes in your knowledge showing up try teaching
someone.  -- Alan Cox

```
