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.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • 2
    What's wrong with using another tool? Isn't that what the command line is all about? – Carl Norum Jun 07 '13 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. – merlin2011 Jun 07 '13 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. – Carl Norum Jun 07 '13 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 – Midpipps Jun 07 '13 at 20:42
  • I'm trying to get files in the top-level directory and ignore subdirectories. – merlin2011 Jun 07 '13 at 20:43
  • I might be missing something but that is what it does on my system. It just grabs the files of the current directory you are in and ignores everything else but might be that I am using the windows version of git and have no way to test it on linux until later – Midpipps Jun 07 '13 at 20:49
  • @Midpipps that would leave out all files which contain no dot (extensionless files) and all files starting with a dot (e.g. `.gitignore`) – Nevik Rehnel Jun 07 '13 at 20:50
  • Ah never thought about that sorry – Midpipps Jun 07 '13 at 20:53

4 Answers4

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/
GoZoner
  • 67,920
  • 20
  • 95
  • 145
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.

Nevik Rehnel
  • 49,633
  • 6
  • 60
  • 50
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?

Community
  • 1
  • 1
krlmlr
  • 25,056
  • 14
  • 120
  • 217
0

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

git add *.*
Rory Hunter
  • 3,425
  • 1
  • 14
  • 16
  • 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`) – Nevik Rehnel Jun 07 '13 at 20:54