Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to Ignore Files that have Already been Committed to the Repo?
Next article icon

How to Ignore Files that have Already been Committed to the Repo?

Last Updated : 23 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Git, keeping your repository clean and organized is crucial. Sometimes, you might have files that were accidentally committed but shouldn't be tracked by Git.

This article explores effective methods to handle such situations and prevent these files from being tracked in the future.

Table of Content

  • Removing the file from Git's Tracking (keeping it Locally)
  • Adding a Pattern to your .gitignore file

Removing the file from Git's Tracking (keeping it Locally)

  • This approach removes the file from Git's staging area (index) while preserving it in your working directory.
  • Execute the following command, replacing <filename> with the actual name of the file you want to ignore:
    git rm --cached <filename>
  • The git rm command removes files.
  • The --cached flag instructs Git to remove the file only from the index, not from your local disk.
gitrm
git rm --cached new.html

Adding a Pattern to your .gitignore file

  • Check if a .gitignore file exists in your repository. If the file doesn't exist, create one.
  • Open the .gitignore file in a text editor.
  • Specify a pattern in the file that matches the file you want to ignore.
  • For a single file, use the exact filename (e.g., filename).
  • To ignore specific file types, use wildcards (e.g., *.log).
  • For directories, use the directory name followed by a forward slash (e.g., folder/)
  • Save the .gitignore file.

Example:

Imagine you want to ignore a file named "config.txt" and any files ending in ".log".

  • Open or create the .gitignore file.
  • Add the following lines to the file:
 config.txt    
*.log
gitignore
Now, Git won't track any changes made to "config.txt" or future files with the ".log" extension.

Next Article
How to Ignore Files that have Already been Committed to the Repo?

H

htomarec8c
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    How To List Only The Names Of Files That Changed Between Two Commits?
    Git is a powerful version control system that allows developers to track changes in their codebase. One common task is identifying which files have changed between two specific commits. This can be useful for code reviews, debugging, and understanding the impact of changes. In this article, we'll ex
    3 min read
    How To Reset Remote Repository to a Certain Commit in Git?
    Resetting a remote repository to a specific commit in Git can be an important task, especially when you need to revert changes or roll back to a stable state. This article will guide you on how To Reset Remote Repository to a Certain Commit in Git. Table of Content Approach 1: Using `git reset` and
    2 min read
    How to Retrieve the Hash for the Current Commit in Git?
    A Git commit hash, typically a 40-character alphanumeric string, serves as a unique fingerprint for a particular commit within your repository's history. Identifying the hash of the current commit in Git is a fundamental skill for version control. We will explore two effective methods to retrieve th
    2 min read
    How to Make .gitignore Ignore Everything Except a Few Files?
    Sometimes you might have files in your Git project that you don't want tracked by version control. Here's how to use a `.gitignore` file to tell Git to ignore everything except a few specific files you want to keep track of. Table of Content Creating the `.gitignore` FileAdding File Exclusion RulesS
    2 min read
    How to list All Files in a Commit in Git?
    Working with Git, it's often essential to inspect the contents of your commits. Whether you need to review changes, debug issues, or understand the history of your project, knowing how to list all files in a commit is a fundamental skill. In this article, we’ll walk you through the steps to list all
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences