Remove a commit

Hey! I’ve been creating a project, a private one, and I had trouble with a commit. I accidentally posted a token in my proj.sln file.

Is there a way to delete that from the commit history?

I want to publish my proj but I’m not gonna be able until I fix this.

*EDIT:
I modified multiple files in the commit I want to delete.
I DONT want to delete the commit, I want to change its “visibility” so no one can see the secret f. eg.

You cannot hide a specific commit. Anyone who can clone your repository will get the full history, that’s one of the core principles of git.

However, what you can do (and relatively easily because your repository is private): Use git rebase to change the commit, so it doesn’t contain the token any more. Specifically, you’ll need to do an interactive rebase and edit the commit that introduced the token.

After you’re done with the rebase make sure the code looks as you want it, you can then force-push it to your repository.

After that you should contact Github support so they can make sure the original commit with the token isn’t cached any more (otherwise it’s not on a branch, but might be accessible if anyone finds the ID). Best make note of the commit ID for that.

1 Like

And how can I do that?

I don’t know the git console commands.

There’s a pretty good description in the Rewriting History chapter of the Git Book. Especially the first two sections are important for your problem (“Changing the Last Commit” and “Changing Multiple Commit Messages”), you’ll need to mix those. Modifying history is a pretty advanced topic, so it might be a good idea to read some of the earlier chapters, too. If you have any questions after that this is the right place to ask. :slightly_smiling_face:

One hint regarding the “Changing Multiple Commit Messages” section: The examples there use “HEAD~3” to select from where to rewrite history (“three commits back from HEAD”). That works fine for the last few commits, if you have a longer history it’ll likely be easier to use the commit ID of the commit right before the one you want to change instead of counting how many commits you want to go back.