2

I have a repository stored locally and on GitHub,

This repository is used to store the contents of my website,

After signing up to a CMS and it creating a hidden file in the GitHub directory I changed my mind and wanted to delete the hidden files.

I had performed

git pull origin master

In order to sync my local files with the files on GitHub, I then removed the hidden files locally and ran the following hoping it would sync the latest file with my local directory.

git add *
git commit -m "Removed CMS hidden files"
git push -u origin master

However, when doing this I get the following

    On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
    deleted:    .forestry/front_matter/templates/catalogue-entry.yml
    deleted:    .forestry/settings.yml

Untracked files:
    .DS_Store

no changes added to commit

My desired outcome is to what I have locally synced exactly with my GitHub directory removing the hidden files added by the CMS.

Henry James
  • 143
  • 3
  • 14

1 Answers1

6

TL;DR; Use git add [--all|-A] or git add [.|-A .] or git add -u or git add '*', instead of git add *. One of them might work (use git status to verify which works).


From this answer and this answer:

  • git add --all or git add -A stages all changes on the whole working tree
  • git add -u stages modifications and deletions, without new files
  • In git version 1.x, git add . staged new files and modifications, without deletions - on the whole working tree.
  • From git 2.0, git add . (or git add -A .) however is to stage new/modified/deleted files (all changes) - but only on the current directory (./ directory)
  • To get the old behavior of git add ., we've to use --ignore-removal

About git add *, from another answer:

  • In git, add * the * is interpreted by the shell, and has nothing to do with Git. If your shell is Bash, files and directories starting with . (hidden files on Linux) will not be matched by *, so they will not be passed to git add, and will not get added to the index.

  • If git add * is being run in a Windows non-bash interpreter, the * however will be passed literally to git add. In this case files and directories whose names start with . will get added.

So the behavior differs from Windows and Linux for git add *, and the OP probably used a Linux/ git with a bash shell system, that's why he got the message:

 Changes not staged for commit:
    deleted:    .forestry/front_matter/templates/catalogue-entry.yml
    deleted:    .forestry/settings.yml

after did git add *, yet because (with bash shell) add * didn't match the files start with ..

Encourage you guys to read this answer to get more detail about git add *.

Conclusion: Depends on cases (which directory you're on, whether you want to delete or add or modify, etc) one of my suggestions above might work, but git add [--all,-A] happens to be the safest one to stage all changes include hidden files on the whole working tree - whole repository.

Community
  • 1
  • 1
Loi Nguyen Huynh
  • 8,492
  • 2
  • 29
  • 52
  • 1
    `git add -u` is the safest way to add modifications and deletions without new files, it shouldn't be part of the "might" work set of commands. – user229044 Dec 23 '19 at 02:22
  • 1
    I wrote "might" work because I haven't tested it yet, yet I'm not super confidently comfortable with git so I wrote "might". About `git add --all`, the OP used it and it did work for him, so I wrote: "will work". If you see you can improve my answer, please do me the favor of giving me an edit suggestion. Thank you. – Loi Nguyen Huynh Dec 23 '19 at 02:29
  • 1
    Seemingly the `git add .` only stages changes of the files **in the current folder** *(`.` folder)* instead of *new files and modifications* as I said above. – Loi Nguyen Huynh Dec 30 '19 at 13:53