Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a specific changeset from Git repository altogether?

Tags:

git

Is it even possible to remove a particular changeset from a Git repository altogether. I want to do this because there was a commit while ago that added some binary files that are absolutely useless for source code management.

I can understand if this is impossible to do given the fact that the commit nodes have parents and a specific history, but I guess it will not hurt to ask.

And the answer is no for questions that ask "can you just clone new copy of the time before that commit occurred and start from there again?".

like image 676
Tower Avatar asked Nov 11 '11 21:11

Tower


People also ask

How do I remove a specific push from github?

When you want to completely remove the last commit, you need to mention the commit id of the one before last. Then push your new branch up to the remote repo if you have a remote. If you just want to remove the commit but keep the changes of that commit, then git reset <COMMIT_ID> will work.

How do I remove a file from git without deleting it?

Using the git rm –cached Command We've mentioned that git rm FILE will remove files from the index and local working tree by default. However, the git rm command provides the –cached option to allow us only to remove files from the repository's index and keep the local file untouched.

How do I delete old commits?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.


1 Answers

Use git rebase -i <commit-id>, where id is a commit before the changeset you want to remove. It will open a list of commits between the head and the changeset, delete from the list the commit you want and continue. When this commit will be "detached", it means it will be floating in the repository until it's garbage collected. You could use git gc to force it.

However, you should remember, that all clones of the repository will be diverged, and should be rebased too after it.

like image 90
kan Avatar answered Oct 21 '22 09:10

kan