2

I have the following directories projectdir>subdir>subsubdir and .git folder inside projectdir

When i do git add . from subsubdir the changes inside projectdir are not added to Changes to be committed:

$ git status  
On branch master
Your branch is up to date with 'basicdjango/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   ../../.gitignore
    new file:   .env.example
    modified:   settings.py

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:   ../../.gitignore

So how can i add changes till the projectdir

Santhosh
  • 9,965
  • 20
  • 103
  • 243

1 Answers1

2

. is syntax for "everything in the present directory" (and recursively inside its subdirectories).

So you could either :

  • move to the upper directory before doing your add. Let's just skip this option since avoiding this seemed to be the very source of your question.

  • do the same thing, but ease the process with an alias for your add, embedding a cd before it. I'm not too sure about recommending this one, though, as it could contribute to set lazy habits while being potentially dangerous.

  • refrain from using git add . every time. It's a handy tool but unneccessary in many cases with only a few files or directories. (And using only this shortcut could lead to just ignoring the existence of the index, which is probably not a good practice)

  • use git add -A which adds every file, with the same caveats stated above. (beware of old git versions where this had the same behaviour as git add .)

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • So what should i use instead of git add . – Santhosh Apr 05 '19 at 09:17
  • i think `git add -A` is the solution. I tried it inside inner directories and it add changes of previous folder files – Santhosh Apr 05 '19 at 09:19
  • @SanthoshYedidi Yes, it's technically doing what you want. Just be cautious with it, it can be a source of mistakes. – Romain Valeri Apr 05 '19 at 10:06
  • why source of mistakes – Santhosh Apr 05 '19 at 10:09
  • Adding unwanted things, or being in the wrong directory unwittingly and add to the wrong repo (in some settings with nested repos), among other things. A lot of torek's posts here on SO detailed the benefits of understanding/using appropriately the index (or staging area). You can also take a look at [this interesting post](https://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add) about differences in "global" add syntaxes. – Romain Valeri Apr 05 '19 at 10:20
  • 1
    `git add -u` will add every changes from tracked files, ignoring untracked. In any case, I'd highly recommend to rather use `git add -p` (long version is --patch) to be selective of what you add. I wrote some explanations on my website a while back about it: https://www.ghislain-rodrigues.fr/articles/git-add---interactive.html – padawin Apr 05 '19 at 11:38