Ignore everything except certain files in Git
This article outlines the steps to make Git ignore all but a few files in a Git repository. The .gitignore file is a useful Git utility that allows us to tell Git which files to track and which files not to track.
If you want your .gitignore file to ignore everything except a few files, stay put, we have a lot of files to unpack.
Ignore everything except certain files in Git
Take this hypothetical situation as an example.
We have over a dozen files added to our repository and only want to share our work on files, namely run.py, index.html, and package.json.
One way is to add these files to the index using the git add command. Another way involves the .gitignore file.
How do we tell gitignore to ignore everything except these three files?
If the file is in the root directory, we can edit our .gitignore file as shown below.
# Ignore everything
/*
# Except these files
!.gitignore
!run.py
!index.html
!package.json
This will make Git ignore everything except the above files. What if our files are in a subdirectory?
Let's say our files are all in a directory called apps and there are other files in the same directory. This is what our .gitignore file looks like.
# Ignore everything
/*
# do not ignore the .gitignore file
!.gitignore
#do not ignore the apps directory
!/apps
# ignore everything in the apps directory
/apps/*
# do not ignore these files in the apps directory
!/apps/run.py
!/apps/index.html
!/apps/package.json
In short, we can do this by adding !
in .gitignore
the file, as shown in the example above.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
在 Git 中忽略除某些文件之外的所有内容
Publish Date:2023/04/01 Views:92 Category:Git
-
本文概述了使 Git 忽略 Git 存储库中除少数文件之外的所有文件的步骤。 .gitignore 文件是一个有用的 Git 实用程序,它允许我们告诉 Git 要跟踪的文件和不跟踪的文件。