0

I previously ignored a folder, but now I would like to ignore its content only: How do I remove/modify some entries from my .gitignore file so that git will track them again? I tried updating the gitignore file and adding the folder with git add -f <folder-name>, but it didn't work.

Thank you for your help

3 Answers3

3

What do you mean by ignoring its content only, If you want to track an empty directory?

there is way to make a directory stay (almost) empty (in the repository) is to create a .gitignore file inside that directory that contains these four lines:

# Ignore everything in this directory
*
# Except this file
!.gitignore
Muhammad Taseen
  • 519
  • 7
  • 22
2

Let us say you have one folder named target under the root folder that you added into your .gitignore file. The folder structure is similar to the one below:

-project root
  |- src
       |-- your src files here
  |- target
       |- file1
       |- file2
  |-.gitignore

I assume you want to undo git ignore and selectively ignore file inside the directory that you added to ignore list earlier. Let's suppose you want to add file 1 only from the directory structure above onto git ignore. In this case, edit the .gitignore file to remove the /target/ folder pattern and add a new . gitignore inside the target folder with file1 as content. It should automatically pick up only file2 for tracking.

-project root
  |- src
       |-- your src files here
  |- target
       |- file1
       |- file2
       |- .gitignore - add file1 here
  |-.gitignore - remove /target/ from here
Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
  • Thank you for your detailed answer, however I've accepted Muhammad Taseen's one since it is more "straight forward" in relation to my problem! – joyfantastic Feb 17 '21 at 12:54
  • Glad to see that you found it useful. You are right. The accepted answer is a more straightforward approach and I have upvoted it too to express my solidarity:) – Kavitha Karunakaran Feb 17 '21 at 14:11
  • 1
    Thank you for you both, Kavitha your answer is pretty detailed, I've upvoted your answer :) – Muhammad Taseen Feb 18 '21 at 00:16
1

git doesn't track folders, ignoring a folder or ignoring its contents is the same thing when it comes to git.

That said, it should be sufficient to remove the folder-entry from .gitignore to start track things in that folder.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78