Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permanently remove (or hide) a commit from a GitHub PR after force pushing

Tags:

git

github

In order to remove commits from a GitHub repository, the typical advice is to locally remove the commit (e.g., via git rebase -i) and force-push the modified branch over the original remote branch.

However, if the remote branch in question is part of a pull request, GitHub now helpfully displays the message

<user> force-pushed the <remote_branch> branch from <old-commit> to <new-commit>

and displays a link to view the diff between <old-commit> and <new-commit>. (This seems to be a recent change, I do not remember encountering this message before.)

According to another answer here, GitHub should periodically remove commits that are no longer referenced. But since they do reference the old commit in their UI, I suspect they will preserve it. (I haven't seen this apparently new feature for long enough to be sure and have not found any official mention of it yet.)

Is it possible to delete the remote commit in a way that prevents other users from seeing that commit? Or is it at least possible to remove the message above from the PR discussion page? Is there any other way to alter the contents of the underlying branch of a PR without everyone being able to access the old commits from the PR page (at least unless they know the commit hash)?

(This question is not about removing sensitive information such as passwords - I am aware that these should be considered compromised regardless of the possibility of removing the commit afterwards.)

like image 519
okwfj Avatar asked Dec 18 '18 01:12

okwfj


People also ask

How do you delete a commit from Github permanently?

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.

How do you remove a pushed commit from github?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit.

Does Force push delete commits?

To delete commits from remote, you will need to push your local changes to the remote using the git push command. Since your local history diverges from the remote history, you need to use the force option.


1 Answers

While typically done for expunging sensitive data, this will also work for removing a commit/file. From GitHub's docs:

If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history. To entirely remove unwanted files from a repository's history you can use either the git filter-branch command or the BFG Repo-Cleaner open source tool.

See above link to the documentation for usage of both.

like image 53
codenamev Avatar answered Sep 29 '22 05:09

codenamev