How to Fix Unwanted File Changes in a GitHub PR
What I Learned
I learned how to remove unwanted file changes from a specific commit in a Pull Request. By combining git rebase -i and git commit --amend, you can pinpoint and modify past commits while keeping the history clean.
Details
The Problem
When creating a PR and checking the diff, I found that a certain commit had unintentionally changed a file:
D example/config.yaml (deleted)
It’s common to accidentally include unrelated changes such as configuration files or documentation in commits. This prevents the PR from being properly reviewed, so I needed to fix only that specific commit.
Solution Strategy
The goals for this fix were:
- Don’t affect other changes in the PR
- Modify “only” the problematic commit
- Keep the history clean and eliminate unwanted changes
- Don’t leave noise like “delete → re-add”
To achieve this, I chose to use git rebase -i to directly edit the target commit and git commit --amend to rewrite its contents.
Actual Steps
Step 1: Start rebase from the parent of the target commit
With the target commit ID as abcd1234:
1git rebase -i 'abcd1234^'
A rebase screen opens, showing a line like:
pick abcd1234 Some commit message
Change it to:
edit abcd1234 Some commit message
When you save, processing pauses at the abcd1234 commit.
Step 2: Restore the file with unwanted changes to its original state
Restore the file to the state before the commit was applied (at the parent commit point).
Example: Restore example/config.yaml
1git checkout 'HEAD^' -- example/config.yaml
2git add example/config.yaml
This restores the accidentally deleted/modified file and stages the fix.
Step 3: Amend the commit contents
1git commit --amend
This rewrites the contents of abcd1234, keeping the parts that were originally needed and replacing it with a new commit where only the erroneously changed file is fixed.
Step 4: Continue the rebase and update the PR branch
1git rebase --continue
2git push --force
This completely rewrites the target commit on the PR, fixing it to a clean state without the unwanted file changes.
Before / After History Comparison
Before (with unwanted file changes)
* abcd1234 ← This commit had unwanted changes to example/config.yaml
|
* 7890fe12
|
...
After (correctly modified commit contents)
* abcd1234' ← Incorrect changes to example/config.yaml have been removed
|
* 7890fe12
|
...
The commit ID changes to a different one because it’s rewritten, but the flow of the history itself is preserved.
Insights Gained
rebase -i is optimal when you want to modify only specific commits
Adding a revert to balance things out is easy, but it doubles the history and makes the PR harder to read. This method is very clean.
Combining rebase and amend allows you to cleanly fix the past
You can fix changes that shouldn’t have been committed as if they never existed in the first place.
Force push is fine if limited to PR branches
If it’s a branch that no one else is using, rewriting history is safe.