Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to use git remove commands effectively

Kenny DuMez
Kenny DuMez
Graphite software engineer


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:

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:

git rm old_project_notes.txt

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

git commit -m "Remove outdated project notes"

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

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:

git rm -r <directory-name>

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

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:

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:

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.

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:

git clean -fd

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

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.

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:

git branch -D <branch-name>

To delete a branch named feature-x:

git branch -d feature-x

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

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:

git reset --soft HEAD^

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

git reset --hard HEAD^

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

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:

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

    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:

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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2