Как добавить папку в gitignore
Перейти к содержимому

Как добавить папку в gitignore

  • автор:

Как задать папку в gitignore?


Файл .gitignore, как правило, является скрытым, поэтому найдите в вашем проекте его.

Затем, чтобы указать имя той папки, которую необходимо исключить из отслеживания вам нужно поставить название папки или путь до неё.

node_modules # Игнорируется папка node_modules

.local # Игнорируется файл .local

someFile.txt # Игнорируется файл someFile.txt

notes/main_page.txt # Здесь игнорируется только файл main_page.txt

*.exe # Игнорировать все файлы с разрешением .exe

Ссылка на документацию
Для более глубокого понимания советую прочитать эту статью

.gitignore File – How to Ignore Files and Folders in Git

Dionysia Lemonaki

Dionysia Lemonaki

.gitignore File – How to Ignore Files and Folders in Git

Git is a popular version control system. It is how developers can collaborate and work together on projects.

Git allows you to track the changes you make to your project over time. On top of that, it lets you revert to a previous version if you want to undo a change.

The way Git works is that you stage files in a project with the git add command and then commit them with the git commit command.

When working on a project as part of a team, there will be times when you don’t want to share some files or parts of the project with others.

In other words, you don’t want to include or commit those specific files to the main version of the project. This is why you may not want to use the period . with the git add command as this stages every single file in the current Git directory.

When you use the git commit command, every single file gets committed – this also includes files that do not need to be or shouldn’t be.

You may instead want Git to ignore specific files, but there is no git ignore command for that purpose.

So, how do you tell Git to ignore and not track specific files? With a .gitignore file.

In this article, you will learn what a .gitignore file is, how to create one, and how to use it to ignore files and folders. You will also see how you can ignore a previously committed file.

Here is what we will cover:

What Is a .gitignore File? What Is a .gitignore File Used For?

Each of the files in any current working Git repository is either:

  • tracked – these are all the files or directories Git knows about. These are the files and directories newly staged (added with git add ) and committed (committed with git commit ) to the main repo.
  • untracked – these are any new files or directories created in the working directory but that have not yet been staged (or added using the git add command).
  • ignored – these are all the files or directories that Git knows to completely exclude, ignore, and not be aware of in the Git repository. Essentially, this is a way to tell Git which untracked files should remain untracked and never get committed.

All ignored files get stored in a .gitignore file.

A .gitignore file is a plain text file that contains a list of all the specified files and folders from the project that Git should ignore and not track.

Inside .gitignore , you can tell Git to ignore only a single file or a single folder by mentioning the name or pattern of that specific file or folder. You can also tell Git to ignore multiple files or folders using the same method.

How to Create a .gitignore File

Typically, a .gitignore file gets placed in the root directory of the repository. The root directory is also known as the parent and the current working directory. The root folder contains all the files and other folders that make up the project.

That said, you can place it in any folder in the repository. You can even have multiple .gitignore files, for that matter.

To create a .gitignore file on a Unix-based system such as macOS or Linux using the command line, open the terminal application (such as Terminal.app on macOS). Then, navigate to the root folder that contains the project using the cd command and enter the following command to create a .gitignore file for your directory:

Files with a dot ( . ) preceding their name are hidden by default.

Hidden files are not visible when using the ls command alone. To view all files – including hidden ones – from the command line, use the -a flag with the ls command like so:

What to Include in a .gitignore File

The types of files you should consider adding to a .gitignore file are any files that do not need to get committed.

You may not want to commit them for security reasons or because they are local to you and therefore unnecessary for other developers working on the same project as you.

Some of these may include:

  • Operating System files. Each Operating System (such as macOS, Windows, and Linux) generates system-specific hidden files that other developers don’t need to use since their system also generates them. For example, on macOS, Finder generates a .DS_Store file that includes user preferences for the appearance and display of folders, such as the size and position of icons.
  • Configuration files generated by applications such as code editors and IDEs (IDE stands for Integrated Development Environment). These files are custom to you, your configurations, and your preferences settings.
  • Files that get automatically generated from the programming language or framework you are using in your project and compiled code-specific files, such as .o files.
  • Folders generated by package managers, such as npm’s node_modules folder. This is a folder used for saving and tracking the dependencies for each package you install locally.
  • Files that contain sensitive data and personal information. Some examples of such files are files with your credentials (username and password) and files with environment variables like .env files ( .env files contain API keys that need to remain secure and private).
  • Runtime files, such as .log files. They provide information on the Operating System’s usage activities and errors, as well as a history of events that have taken place within the OS.

How to Ignore a File and Folder in Git

If you want to ignore only one specific file, you need to provide the full path to the file from the root of the project.

For example, if you want to ignore a text.txt file located in the root directory, you would do the following:

And if you wanted to ignore a text.txt file located in a test directory at the root directory, you would do the following:

You could also write the above like so:

If you want to ignore all files with a specific name, you need to write the literal name of the file.

For example, if you wanted to ignore any text.txt files, you would add the following to .gitignore :

In this case, you don’t need to provide the full path to a specific file. This pattern will ignore all files with that particular name that are located anywhere in the project.

To ignore an entire directory with all its contents, you need to include the name of the directory with the slash / at the end:

This command will ignore any directory (including other files and other sub-directories inside the directory) named test located anywhere in your project.

Something to note is that if you write the name of a file alone or the name of the directory alone without the slash / , then this pattern will match both any files or directories with that name:

What if you want to ignore any files or directories that start with a specific word?

Say that you want to ignore all files and directories that have a name starting with img . To do this, you would need to specify the name you want to ignore followed by the * wildcard selector like so:

This command will ignore all files and directories that have a name starting with img .

But what if you want to ignore any files or directories that end with a specific word?

If you wanted to ignore all files that end with a specific file extension, you would need to use the * wildcard selector followed by the file extension you want to ignore.

For example, if you wanted to ignore all markdown files that end with a .md file extension, you would add the following to your .gitignore file:

This pattern will match any file ending with the .md extension located anywhere in the project.

Earlier, you saw how to ignore all files ending with a specific suffix. What happens when you want to make an exception, and there is one file with that suffix that you don’t want to ignore?

Say you added the following to your .gitignore file:

This pattern ignores all files ending in .md , but you don’t want Git to ignore a README.md file.

To do this, you would need to use the negating pattern with an exclamation mark, ! , to negate a file that would otherwise be ignored:

With both of those patterns in the .gitignore file, all files ending in .md get ignored except for the README.md file.

Something to keep in mind is that this pattern will not work if you ignore an entire directory.

Say that you ignore all test directories:

Say that inside a test folder, you have a file, example.md , that you don’t want to ignore.

You cannot negate a file inside an ignored directory like so:

How to Ignore a Previously Committed File

It’s a best practice to create a .gitignore file with all the files and the different file patterns you want to ignore when you create a new repository – before committing it.

Git can only ignore untracked files that haven’t yet been committed to the repository.

What happens when you have already committed a file in the past and wish you hadn’t?

Say you accidentally committed a .env file that stores environment variables.

You first need to update the .gitignore file to include the .env file:

Now, you will need to tell Git not to track this file by removing it from the index:

The git rm command, along with the —cached option, deletes the file from the repository but does not delete the actual file. This means the file remains on your local system and in your working directory as an ignored file.

A git status will show that the file is no longer in the repository, and entering the ls command will show that the file exists on your local file system.

If you want to delete the file from the repository and your local system, omit the —cached option.

Next, add the .gitignore to the staging area using the git add command:

Finally, commit the .gitignore file using the git commit command:


And there you have it – you now know the basics of ignoring files and folders in Git.

Ignoring files

You can configure Git to ignore files you don’t want to check in to GitHub.

Configuring ignored files for a single repository

You can create a .gitignore file in your repository’s root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the .gitignore file in to your repository.

GitHub maintains an official list of recommended .gitignore files for many popular operating systems, environments, and languages in the github/gitignore public repository. You can also use gitignore.io to create a .gitignore file for your operating system, programming language, or IDE. For more information, see «github/gitignore» and the «gitignore.io» site.

Open Terminal Terminal Git Bash .

Navigate to the location of your Git repository.

Create a .gitignore file for your repository.

If the command succeeds, there will be no output.

For an example .gitignore file, see «Some common .gitignore configurations» in the Octocat repository.

If you want to ignore a file that is already checked in, you must untrack the file before you add a rule to ignore it. From your terminal, untrack the file.

Configuring ignored files for all repositories on your computer

You can also create a global .gitignore file to define a list of rules for ignoring files in every Git repository on your computer. For example, you might create the file at

/.gitignore_global and add some rules to it.

  1. Open Terminal Terminal Git Bash .
  2. Configure Git to use the exclude file

Excluding local files without creating a .gitignore file

If you don’t want to create a .gitignore file to share with others, you can create rules that are not committed with the repository. You can use this technique for locally-generated files that you don’t expect other users to generate, such as files created by your editor.

Use your favorite text editor to open the file called .git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

  1. Open Terminal Terminal Git Bash .
  2. Navigate to the location of your Git repository.
  3. Using your favorite text editor, open the file .git/info/exclude.
    in the Git documentation in the Git documentation in the github/gitignore repository site

Help us make these docs great!

All GitHub docs are open source. See something that's wrong or unclear? Submit a pull request.

How to add a folder to the .gitignore

Maybe my Googling skills are lacking but this seems like a question that should give me back thousands of hits and yet, I can’t find a straight answer.

I do constant pushes to github to share with a remote developer. We both have npm and bower installed. No need to push these huge folders to github all the time. My node_modules are ignored. But I can’t seem to get gitignore to ignore my bower_components folder

I’m not too savvy on cmd, I barely scratch the surface so if you ar going to suggest that, please don’t assume I know what you are talking about. Otherwise if it is as easy as adding it to the file itself using an IDE, well, I did that and no cigar. Here is my .gtignore file for your review

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *