Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use leading slash in gitignore

Tags:

git

gitignore

People also ask

What does double asterisk mean in Gitignore?

A trailing " /** " matches everything inside. For example, " abc/** " matches all files inside directory " abc ", relative to the location of the . gitignore file, with infinite depth. A slash followed by two consecutive asterisks then a slash matches zero or more directories.

What should I ignore in Gitignore?

gitignore only works for ignoring files that are not being tracked by Git yet. Adding an already tracked file to . gitignore will not prevent you from commiting changes to that file.

How do I use wildcards in Gitignore?

. 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 mark a file as Gitignore?

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.


Just wanted to summarize for possible quick future reference -- the leading slash anchors the match to the root. Thus, in the example below, without the slash, the wildcard would also exclude everything within foo because it would take * and move recursively down the tree. However, with /*, it excludes everything except folder foo and its contents:

$ cat .gitignore
/*
!/foo

You've answered your own question entirely. If you look at the github/gitignore repo more closely, you'll see most files use inconsistent rules about how patterns are written; it's very likely most were contributed by people who didn't bother to read the documentation nor test things out as you did.

So if that helps: You're right, be confident.

If you see mistakes in collaborative projects such as this, don't hesitate to contribute your knowledge. There's even some precedent if you need to build up your confidence further.