Skip to main content
ubuntuask.com

Back to all posts

How to Exclude Files From A Git Commit?

Published on
6 min read
How to Exclude Files From A Git Commit? image

Best Git Management Tools to Buy in October 2025

1 Learning Git: A Hands-On and Visual Guide to the Basics of Git

Learning Git: A Hands-On and Visual Guide to the Basics of Git

BUY & SAVE
$34.92 $45.99
Save 24%
Learning Git: A Hands-On and Visual Guide to the Basics of Git
2 Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development

Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development

BUY & SAVE
$43.23 $65.99
Save 34%
Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development
3 Professional Git

Professional Git

BUY & SAVE
$24.79 $52.00
Save 52%
Professional Git
4 Version Control with Git: Powerful tools and techniques for collaborative software development

Version Control with Git: Powerful tools and techniques for collaborative software development

  • HIGH-QUALITY USED BOOKS AT A FRACTION OF RETAIL PRICES!
  • THOROUGHLY CHECKED FOR GOOD CONDITION-GREAT VALUE GUARANTEED!
  • ECO-FRIENDLY CHOICE: SAVE MONEY AND REDUCE WASTE WITH USED BOOKS!
BUY & SAVE
$42.44 $44.99
Save 6%
Version Control with Git: Powerful tools and techniques for collaborative software development
5 Head First Git: A Learner's Guide to Understanding Git from the Inside Out

Head First Git: A Learner's Guide to Understanding Git from the Inside Out

BUY & SAVE
$50.99 $79.99
Save 36%
Head First Git: A Learner's Guide to Understanding Git from the Inside Out
6 Git Commands Cheat Sheet Reference Guide – Essential Git Command Quick Guide for Beginners Developers

Git Commands Cheat Sheet Reference Guide – Essential Git Command Quick Guide for Beginners Developers

BUY & SAVE
$14.99
Git Commands Cheat Sheet Reference Guide – Essential Git Command Quick Guide for Beginners Developers
7 Pro Git

Pro Git

BUY & SAVE
$31.02 $59.99
Save 48%
Pro Git
8 Git Prodigy: Mastering Version Control with Git and GitHub

Git Prodigy: Mastering Version Control with Git and GitHub

BUY & SAVE
$19.00
Git Prodigy: Mastering Version Control with Git and GitHub
9 Pragmatic Guide to Git (Pragmatic Programmers)

Pragmatic Guide to Git (Pragmatic Programmers)

  • AFFORDABLE PRICES FOR QUALITY USED BOOKS IN GREAT SHAPE!
  • ENVIRONMENTALLY FRIENDLY CHOICE – REDUCE, REUSE, RECYCLE!
  • CURATED SELECTION: FIND HIDDEN GEMS IN EVERY GENRE!
BUY & SAVE
$7.90 $25.00
Save 68%
Pragmatic Guide to Git (Pragmatic Programmers)
+
ONE MORE?

To exclude files from a Git commit, you can use the .gitignore file in your repository. Here are the steps:

  1. Open your text editor and create a file named ".gitignore" in the root directory of your Git repository (if it doesn't already exist).
  2. In the .gitignore file, you can specify patterns to match the files that you want to exclude from Git commits. Each pattern should be on a separate line.
  3. Use wildcards and other special characters to define your patterns: "*" matches any series of characters. "?" matches any single character. "/" specifies a directory separator. "#" at the start of a line indicates a comment. "!" negates a pattern (i.e., includes files that would otherwise be ignored).
  4. Save the .gitignore file.

Now, Git will exclude any files or directories that match the patterns specified in the .gitignore file when you make a commit. Keep in mind that the .gitignore file also applies to all future commits, so ensure you only exclude the necessary files.

If you want to exclude a file that is already tracked by Git, you need to remove it from the repository using the following command:

git rm --cached

Replace <file> with the path of the file you want to remove from Git without deleting it locally.

Remember to commit the changes made to the .gitignore file by running:

git commit -m "Exclude specified files from commit"

After this, the specified files will be excluded from future Git commits.

What is the command to exclude a file that is already committed?

The command to exclude a file that is already committed is:

git rm --cached

This command removes the file from the Git repository's index, but keeps it locally in your working directory. Once you've executed the command and made a commit, the file will no longer be tracked.

What is the best practice for ignoring files in a git repository?

The best practice for ignoring files in a Git repository is to use a .gitignore file. This file is placed in the root directory of your repository and lists patterns for files and directories that Git should ignore.

Here are the steps to follow:

  1. Create a file named .gitignore in the root directory of your repository.
  2. Open the file in a text editor.
  3. Add patterns to ignore files or directories, each on a new line.
  4. Save the file and commit it to your repository.

Some common patterns you might include in a .gitignore file are:

  • Specific files: filename.txt
  • Wildcard patterns: *.txt (ignores all .txt files)
  • Directories: dir/ (ignores the entire dir directory)

You can also use additional syntax like negations, comments, and globbing patterns to make your .gitignore file more specific.

It's important to note that .gitignore only ignores untracked files. If a file is already tracked by Git before you add it to .gitignore, it will continue to be tracked and changes will still be visible in Git status and commits. To stop tracking a file that was previously committed, you need to use the git rm --cached command.

Regularly reviewing and updating your .gitignore file is necessary to ensure that ignored files are still being ignored and new files or patterns are included as needed.

How to exclude files from a git commit using regular expressions?

In Git, you can use the git add command with the -u or --update option followed by a regular expression pattern to selectively include or exclude files from a commit. Here's how you can exclude files based on a regular expression:

  1. Open your terminal or command prompt.
  2. Navigate to the root directory of your Git repository.
  3. Use the following command to exclude files matching a regular expression pattern: git add --update :/!Replace with the regular expression pattern you want to use to exclude files. For example, if you want to exclude all files ending with .txt, you can use: git add --update :/!*.txt
  4. Verify the changes using git status command: git status You should see the files that match the given pattern are not included in the "Changes to be committed" list.
  5. Commit the changes using git commit command: git commit -m "Your commit message" This will create a commit excluding the files matching the regular expression pattern.

Note: The exclusion pattern (!<pattern>) must be added after the colon (:/). The --update option is used to stage changes to tracked files, and the ! is used to exclude files matching the pattern.

How to exclude files from a git commit on Mac/Linux?

To exclude files from a Git commit on Mac/Linux, you can use a .gitignore file or the --exclude flag. Here's how you can do it:

  1. Create or edit the .gitignore file in the root directory of your Git repository. If the file does not exist, create it using the touch .gitignore command.
  2. Open the .gitignore file in a text editor and add the filenames or patterns of files you want to exclude. Each pattern should be on a new line. For example, to exclude a file named secret.txt, add the following line to .gitignore:

secret.txt

You can also use wildcards and glob patterns, such as *.txt to exclude all files with the .txt extension.

  1. Save and close the .gitignore file.
  2. When you run git status or git add ., the files matching the patterns in .gitignore will not be included in the list of modified files for staging.

If you want to exclude files temporarily for a specific commit without modifying the .gitignore file, you can use the --exclude flag with the git commit command. For example, to exclude a file named secret.txt for a single commit, use the following command:

git commit --exclude=secret.txt

This command will exclude secret.txt from the commit while keeping it tracked in the repository.

Note: It's important to remember that .gitignore only works for untracked files. If a file is already tracked in Git, removing it from the .gitignore file will not exclude it from further commits.