Home > front end >  Easier way to create a .gitignore file
Easier way to create a .gitignore file

Time:04-04

I have deeply nested files that I would like to include in source control and have git track them.

Example is:

Projects/
    .git/
    StrategicProject/
        SpecificProject1/
            Method1/
                doc/
                   *.tex
    .gitignore

In each of the various folders there are other files/folders that I would not like to track. I only want to track, say, all of the .tex files in Projects/StrategicProject/SpecificProject1/Method1/doc/

As of now, to accomplish this, I have in my .gitignore file:

StrategicProject/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/ # don't ignore this
StrategicProject/SpecificProject1/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/ # don't ignore this
StrategicProject/SpecificProject1/Method1/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/doc/ # don't ignore this
StrategicProject/SpecificProject1/Method1/doc/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/doc/*.tex # don't ignore this

The above works, but is prone to error when I create a new folder structure in my working directory. It is also quite cumbersome. To track one set of .tex files in a nested folder needs creation of 8 lines in the .gitignore file. A fellow-SO user created an web-app to ease this specific difficult activity. See here

While that app works fine for now, it only works over the web. More generally, is there a gui app, that works offline (without having to use the internet), that can take a look at my folder structure and allows me to check/uncheck boxes (as part of its user interface) corresponding to files/folders and based on that automatically generate the appropriate .gitignore file?


ETA:

I use VSCode's default Git interface. While that does provide option to right click on a file and add it to .gitignore, that is not what I would like. I would like to not ignore a specific deeply nested set of files. I am open to trying out other editors for this task.

CodePudding user response:

I guess I've solved the problem:

var include = "StrategicProject/SpecificProject1/Method1/doc/*.tex";
var pathParts = include.Split('/');
var sb = new StringBuilder();
sb.AppendLine(pathParts[0]   "/*");
for (var i = 2; i < pathParts.Length; i  )
{
    var path = string.Join('/', pathParts.Take(i));
    sb.AppendLine("!"   path   "/");
    sb.AppendLine(path   "/*");
}
sb.AppendLine("!"   include);
sb.ToString().Dump();

Running this script in https://www.linqpad.net/LINQPad7.aspx (download LinqPad, paste it as Language: C# Statements and press the play button) gives:

StrategicProject/*
!StrategicProject/SpecificProject1/
StrategicProject/SpecificProject1/*
!StrategicProject/SpecificProject1/Method1/
StrategicProject/SpecificProject1/Method1/*
!StrategicProject/SpecificProject1/Method1/doc/
StrategicProject/SpecificProject1/Method1/doc/*
!StrategicProject/SpecificProject1/Method1/doc/*.tex

Of course it misses the step to add it to your gitignore file and it has no nice UI. Ideally it would be accessible via a mouse-click in VS Code in a file. Now you need to paste a string as a value of the include variable. However, now you can generate the pattern offline too :)

Or… another .gitignore file in the deep subdirectory

Note that if you find it OK to have multiple .gitignore files, you could ignore a folder in one .gitignore file and then not-ignore a few files via a .gitignore file that you put in the deep subfolder where those files are. That would require only 2 rules instead of 8.

CodePudding user response:

I find that the easy way to create .gitignore files is to use my editor to create files named .gitignore.

  • Related