Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove old git commits

Tags:

git

I'm very new to git, and was wondering if something like this is possible?

>git log --pretty=oneline --abbrev-commit 2f05aba Added new feature 3371cec Fixed screw up    <-- I want to remove this daed25c Screw up          <-- and remove this. e2b2a84 First.                So it's like they never happend. 

Is this possible?

like image 687
Josh Avatar asked Mar 15 '12 17:03

Josh


People also ask

How do I remove multiple commits in git?

Thus, we can use the git revert command with the --no-commit option. The syntax of the command is git revert --no-commit <commit> . Thus, to revert the three commits of the bug fixes done, we need to do as follows. Thus, the repository in Git is now in the state before committing the bug1 fixed .

How do I clean up commit history?

It's cleanup time ⏰ If you have been lazily writing multiple vague commits, you can use git reset --soft <old-commit> to make your branch point to that old commit. And as we learned, Git will start by moving the branch pointer to it and stops right there. It won't modify the index or working directory.


2 Answers

If you truly wish to delete them (wiping them from history, never to be seen any more), you can

run rebase:

git rebase -i HEAD~4 

and then, just delete (or comment out) the lines corresponding to the commits you wish to delete, like so:

pick 2f05aba ... #will be preserved #pick 3371cec ... #will be deleted #pick daed25c ... #will be deleted pick e2b2a84 ... #will be preserved 
like image 146
Shathur Avatar answered Oct 10 '22 03:10

Shathur


This is possible with git rebase. Try the following

git rebase -i HEAD~4 

and then, follow the interactive instructions in your editor. In the first step you "squash" the commits. It should look something like this:

pick 2f05aba ... will be preserved squash 3371cec ... will be squashed with daed25c squash daed25c ... will be squashed with e2b2a84 pick e2b2a84 .. will contain this and 3371cec and daed25c 

In the second step you can edit the commit messages.

like image 31
Deve Avatar answered Oct 10 '22 01:10

Deve