0

I am experimenting with Git. I have a file a.txt in the repo and 4 commits in my master branch currently. git log:

* aabe7d0 (HEAD -> master, origin/master, origin/HEAD) Updated goats
* 57694cf (tag: customerY_release) Updated sheep
* 791b4cb (tag: customerX_release) Updated cows
* e96a52b Added init contents..'

I want to amend an older commit ie., change a line in file a.txt in the commit 791b4cb. "cats=10" to "cats=100". This change then should reflect across all commits 791b4cb, 57694cf, aabe7d0 and no new commits should be created in the process, the tree should look same at the end.

I tried the following from reference: https://stackoverflow.com/questions/8824971/how-to-amend-older-git-commit:

git rebase -i HEAD~3   // HEAD~2 did not list the commit 791b4cb...

Then changed the line in the opened text editor to:

edit 791b4cb
pick 57694cf 
pick aabe7d0 

Saved... and then made the required changes in a.txt "cats=10" to "cats=100"... and then commited.

git add a.txt
git commit --amend --no-edit
git rebase --continue

But the above process resulted in a different looking tree with new commits added.

* f55fdd4 (HEAD -> master) Updated goats
* 59c850d Updated sheep
* 7aee3ea Updated cows
| * aabe7d0 (origin/master, origin/HEAD) Updated goats
| * 57694cf (tag: customerY_release) Updated sheep
| * 791b4cb (tag: customerX_release) Updated cows
|/
* e96a52b Added init contents..'

This is not what I intended. I wanted the tree looks same as from beginning as if all existing commits: 791b4cb, 57694cf, aabe7d0 get merged and amended across with changes happened in a.txt at 791b4cb... Is it possible to do this in Git?

Mitu Raj
  • 213
  • 1
  • 5
  • 11

1 Answers1

0

What you're asking for doesn't make sense.

In Git, each commit is a record of the filesystem tree and a pointer to its parent commit. If you change the parent of a commit, you're actually constructing a different commit. And then you make copies of the subsequent commit, that again is similar to originals, but with a different parent. And so on.

If you push (with --force-with-lease option), then the upstream tree will once again look like your local tree. Be careful not to surprise other developers with force-push, though.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103