Read Anthropic’s case study about Graphite Reviewer

How to remove committed files in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


Sometimes, you may need to remove a file that has already been committed to a Git repository. This could be due to various reasons such as accidentally committing a scratch file, or some other incomplete piece of work. This guide will explain how to remove committed files in Git using different methods and commands.

Note: In the case that you committed sensitive information to the repo, you should treat this information as compromised and take more careful steps to remove this data from the repo. For a detailed walkthrough see this guide on removing sensitive data from Git.

Removing files from a commit can alter the commit history, especially if the changes are already pushed to a shared repository. It's important to understand the implications of modifying commit history and to communicate with your team when working in a collaborative environment.

If you need to remove a file from the most recent commit, you can do so using the following steps:

  1. Reset the file to the last commit:

    Terminal
    git reset HEAD~1 path/to/your/file.txt

    This command unstages the file from the last commit but keeps it in your working directory.

  2. Remove the file from the working directory:

    Terminal
    rm path/to/your/file.txt
  3. Stage the changes:

    Terminal
    git add .
  4. Amend the last commit:

    Terminal
    git commit --amend -m "Your updated commit message"

    This command creates a new commit, replacing the previous one.

  5. Push the changes:

    Terminal
    git push --force

    Force pushing effectively replaces the previous commit with the new one. Use caution with --force however, as it rewrites the commit history.

If the file is committed in an older commit, you need to use an interactive rebase:

  1. Start an interactive rebase:

    Terminal
    git rebase -i HEAD~n

    Replace n with the number of commits back you want to start the rebase. For example git rebase -i HEAD~2 will start a rebase using the last 2 recent commits.

  2. Mark the commit for editing: In the text editor that opens, change pick to edit for the commit where the file was added.

  3. Remove the file:

    Terminal
    git rm --cached path/to/your/file.txt
  4. Amend the commit:

    Terminal
    git commit --amend
  5. Continue the rebase:

    Terminal
    git rebase --continue
  6. Push the changes:

    Terminal
    git push --force

If the file is in multiple commits, consider using the filter-branch or filter-repo tool to rewrite history:

  1. Using filter-branch:

    Terminal
    git filter-branch --force --index-filter \
    'git rm --cached --ignore-unmatch path/to/your/file.txt' \
    --prune-empty --tag-name-filter cat -- --all
  2. Using filter-repo (recommended for large repositories):

    Terminal
    git filter-repo --path path/to/your/file.txt --invert-paths
  3. Push the changes:

    Terminal
    git push --force --all

For more detailed instructions, see this guide on the filter-repo tool.

  • Backup your repository: Before making significant changes to your commit history, create a backup branch:

    Terminal
    git branch backup-branch
  • Communication: Inform your team when performing history rewrites, especially in shared branches.

  • Avoid force pushing in shared branches: If possible, avoid --force pushing to shared branches to prevent conflicts with your team's work.

  • Use .gitignore: To prevent accidental commits of certain files in the future, add them to your .gitignore file.

For further reading on removing committed files in Git, see the official Git documentation.

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

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