5

Here are the git commands which I have typed

$ git add -u -n
add 'proj1/Foo.scala'
$ git add .
$ git add .
$ git commit -m "message"
On branch feature/branch
Your branch is up-to-date with 'origin/feature/branch'.
Changes not staged for commit:
    modified:   ../proj1/Foo.scala

So why did I get the Changes not staged for commit? as you can see that I did git add . twice

Now if I got ahead and do

git add ../proj1/Foo.scala

and then do commit it works. Why should I do each file specifically rather than just do git add .

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
  • I usually do a `git add *` instead ... Only if you KNOW you want to add ALL the added/changed files in the current working directory .. But usually you know what you want to add to your commit. And -- You could shorten by committing and adding at the same time using `git commit -a -m "message"` which does the same thing as a `git add *` – Zak Mar 29 '16 at 17:39
  • 1
    ```git add .``` will only add files in the current directory (aka ```.```). From the path in your copy/pasted commands, it looks like proj1 is up a directory? – BlueSpaceCanary Mar 29 '16 at 17:40
  • Why does it appear the file has changed location from one command to the next? When you dry-run the add, it shows the file location as `proj1/Foo.scala`, one directory below the current. Why then does the output later show the location as `../proj1/Foo.scala`? – TriskalJM Mar 29 '16 at 17:41
  • 1
    @TriskalJM It looks like they're in a subdirectory in the git repo. Since the first one used -u without a path, it looked at the whole repository, so it printed the path relative to the top of the repo. But commit shows paths relative to your current location. – BlueSpaceCanary Mar 29 '16 at 17:44

2 Answers2

13

git add . by default will add files changed in current working directory and its subdirectories only.

If you want to add all files, use git add -A (this works in the latest versions of git).

Alternatively, as pointed by @Zak in comments, you can use git commit -am "commit message" to do this in a single step.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
4

I had a similar problem when git add <filename> did not work.
I cd down to the directory and did git add there and file was added as expected.

LosManos
  • 7,195
  • 6
  • 56
  • 107