To remove a file from a commit while keeping it in your working directory, you can use the following command:
git reset HEAD~1 path/to/file
Understanding Commits
What is a Commit?
In Git, a commit represents a snapshot of your project at a certain point in time. Each commit records changes to the repository, along with metadata such as the author, timestamp, and a commit message. This history of commits allows you to track changes, revert to previous versions, and collaborate with others.
The Difference Between Staging and Committing
Before a file becomes part of a commit, it must be staged in the index. The staging area acts as a buffer between your working directory and the commit history. Changes that you want to include in the next commit must be added to the staging area using the `git add` command. Only the files in the staging area are included when you make a commit.

The Need to Remove Files from a Commit
Scenarios for Removing Files
There are various situations where you might find yourself needing to remove files from a commit but want to keep them locally. Common scenarios include:
- Mistakenly added files: Sometimes, you may accidentally add files that are not relevant to the commit.
- Large files that should have been ignored: If you added a large file that you intended to be included in your `.gitignore`, you'll want to remove it from the commit while keeping it for local use.
- Sensitive information: If a commit contains sensitive data, you may want to remove the committed file but still retain a copy locally to correct or use later.

Methods to Remove Files from a Commit but Keep Them Locally
Using `git reset`
Explanation of the Command
The `git reset` command is powerful for manipulating the commit history and the staging area. It can operate in three modes:
- Soft: Moves the HEAD pointer to a specified commit while keeping your changes staged.
- Mixed: Moves the HEAD pointer and unstages changes, keeping them in the working directory. This is the default mode.
- Hard: Moves the HEAD and resets the working directory to match the specified commit, losing uncommitted changes.
Practical Example
Suppose you've just committed a change and realize that `example.txt` should not have been included. Here's how to remove it from the latest commit while keeping it locally:
- Check your commit history to identify the commit hash (usually just the previous commit):
git log
- Use `git reset` to unstage the file:
git reset HEAD~1 example.txt
This command moves the HEAD pointer to the previous commit while keeping your changes in the working directory.
Result of the Command
After running this command, the file `example.txt` will be removed from the most recent commit but will still exist in your working directory.
Using `git restore`
Detailed Explanation
The `git restore` command is designed for managing the working directory and index more straightforwardly. It's a newer command (introduced in Git 2.23) that is often preferred for unstaging files.
Practical Example
To unstage and keep the file after committing, you can run:
git restore --staged example.txt
This command directly unstages `example.txt`, keeping it in your working directory unchanged.
Additional Notes
Using `git restore` is beneficial as it clarifies intent, helping avoid confusion with more complex commands like `git reset`.
Using `git checkout` (for older versions)
Overview
In versions of Git prior to 2.23, the `git checkout` command was commonly used to reset files in the working directory from the index or a commit.
Code Snippet Example
To unstage a file and keep it in the working directory, you can use:
git checkout -- example.txt
This effectively reverts the file back to the last committed version but retains the file locally.
Limitations and Best Practices
While `git checkout` still works, it can sometimes lead to confusion, especially with other uses of the command. Therefore, it's often recommended to use `git restore` when possible for clarity.

Amending Commits
What is Commit Amending?
Amending a commit allows you to modify the most recent commit, either by changing its content or updating the commit message. This can be a useful approach when you realize you've forgotten to include changes or need to edit a commit message.
How to Amend a Commit
If you want to amend the last commit but need to remove a specific file, ensure it's unstaged first. Here’s how to proceed:
git commit --amend --no-edit
This command updates the commit using your staged changes while keeping the commit message the same. Be mindful that if you're working in a shared repository, amending commits can rewrite history, potentially disrupting collaborators.
Amending with File Removal
To remove a file effectively during the amend process, ensure you've unstaged the file first and then run the amend command. This keeps the file on your local space while modifying the commit.

Best Practices When Modifying Commits
Understanding Consequences
Before modifying any commit, especially in a shared environment, it's vital to recognize the implications. Altering commit history can affect collaborators who have already pulled the changes. Always communicate with your team before rewriting history.
Keeping Commit History Clean
Maintaining a tidy commit history is crucial for better collaboration and code management. Here are some helpful tips:
- Create smaller, atomic commits: Each commit should encapsulate a single logical change. This practice enhances clarity and makes it easier to revert specific changes.
- Write clear commit messages: Descriptive messages help you and your collaborators understand the purpose of each commit. Good commit messages can significantly speed up code reviews and debugging.

Conclusion
Knowing how to remove files from a Git commit while keeping them locally is an essential skill for any developer. Mastering these commands empowers you to manipulate your commit history effectively and adhere to best practices. Practicing these techniques in sample repositories can solidify your understanding of Git commands and enhance your version control proficiency.

Call to Action
If you found this guide helpful, consider subscribing to our blog for more practical Git tutorials. Explore further to unlock the full potential of version control and take your coding practices to the next level!