Data report"State of code review 2024" is now liveRead the full report

How to use git remove commands effectively

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git rm command is used to remove files from both the working directory and the index (staging area), essentially telling Git to stop tracking the file altogether. This command is crucial when you want to permanently delete a file from your repository, ensuring it does not appear in future commits.

To remove a file that is currently tracked by Git, simply execute:

Terminal
git rm <file-name>

This command will remove the file from your working directory and staging area. If you commit after this command, the file will be removed from the repository.

If you have a file named old_project_notes.txt that is no longer needed, you can remove it like so:

Terminal
git rm old_project_notes.txt

After running this command, you need to follow up with a commit to finalize the removal:

Terminal
git commit -m "Remove outdated project notes"

You can also remove multiple files simultaneously by providing them as arguments to the git rm command:

Terminal
git rm file1.txt file2.txt file3.txt

If you need to remove a directory and all of its contents, you can use the -r option, which stands for recursive:

Terminal
git rm -r <directory-name>

To remove a directory named old_docs along with all its files:

Terminal
git rm -r old_docs

If you want to remove the file from Git but keep it in your local file system (not delete it from your working directory), you can use the --cached option:

Terminal
git rm --cached <file-name>

This command removes the file from staging and the repository after the next commit but does not delete it from your working directory.

To stop tracking a file named config.txt but keep it locally:

Terminal
git rm --cached config.txt

This command is particularly useful when you have accidentally added sensitive data or large files that you don't want to push to a remote repository but need to retain locally.

Alternatively you can also use the git clean command to remove unwanted files like untracked files. Untracked files are those that have been added to your working directory but not yet tracked by Git. Over time, these can accumulate and clutter your repository.

The git clean command is powerful and should be used with caution because it permanently deletes files from your directory.

Terminal
git clean -f

This command removes untracked files from the current directory. If you also want to remove untracked directories, you can add the -d option:

Terminal
git clean -fd

To preview which files would be removed without actually deleting them, use the -n option:

Terminal
git clean -n

To remove a local branch in Git, use the git branch command with the -d option. This command deletes the branch only if it has been fully merged in its upstream branch.

Terminal
git branch -d <branch-name>

If the branch has unmerged changes and you still want to delete it, use the -D option, which forces deletion:

Terminal
git branch -D <branch-name>

To delete a branch named feature-x:

Terminal
git branch -d feature-x

If feature-x is not fully merged and you are sure you want to remove it:

Terminal
git branch -D feature-x

Sometimes, you may need to undo changes introduced by recent commits, either to correct mistakes or to revert unwanted changes.

To undo the last commit and keep all the file changes in your working directory, use:

Terminal
git reset --soft HEAD^

If you want to completely undo the commit and all changes, use:

Terminal
git reset --hard HEAD^

If you need to remove a specific commit from the middle of a branch, you can use git rebase interactively:

Terminal
git rebase -i <commit-id>^

In the interactive mode, you can choose to drop the specific commit by changing the word pick to drop next to the commit you want to remove.

If you accidentally committed a file that you didn't intend to, you can remove it from the commit using git reset and git commit --amend.

  1. Unstage the file:

    Terminal
    git reset HEAD^ <file-name>
  2. Amend the commit:

    Terminal
    git commit --amend

This opens your editor, allowing you to modify the commit message if necessary, and removes the file from the commit.

If you accidentally committed a file named secret.txt:

Terminal
git reset HEAD^ secret.txt
git commit --amend

This will remove secret.txt from the last commit and allow you to adjust the commit message.

For further reading on removing objects from you Git repository, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2