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
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 🤓
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.
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.
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.
Try this:
git reflog expire --expire=now
git gc --prune=now
Do the below:
git config gc.reflogexpireUnreachable now
git gc --prune=now
git config --unset gc.reflogexpireUnreachable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With