Horje
How to Ignore Folders and Directories in Git with .gitignore?

Git is a powerful and widely-used version control system that allows developers to track changes in their codebase, collaborate seamlessly, and maintain a robust history of their projects. It was created by Linus Torvalds in 2005, Git has become widely accepted for version control in the software development industry.

In this article, we will learn how to ignore git folders and directories. .gitignore file is used to ignore the folders and directories that need to be kept private or the folders that are part of every project setup like node_modules in Nodejs project or jar files.It will ignore the folders and directories as the project is pushed to the repository.

Steps to Ignore Git Folders and directories using .gitignoreFile

Step 1: Initialize the Git Repository

git init

The main purpose of the git init command is to initialize the new working environment or a new git repository to work on a new project. Before performing the git init command git will not track the files that are in the system it will start to track only when you initialize the git init command form there you can commit the files and push them to the remote repository. As we initialize the project to the repository .git folder is created. It helps to create a repository and keep track of it.

git-Folder

.git Folder

Step 2: Create a .gitignore file

In this step, we create a .gitignore file in the project directory. .gitignore file is a text file that specifies which files and directories are ignored. Which will reduce the size of the repository. As the file is created we can add the required files and folders which are to be excluded.

Syntax to exclude the folder

/folder_name

Syntax to exclude the file

file_name

Example: Initialize a repository on a project and push it to the Master Branch.

  • Initially, a new repository is initialized using the git init command.
  • In the next step status of the repository it checked using the git status command.Git status provides information about the branch, respective commits and the untracked files of the project.
  • In the next step we add the files and folders to the repository and commit them to repository.
  • As the files and folders are committed to the local repository,we use git remote add origin [URL] to set up connection between the local and the remote repo.
  • Finally, we push all the commit to master branch using git push -u origin master command.

Steps to Removing Already Tracked Files

Already Tracked Files are the files that are pushed in the Git repository.As git is a version control system it keeps tract of the changes in the files and folder of the repository .We can remove the file which is tracked using the following command

git rm --cached file_name
  • The command removes the file from the remote repository and does not make changes in the local repository .

Commit the changes

As the file is removed it needs to be committed to the remote repository .We use following command to commit.

git commit -m "Changes"

Push the Changes

Push the changes to the remote repository . Following command is used to commit the changes

git push

Example : Remove the already tracked file _styles.css

Remove-already-traced-file

Remove already traced file

  • Initally the _styles.css file is removed using the git rm –cached _styles.css command .The changes are committed and are pushed to the remote repository.

Why Use Git?

1. Version Control

Git’s primary function is to manage changes to files over time. It allows developers to revert to previous states, track modifications, and manage multiple versions of a project without losing work.

2. Collaboration

Git allows multiple developers to work on the same project simultaneously without overwriting each other’s changes. Through branches and merging, team members can contribute features and fixes while maintaining a stable codebase.

3. Backup and Restore

With Git, every copy of the repository is a complete backup of the entire project history. This decentralized approach ensures that the loss of any single repository does not result in data loss.




Reffered: https://www.geeksforgeeks.org


Git

Related
How to Add SSH Key to Your GitHub Account? How to Add SSH Key to Your GitHub Account?
How To Delete Remote Branch in Git? How To Delete Remote Branch in Git?
How to Undo Git Reset? How to Undo Git Reset?
How to Ignore 'node_modules' Folder in Git? How to Ignore 'node_modules' Folder in Git?
Difference between .gitignore and .gitkeep Difference between .gitignore and .gitkeep

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14