0

I'm trying to initialize a new git repo with a handful of previously created files and

git add .

is not adding the files. I double checked all the usual suspects .gitignore with nothing turning up. Then I tried making a slight modification to one of the files and then adding would work. My hypothesis is when I made a change to the file it receives a new modification date. Or my other hypothesis is that my slight change, changes the hash of the file so git recognizes it as a new file to be added?

I really don't want to go in a touch every file so git will pick it up. Does anyone know of another solution?

2
  • Could you show us git status and the content of your .gitignore? Also why do you believe git add . is not adding the files? It's normal for git add to report nothing when it works. Commented May 14, 2018 at 19:04
  • Are you sure you are not invoking it from some subdirectory? Otherwise, Schwern's answer covers git add pretty thoroughly. Commented May 14, 2018 at 19:32

1 Answer 1

2

What I suspect happened is git add . worked just fine. Here's how that would normally go.

$ git init bar
Initialized empty Git repository in /Users/schwern/tmp/bar/.git/
$ cd bar
$ touch foo bar baz
$ git add .

Note that git add . didn't say anything, it just worked. We can check with git status.

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   bar
    new file:   baz
    new file:   foo

Anything in Changes to be committed is in the "staging area" (aka the "index" or "cache"). This is where you build your next commit and is fairly unique to Git.

Whereas other version control systems will commit any changes to any tracked file, Git will only commit what you've copied to the staging area with git add. When you git commit it's the content of the staging area that will be committed.

git add does two things: it tells the version control system to "track" the file, and then it copies the whole file to the staging area. The tracking sticks, but the copy only happens once.

Or my other hypothesis is that my slight change, changes the hash of the file so git recognizes it as a new file to be added?

Almost. Git must be told to track files. Once tracked, it will notice changes via the file's hash/checksum, but it will not automatically add those changes.

Here's an illustration.

$ echo 'Basset hounds got long ears' >> foo
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   bar
    new file:   baz
    new file:   foo

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   foo

Note that foo is both in the staging area and it's modified "not staged for commit". That's because git add copied foo to the staging area, and it does not automatically copy the new changes. If you git commit now you won't get those changes, they have to be added to the staging area.

$ git add foo
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   bar
    new file:   baz
    new file:   foo

This might seem tedious, and it is. Once you've tracked the files you can commit all changes with git commit -a. This commits all changes to all tracked files.


The staging area can be annoying at first, but once you've learned to use it it can become a powerful tool to slice large commits up into smaller ones.

Let's say you've been working and you find that you've done a lot of changes. Too many for a single commit. Maybe you've added fixed some doc typos, fixed a few small bugs, renamed a method, and added a feature. You can use the staging area to pull this apart into multiple commits.

git add -p allows you to add changes hunk by hunk. So you could add just the doc typo fixes and commit them. Then just the small bug fixes and commit those. Then add just the method renaming, and commit. Finally you've got just your feature changes as a clean, easy to understand commit.

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

Comments

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.