GIT IGNORE

Posted By : Shivansh Pandey | 11-Jan-2021

Getting Started
---------------

To rescue this problem GitHub has an option where you can create a file and mentioned the directories or files you don't want to get pushed. You just need to create a file named .gitignore and place that inside your cloned folder. In a Gitignore file, each line defines a pattern. GIt follows the below order of precedence, from highest to lowest, to decide if it has to ignore a path.

- Patterns read from the command line for the supporting commands.
- Same path directories (meaning the directories in the clone folder)
- Patterns read from $GIT_DIR/info/exclude
- Patterns read from the file that the configuration variable core.excludesFile specifies.

 

Now, you might be thinking this all messed up and what files you should ignore apart from ide config files and environment-specific files.  Since the GitHub version is controlled and distributed to other repositories via clone, therefore, files that all developers will want to ignore should go into a .gitignore file.

 

Also Read: A Descriptive Comparison Between Mapbox and Highcharts

 

Pattern Format
---------------------------------

 

- Since a blank line doesn't match any file,  it can serve as a separator for readability.
- Any line that begins with # serves as a comment. You must put a backslash ("\") in front of the first hash for patterns beginning with a hash.
- Unless quoted with a backslash ("\"), trailing spaces are overlooked.
- An optional prefix "!" cancels the pattern; any matching file that got excluded in the previous pattern will be included again. However, if a parent directory of a file is excluded, it is impossible to re-include that file. For performance reasons, Git doesn't list the excluded directories. Hence, no matter what, any patterns on contained files, no matter where they are defined will remain ineffective. For patterns that begin with a "!", add a backslash ("\") in front of the first "!". Like- "\!important!.txt".


- The slash (/) works as a directory separator. Separators may be found at the beginning, middle, or end of the .gitignore search pattern.


Note - Two consecutive asterisks ("**") in patterns matched against full pathname may return a distinctive meaning:

- A leading "**" followed by a slash indicates a match in all directories. We can understand this with a small example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
- 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 means a slash matches zero or more directories. Here's a small example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
- Other consecutive asterisks are considered regular asterisks they will follow the previous rules to match.

 

Also Read: Choosing The Right Data Structure In Swift

 

Examples :
---------------------

$ git status
    [...]
    # Untracked files:
    [...]
    #       Documentation/foo.html
    #       Documentation/gitignore.html
    #       file.o
    #       lib.a
    #       src/internal.o
    [...]

 $ cat .git/info/exclude
    # ignore objects and archives, anywhere in the tree.
    *.[oa]
    $ cat Documentation/.gitignore
    # ignore generated html files,
    *.html
    # except foo.html which is maintained by hand
    !foo.html
    $ git status
    [...]
    # Untracked files:
    [...]
    #       Documentation/foo.html
    [...]

Another Example :
---------------------

 $ cat .gitignore
    vmlinux*
    $ ls arch/foo/kernel/vm*
    arch/foo/kernel/vmlinux.lds.S
    $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore

 

Git cannot ignore arch/foo/kernel/vmlinux.lds.S due to the second .gitignore.

 

Here we have a small example to exclude everything except a specific directory foo/bar. (Note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar

 

Choose Oodles For SaaS App Development Services

 

We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at info@oodlestechnologies.com.

About Author

Author Image
Shivansh Pandey

He is a backend developer having good knowledge of java. He is having experience in Springboot, MQTT, Spring Cloud, etc. He writes optimized codes and having good problem-solving techniques.

Request for Proposal

Name is required

Comment is required

Sending message..