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

How to remove a file from a Git commit

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


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: Unstage the file you want to remove from the commit
git reset HEAD <file-to-remove>
# Step 2: If you also want to delete the file from your working directory, use:
git rm <file-to-remove>
# Alternatively, if you just want to unstage the file and keep it in your working directory, use:
git restore --staged <file-to-remove>
# Step 3: Amend the last commit to exclude the file
git commit --amend

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 amended commit with the file removed.

If the file to be removed is part of an older commit, you need to perform an interactive rebase:

Terminal
# 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.

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