Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove unneeded git commits?

Tags:

git

I have some garbage commits in my git repositry. These have been created by e.g. the git gui when changing the latest commit again and accidently creating additional commits.

Now I have some commits lying around with no HEAD assigned (detached, not part of any branch).

As I want to tidy up, my question is: How can I delete these commits (see F, G and H)? Is this done using rebase or revert or reset? Or using another tool? On which commit do I have to sit to do it?

A -- B -- C -- D -- E [master]
      \-- F -- G
           \-- H

Thanks

Christian

like image 953
Christian Wolf Avatar asked Aug 07 '11 19:08

Christian Wolf


People also ask

How do you clean up a commit?

Steps to get to a clean commit history:use fast-forward or squash merging option when adding your changes to the target branch. use atomic commits — learn how to amend, squash or restructure your commits. learn to force push 🤓

How do I get rid of local commits?

If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name> to move back to where the origin is. This will reset the state of the repository to the previous commit, and it will discard all local changes.

Can we delete commits in git?

Deleting the commit in Git must be approached in one of two ways, depending on if you have or have not pushed your changes. Please note before attempting this, running these commands will DELETE your working directory changes. Any changes to tracked files in the working tree since <commit> are discarded.

Can you discard commits?

You can simply remove that commit using option "d" or Removing a line that has your commit. In the latest git version there is no more option d. You need just remove lines with commits from rebase to delete them.


2 Answers

Try this:

git reflog expire --expire=now
git gc --prune=now
like image 115
cdhowie Avatar answered Oct 12 '22 13:10

cdhowie


Do the below:

git config gc.reflogexpireUnreachable now
git gc --prune=now
git config --unset gc.reflogexpireUnreachable
like image 23
manojlds Avatar answered Oct 12 '22 12:10

manojlds