Skip to content

Instantly share code, notes, and snippets.

@protrolium
Last active December 24, 2023 18:57
Show Gist options
  • Save protrolium/97260cecbe1170f7999de7247b194460 to your computer and use it in GitHub Desktop.
Save protrolium/97260cecbe1170f7999de7247b194460 to your computer and use it in GitHub Desktop.
git cheatsheet

git rm --cached [filenames]

Here’s how I removed all the files I wanted to delete from one of my bin subdirectories:

git rm --cached bin/com/devdaily/sarah/\*

I use the unusual * syntax at the end of that command instead of * because you need to escape the * from the git command. In a simpler example, if there was just one file in the bin directory named Foo.class that I wanted to remove from the Git repository, I would use this command:

git rm --cached bin/Foo.class

To be clear, what this command means is: You want to keep these files on your hard drive, but you don’t want Git to track them any more.

Staging

Stage all (new, modified, deleted) files
git add -A

Stage all (new, modified, deleted) files in current folder
git add .

Stage new and modified files only
git add --ignore-removal .

Stage modified and deleted files only
git add -u

Removing

If you want to remove the file from the Git repository and the filesystem, use:

git rm file1.txt
git commit -m "remove file1.txt"

But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:

git rm --cached file1.txt
git commit -m "remove file1.txt"

Clear the cache and re-instate .gitignore

This will help you remove cached index files, and then only add the ones you need, including changes to your .gitignore file.

1. git rm -r --cached .
2. git add .
3. git commit -m 'Removing ignored files'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment