gran_profaci
gran_profaci

Reputation: 8471

How do I change files in a specific Commit in my history?

say I have the following commit history :

4317629 Bug-100 : Formatting / Cosmetic Changes
da10348 Bug-100 : Wire Up UI
9b49b0a Bug-100 : Added UI

Now, say I find that in the commit da10348, I had a few files : a.txt, b.txt, c.txt that contain lines that I need to remove / fix.

Can someone tell me just how I can do that?

Upvotes: 1

Views: 178

Answers (3)

Esko Luontola
Esko Luontola

Reputation: 73645

This is the method I find the easiest for doing small fixes to recent commits, because it requires just one rebase command (i.e. no git rebase --continue) and doesn't involve remembering commit hashes.

  1. Made a new commit with those fixes (typically I use just "fix" as the commit message)
  2. git rebase -i origin/master if the commit has not yet been pushed to origin, or alternatively git rebase -i HEAD~3 based on how far in the history the commit to change is (it's OK to overestimate - the older commits won't be changed)
  3. In the interactive rebase's editor, move the new commit with those fixes to be immediately after the commit you want to fix and change its command to f or fixup.
  4. Save and close the editor, wait for rebase to finish and check whether the resulting history is what you wanted.

Upvotes: 1

jonderry
jonderry

Reputation: 23633

Note that you can only do this if you're comfortable rewriting history for all descendant commits of the commit you want to change. Otherwise you should just make the change at the tip of your current master branch.

If you do want to do it you can execute

git checkout da10348
# make changes
git commit -a --amend
git rebase --onto <amended-sha> da10348 <your-branch>

Or if you prefer interactive rebase

git rebase -i da10348^
# change "pick da10348" to "edit da10348"
# save, and make changes when rebase -i stops at da10348

Upvotes: 1

zbs
zbs

Reputation: 681

You have to rebase your branch onto the commit you want to change in order to change the history after the commit so actually in this case you have to rebase your branch on the commit before the one you want to change( ^ operator):

git rebase -i da10348^

change the commit to edit and implement your changes and then:

git commit -a --amend --no-edit

and finally finish the rebase:

git rebase --continue

Upvotes: 1

Related Questions