How to remove a file from a Git commit

Kenny DuMez
Kenny DuMez
Graphite software engineer
Try Graphite


Note

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


Table of contents

Removing a file from a Git commit allows developers to correct their commit history by excluding specific files. Whether you accidentally included a file in your commit or need to remove sensitive data, Git offers tools to handle these modifications. This guide will walk you through the process of removing a file from a Git commit, both before and after the commit has been pushed to a remote repository.

Sometimes files are mistakenly added to a commit or contain sensitive data that should not be shared. Removing a file from a commit helps to keep the repository clean and secure.

If the commit with the unwanted file has not been pushed to the remote repository, the process to remove it is straightforward.

First, determine if the file was included in the most recent commit or in a prior commit:

To identify which files were included in a specific commit in Git, you can follow these steps:

If you are checking the most recent commit you can use the following command to list the files that were changed in the most recent commit, along with the type of changes made (add, modify, delete):

Terminal
git show --name-status

This command will display the commit details, including the file names and the status of each file (e.g., A for added, M for modified, D for deleted).

If you are checking a prior commit, first, you need to find the commit hash of the commit you're interested in. You can list recent commits using:

Terminal
git log --oneline

This will show you a brief one-line description of each commit, including the commit hash. Once you've identified the commit hash for the commit you're interested in, use the git show command with the commit hash to see the files changed in that specific commit:

Terminal
git show --name-status <commit-hash>

Replace <commit-hash> with the actual commit hash. This command will list the files that were included in the specified commit along with what changes were made to them.

  • To remove a file from the most recent commit, you will use the git reset command.
  • To remove a file from an earlier commit, you will need to perform an interactive rebase.

If the file was included in the latest commit, follow these steps:

Terminal
# Step 1: Reset your HEAD to be pointing at one commit before the most recent:
git reset --soft HEAD~1
# Step 2: if you just want to unstage the file and keep it in your working directory, use:
git restore --staged <file-to-remove>
# Alternatively, if you also want to delete the file from your working directory, use:
git rm <file-to-remove>
# Step 3: Create a new commit excluding the file
git commit
Replace `<file-to-remove>` with the name of the file you want to exclude from the commit. This command sequence will open your default text editor to allow you to optionally edit the commit message. Saving and closing the editor will finalize the new commit with the file removed, leaving your repository in a clean state.
#### 3. Removing a file from an older commit
If the file to be removed is part of an older commit, you need to perform an interactive rebase:
```bash
# Start an interactive rebase for the last n commits
git rebase -i HEAD~n

Replace n with the number of commits you want to go back. This will open an editor showing a list of commits.

  • Find the commit containing the file you want to remove and change pick to edit.
  • Save and close the editor.
  • Git will pause the rebase at the commit you chose to edit.
Terminal
# Remove the file and continue the rebase
git rm <file-to-remove>
git commit --amend
git rebase --continue

If you've already pushed the commit to a remote repository, the process is similar to the above, but requires force-pushing to re-write the history of the branch:

  • Follow the steps for removing the file from the most recent or older commits as described.
  • Once you've amended the commits locally, use the following command to update the remote repository:
Terminal
git push --force

For further reading see the official Git documentation.

Yes - if you want to keep the file in your working directory but remove it from the commit, use git restore --staged <file> instead of git rm <file>. This will unstage the file but keep your changes intact.

Force pushing rewrites Git history, which can cause problems for other developers who have based their work on the old commits. Always coordinate with your team before force pushing to shared branches, or consider using git push --force-with-lease for additional safety. For more details, see our guide on how to Git force push.

Yes, you can remove multiple files by running git restore --staged <file1> <file2> <file3> or git rm <file1> <file2> <file3> for each file you want to remove.

  • git reset --soft HEAD~1 moves your HEAD back one commit but keeps all changes staged
  • git reset --mixed HEAD~1 (or just git reset HEAD~1) moves your HEAD back and unstages all changes

For a comprehensive overview of reset options, check out our guide on how to use the Git command git reset.

Common sensitive files include:

  • Configuration files with API keys or passwords
  • Environment files (.env)
  • Log files with user data
  • Database dumps
  • Personal information files

When in doubt, it's better to remove a file and add it to .gitignore than to leave sensitive data in your repository.

If you used git restore --staged, the file is still in your working directory. If you used git rm, you can recover it using git checkout HEAD -- <file> or git restore <file> (Git 2.23+).

Immediately contact your team and explain the situation. They may need to reset their local branches. Consider using git reflog to find the previous commit hash and potentially recover the lost history.

When removing files from older commits, you'll often use interactive rebase. For more information on this process, see our guide on git rebase using HEAD which covers interactive rebasing in detail.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone