Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git ignore file but not folder with the same name

Tags:

git

gitignore

My code structure is:

.gitignore
my-service << this is a file without an extention
charts
| my-service << this is a folder
| | file1
| | file2

When I write in my gitignore my-service it ignores both my-service the file and my-service the folder. How can I ignore only the file in the root directory, and not the sub-directory?

like image 368
Naguib Ihab Avatar asked Mar 22 '18 04:03

Naguib Ihab


People also ask

Can Gitignore ignore a folder?

. gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

How do I ignore a file name in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Does Gitignore need to be in every folder?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

How do I ignore a file without adding to Gitignore?

To ignore untracked files, you have a file in your git folder called . git/info/exclude . This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.


1 Answers

you can do the reverse way with! in .gitignore

myservice
!myservice/
like image 182
Yu Jiaao Avatar answered Oct 21 '22 16:10

Yu Jiaao