Ashish
Ashish

Reputation: 9

How to remove commits from the middle without affecting history?

I need to delete some commits from the remote, the ones which show after merging sometimes.

Mar 23   abcd999    some message
Mar 11   abcd123    some message
Feb 12   xyze456    merged in feature (not required)
Feb 6    98y65r4    merged in develop (not required)
Jan 20   987xcrt    2nd commit
Jan 5    45345rt    initial commit

However, I don't want the history to be changed. I have tried various methods, but all change the history and the abcd123, abcd999 commits shows as commited 5 minutes ago.

I am able to create the branch with correct commits and the correct time is shown when I execute : git log

But when I push, the top commits take the current time. I have tried doing:

git push -f
git push --force

But it hasn't worked. How can I do this?

Clarification : By keeping the commit history, i mean the date of the commit. I tried changing date by using --date. But the commit date still shows the latest when i push. My repo is on aws codecommit. Can this be the issue? In git log, all dates shown are as per requirement.

Upvotes: 0

Views: 89

Answers (1)

matt
matt

Reputation: 536027

I need to delete some commits... However, I don't want the history to be changed.

Deleting commits does change history. You cannot want to delete commits without wanting to change history. It's metaphysically impossible.

But when I push, the top commits take the current time

Correct. That's because you also cannot change an existing commit. But parentage is a feature of a commit. Therefore if you do delete a commit, every subsequent commit (commits with the deleted commit as an ancestor) is replaced with a new and different commit (because parentage cannot be changed). So yes, it does have a new date, because it is in fact a new commit.

Upvotes: 3

Related Questions