2

Suppose I have a directory with a bunch of files and other directories in git.

If I do

git init .
git add .

I will be adding everything including the directories to my git repository. However, if I only want the files in the current directory to be added (no recursive traversal of directories), is there a non-manual way to do it?

The manual way would be to pick out the files using another tool and running git-add on these files.

8
  • 2
    What's wrong with using another tool? Isn't that what the command line is all about? Commented Jun 7, 2013 at 20:36
  • There's nothing wrong with another tool, but it seems to be such a basic functionality that I think git ought to have it, but I havne't figured out how. Commented Jun 7, 2013 at 20:38
  • 1
    I guess I don't see why the git developers would add something that other tools already do more easily and better; that's just effort & debugging, right? Sort of against the UNIX philosophy, I'd say. Commented Jun 7, 2013 at 20:38
  • Am I missing a requirement here or could you just browse to that directory and do git add *.* which would just grab the files Commented Jun 7, 2013 at 20:42
  • I'm trying to get files in the top-level directory and ignore subdirectories. Commented Jun 7, 2013 at 20:43

4 Answers 4

1

Unfortunately, Git does not have a functionality like that built in. You can easily do it with a shell loop though. Assuming you're using bash, this would work:

#!/bin/bash

for f in `ls -A`; do
    if [ -f $f ]; then
        git add $f
    fi
done

which will add all files from the current directory.

Note that like all bash scripts, you can write this on one line if you only need it once:
for f in $(ls -A); do if [ -f $f ]; then git add $f; fi; done

This is just a proof of concept script and could certainly be improved; e.g. it could first construct a list and then call git add once on that list.

Sign up to request clarification or add additional context in comments.

Comments

1

One option is:

git add --interactive

which will allow you to pick files one by one. Could be onerous; but it would allow you to skip directories. And you've got this:

find . -depth 1 -and ! -type d -exec git add {} \;

Here is an example:

ebg@tsuki(26)$ find . -depth  1 -and ! -type d -print
./a
./b
ebg@tsuki(27)$ find . -depth  1 -and ! -type d -exec git add {} \;
ebg@tsuki(28)$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a
#   new file:   b
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   c/

Comments

1

Something like

find . -maxdepth 1 -type f | xargs git add --

perhaps?

The first part lists all files in the current directory (but not in subdirectories, due to -maxdepth), xargs appends this list as arguments to git add.

You can also try the more robust

find . -maxdepth 1 -type f -exec git add -- \{\} \+

if your version of find supports it. (Or replace \+ by \;, but this will run slower.)

See also: Bash: How to list only files?

2 Comments

That will find all files recursively, including files in the subdirectories which I am trying to exclude.
@merlin2011: That's what -maxdepth is supposed to handle.
0

Depending on how your files and directories are named, there's always the trusty:

git add *.*

1 Comment

you should mentioned that that would leave out all files which contain no dot (extensionless files) and all files starting with a dot (e.g. .gitignore)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.