Once you push to the repo, you really don't want to go about changing history. However, if you are absolutely sure that nobody has pulled/fetched from the repo since your offending commit, you have 2 options.
If you want to remove the "bad" commit altogether (and every commit that came after that), do a git reset --hard ABC (assuming ABC is the hash of the "bad" commit's elder sibling — the one you want to see as the new head commit of that branch). Then do a git push --force (or git push -f).
If you just want to edit that commit, and preserve the commits that came after it, do a git rebase -i ABC~. This will launch your editor, showing the list of your commits, starting with the offending one. Change the flag from "pick" to "e", save the file and close the editor. Then make the necessary changes to the files, and do a git commit -a --amend, then do git rebase --continue. Follow it all up with a git push -f.
I want to repeat, these options are only available to you if nobody has done a pull or fetch that contains your offending commit. If they have, doing these steps will just make matters worse.
Answer from David Deutsch on Stack OverflowOnce you push to the repo, you really don't want to go about changing history. However, if you are absolutely sure that nobody has pulled/fetched from the repo since your offending commit, you have 2 options.
If you want to remove the "bad" commit altogether (and every commit that came after that), do a git reset --hard ABC (assuming ABC is the hash of the "bad" commit's elder sibling — the one you want to see as the new head commit of that branch). Then do a git push --force (or git push -f).
If you just want to edit that commit, and preserve the commits that came after it, do a git rebase -i ABC~. This will launch your editor, showing the list of your commits, starting with the offending one. Change the flag from "pick" to "e", save the file and close the editor. Then make the necessary changes to the files, and do a git commit -a --amend, then do git rebase --continue. Follow it all up with a git push -f.
I want to repeat, these options are only available to you if nobody has done a pull or fetch that contains your offending commit. If they have, doing these steps will just make matters worse.
If it's only on your local PC (or noone checked out your changes):
Use:
git log
to find the commit you want to remove. Copy hash (the long sqeuence like: e8348ebe553102018c...).
Use:
git rebase -i [hash]~for example:
git rebase -i e8348~
Just remove the commit you don't need and save the file.
Interactive git rebase can let you also fix the broken commit - there is no need to remove it.
If you pushed changes to the server or someone already got your changes - never change history - it'd cause serious problems for your team.
First off, git revert is the wrong command here. That creates a new commit that reverts an older one. That's not what you're asking for. Secondly, it looks like you want to revert HEAD instead of HEAD^.
If you haven't pushed this anywhere, you can use git reset --hard HEAD^ to throw away the latest commit (this also throws away any uncommitted changes, so be sure you don't have any you want to save). Assuming you're ok with the sensitive information being present in your copy and nobody else's, you're done. You can continue to work and a subsequent git push won't push your bad commit.
If that's not a safe assumption (though if not I'd love to hear why), then you need to expire your reflogs and force a garbage collection that collects all outstanding objects right now. You can do that with
git reflog expire --expire=now --expire-unreachable=now --all
git gc --prune=now
though this should only be done if you really absolutely need to do it.
If you have pushed your commit, then you're pretty much out of luck. You can do a force-push to revert it remotely (though only if the remote side allows that), but you can't delete the commit itself from the remote side's database, so anyone who has access to that repository can find it if they know what to look for.
If you don't care about the commit, just do:
git reset --hard HEAD~
to blow away the commit.
If you want the changes to be in working directory, do:
git reset HEAD~
Depending on what you have done with git revert, you might have to change the above commands. Revert creates a new commit that reverts the commit you wanted to revert. So there will be two commits. You might have to do HEAD~2 to remove them both.
Note that, usually, revert is the safer way to, well, revert changes. But here, since you want to remove sensitive data, reset is the best approach.
Hello, I have an unusual git repo which I'm using to create backups of a project with quite a few non-source code files, which have changed more than I expected. I'm actually thinking git might not have been the best tool for the job here, but I'm familiar with it and will probably continue to use it. This is just a personal project, and I'm the only contributor.
What I'm looking for is a way to completely erase a git commit, preferably give the git commit hash. The reason for this is because I have several consecutive commits which change a variety of large files, but I really don't care about the commits in between those which I think would be sufficient to keep. I was thinking there should be a way to remove the unneeded intermediate commits with prune, but am not sure what the best approach here is - thanks!
This is a theoretical question: If I work on a project where I made changes that seem to be OK and not causing issues, but then, let's say after many other commits and merges with the entire team we realize that the commit I did actually caused some damage that needs to be undone..
for example, I refactored some variable name across the entire project in all occurences and only later realized some were supposed to remain unchanged.
Is it possible to "remove" this commit from the project while keeping all the other merges? Assuming they do not conflict or have anything to do with this change