Git: .gitignore and quick way to stage many deleted files

Overview

Discuss how to use and put the .gitignore file and to stage tracked files: remove files from Git without having to manually go through and add each file.

Git: How to use and put gitignore file

The best answer and way to understand gitignore file I found from stackoverflow: “.gitignore file, where should I put it?“:

You can have a .gitignore in every single directory of your project.

However, the best practice is to have on single .gitignore file on the project root directory, and place all files that you want to ignore in it, like this:

ignoredFile.whatever
ignoredDirectory/*
directory/ignoredFileInsideDirectory
.svn

Once you create the .gitignore file, the ignore files that have changes or are new into the repository will not be shown to as waiting for commit.

You can also have one global local git ignore file, that will manage all of your git repositories.

git config --global core.excludesfile ~/.gitignore_global

And popular gitignore files are listed in GitHub public repository:

GitHub maintains an official list of recommended .gitignore files for many popular operating systems, environments, and languages in the github/gitignore public repository.

Remarks: You need to put .gitignore file under the root folder rather than in the .git folder, that is, the .gitignore and .git folder are at the same folder level, which are parallel, no one contains the other.

Git: Stage many deleted files as a whole

1) Run the following

git rm $(git ls-files --deleted)

$(git ls-files –deleted) will find all the deleted files and make them a single string, so it is convenient to delete them as a whole rather than delete each file manually one by one.

2) And another option which might be better is as follows:

For Git 1.x

$ git add -u

This tells git to automatically stage tracked files — including deleting the previously tracked files.

For Git 2.0

To stage your whole working tree:

$ git add -u :/

To stage just the current path:

$ git add -u .

Reference: http://stackoverflow.com/questions/492558/removing-multiple-files-from-a-git-repo-that-have-already-been-deleted-from-disk

Summary

Discuss how to use and put the .gitignore file and to stage tracked files: remove files from Git without having to manually go through and add each file.

Written on December 16, 2014