git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Improved "git add"

From
Linus Torvalds <torvalds@osdl.org>
Date
Sep 18, 2005, 18:27 UTC
Message-ID
<Pine.LNX.4.58.0509181119040.26803@g5.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
Next: Nico -telmich- Schottelius
Message 1 of 3 in “Improved "git add"”
  1. Linus TorvaldsSep 18, 2005
  2. Nico -telmich- SchotteliusSep 18, 2005
  3. Petr BaudisSep 18, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.