How can I `git add` on cloned git repo by SSH?

49 Views Asked by At

I can git add just add new file on repo directory. but when I make directory and add new file to this directory, I cannot git add.

Note:

  • I ran git clone for clone private repo by ssh public key
$ git clone [email protected]:nyusername/mygitrepo.git
$ cd mygitrepo && ls
.gitignore
README.md
requirements.txt
my_dir
  • my_dir: directory that originally existed in repo
  • .gitignore : only have one line *.pyc

case 1: add new file where existed directory path.

$ cd my_dir && cat <<EOF > "tmp_file.json"
{
    "foo": bar
}
EOF
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        my_dir/tmp_file.json

nothing added to commit but untracked files present (use "git add" to track)

I can see that I can git add my_dir/tmp_file.json

$ rm tmp_file.json
$ cd .. && ls
.gitignore
README.md
requirements.txt
my_dir

delete my_dir/tmp_file.json

case 2: make dir and add new file in here.

$ mkdir tmp_dir && cd tmp_dir
$ cat <<EOF > "tmp_file.json"
{
    "foo": bar
}
EOF

run git status

$ cd .. && git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        tmp_dir/

nothing added to commit but untracked files present (use "git add" to track)

only I can git add tmp_dir/, can't git add tmp_dir/tmp_file.json

what I need help:

  • Why did this happen?
  • How can I git add with make dir and file in here?
1

There are 1 best solutions below

2
Schwern On

Don't panic, those are just hints.

In the first instance, my_dir/ already has tracked files in it. So Git is telling you which specific files in my_dir/ are untracked.

In the second instance, tmp_dir/ is brand new. All files within are untracked. So Git is telling you everything in tmp_dir/ is untracked.

In either case, you can git add the whole directory (really all the files within that directory; git does not track directories) to add all the untracked and changed files within.