Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

amending a single file in a past commit in git

Tags:

git

I want to fix a file in past commit. This might affect all ascending commits.

Is there an easy way to do that?

Many times when I commit twice I find that I've had error in the first commit, and I wish to fix the error without having to git reset my last good commit.

For clarification, I want to change the actual commit, that is, I want the content of the past commit to be changed. In other words, I want to change history!

like image 999
Elazar Leibovich Avatar asked Jan 29 '09 21:01

Elazar Leibovich


People also ask

How do I change a file from a previous commit?

You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.

How do I amend last committed push?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.


2 Answers

If you only want to amend the second to last commit (eg. not long ago, especially not before many branches and merges), then I use this procedure:

  1. git checkout -b tmp bad-commit
  2. fix the files
  3. git commit --amend
  4. git rebase tmp master

If you have merges in between, you may wanna try rebase -i -p, but the results may vary.

like image 95
jpalecek Avatar answered Sep 29 '22 11:09

jpalecek


It looks like:

  • a filter-branch (complex command which could do what you want)
  • and amend and rebase as described by jpalecek. You will find the same solution slightly more detailed in this other related question.

You can find an example of rebase interactive in this comment: you could then avoid the temporary branch, but again, it is more complex.

I also rebase often to clean up the history of development so that changes are correct and grouped properly.

A made-up example:
I rename function foo to bar and commit it with a comment that says, "renamed foo to bar".
Then I move on to the next feature or fix, and commit that, and move on to the next.
Halfway through that, I find that I missed an instance of foo!
I commit my work in progress (or use git-stash), fix the missing 'foo' and commit that, then use git-rebase --interactive to merge the all the foo fixes together into one clean commit.
If I didn't use git-stash, then I'll use git-commit --amend (another form of rebasing) when I finally finish the feature that was in progress.

When my patches are pushed for review, all the pieces are correct and tell a coherent story. Sometimes I use git-rebase --interactive just to make adjacent temporally-separated changes which affect the same bits, so that changes are in context.

like image 41
VonC Avatar answered Sep 29 '22 10:09

VonC