How to delete commit history in github

How to delete commit history in github

Delete Github commit history

Is there anyway for me to delete my github commit history? I don’t want to lose all my commits, just the last 10 or so that were pushed to github. I can’t seem to figure out how to do this.

4 Answers 4

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

Just pick the hash you want to go back to and in your clone do:

where branch is the name of the branch you want to push. Voilà. Be carefully with the force push. You may want to copy your working directory until you are familiar with it.

If you want to do this, you can use HEAD

It is not recommended to do delete server commit history, especially: if you have a team of people working on this repo.

If you have a team of people working on this, I would recommend to fall back by adding another commit that undo your code you don’t want.

Paste the SHA here. Go to your project’s directory.

Force the push to the master, otherwise it will block your attempt to erase your history.

Delete your grafts file.

NEVERMIND

Well, my answer is only half the battle. My public activity RSS feed still has links to all the diffs that detail the very information I wanted to remove. Apparently, there is no way to delete this, but you should probably read Change your Password, and update any sensitive data you may have accidentally uploaded.

Removing sensitive data from a repository

In this article

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-repo tool or the BFG Repo-Cleaner open source tool.

The git filter-repo tool and the BFG Repo-Cleaner rewrite your repository’s history, which changes the SHAs for existing commits that you alter and any dependent commits. Changed commit SHAs may affect open pull requests in your repository. We recommend merging or closing all open pull requests before removing files from your repository.

Warning: This article tells you how to make commits with sensitive data unreachable from any branches or tags in your repository on GitHub.com. However, those commits may still be accessible in any clones or forks of your repository, directly via their SHA-1 hashes in cached views on GitHub, and through any pull requests that reference them. You cannot remove sensitive data from other users’ clones or forks of your repository, but you can permanently remove cached views and references to the sensitive data in pull requests on GitHub by contacting GitHub Support.

Once you have pushed a commit to GitHub, you should consider any sensitive data in the commit compromised. If you committed a password, change it! If you committed a key, generate a new one. Removing the compromised data doesn’t resolve its initial exposure, especially in existing clones or forks of your repository. Consider these limitations in your decision to rewrite your repository’s history.

Purging a file from your repository’s history

You can purge a file from your repository’s history using either the git filter-repo tool or the BFG Repo-Cleaner open source tool.

The BFG Repo-Cleaner is a tool that’s built and maintained by the open source community. It provides a faster, simpler alternative to git filter-branch for removing unwanted data.

For example, to remove your file with sensitive data and leave your latest commit untouched, run:

To replace all text listed in passwords.txt wherever it can be found in your repository’s history, run:

After the sensitive data is removed, you must force push your changes to GitHub. Force pushing rewrites the repository history, which removes sensitive data from the commit history. If you force push, it may overwrite commits that other people have based their work on.

See the BFG Repo-Cleaner’s documentation for full usage and download instructions.

Using git filter-repo

Install the latest release of the git filter-repo tool. You can install git-filter-repo manually or by using a package manager. For example, to install the tool with HomeBrew, use the brew install command.

For more information, see INSTALL.md in the newren/git-filter-repo repository.

If you don’t already have a local copy of your repository with sensitive data in its history, clone the repository to your local computer.

Navigate into the repository’s working directory.

Run the following command, replacing PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename. These arguments will:

Note: If the file with sensitive data used to exist at any other paths (because it was moved or renamed), you must run this command on those paths, as well.

Double-check that you’ve removed everything you wanted to from your repository’s history, and that all of your branches are checked out.

Once you’re happy with the state of your repository, force-push your local changes to overwrite your repository on GitHub.com, as well as all the branches you’ve pushed up. A force push is required to remove sensitive data from your commit history.

In order to remove the sensitive file from your tagged releases, you’ll also need to force-push against your Git tags:

Fully removing the data from GitHub

After using either the BFG tool or git filter-repo to remove the sensitive data and pushing your changes to GitHub, you must take a few more steps to fully remove the data from GitHub.

Contact GitHub Support, asking them to remove cached views and references to the sensitive data in pull requests on GitHub. Please provide the name of the repository and/or a link to the commit you need removed.

Tell your collaborators to rebase, not merge, any branches they created off of your old (tainted) repository history. One merge commit could reintroduce some or all of the tainted history that you just went to the trouble of purging.

After some time has passed and you’re confident that the BFG tool / git filter-repo had no unintended side effects, you can force all objects in your local repository to be dereferenced and garbage collected with the following commands (using Git 1.8.5 or newer):

Note: You can also achieve this by pushing your filtered history to a new or empty repository and then making a fresh clone from GitHub.

Avoiding accidental commits in the future

There are a few simple tricks to avoid committing things you don’t want committed:

How to permanently delete a commit from Git’s history?

How can I permanently delete a commit from Git’s history?

How to delete commit history in github. Смотреть фото How to delete commit history in github. Смотреть картинку How to delete commit history in github. Картинка про How to delete commit history in github. Фото How to delete commit history in github

5 Answers 5

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

Delete the file from a checkout

Github has a useful page how to permanently delete file(s) from a repository, in brief:

That would remove the file from all branches. Then to recover the space locally:

Recovering space on the git server

Force pushing does not remove any commits/objects on the remote server. If you don’t want to wait for git to clean up itself, you can run it explicitly on the server:

Teammates

The first is very simple, the second means one of two things:

User has no local commits

That would make any local checkout exactly match the remote

User does have local commits

User cleanup

Then (optionally, because git won’t push unreferenced commits to the server) recover space, and everyone has a consistent slimmer repository state:

Note that after the cleanup you should run git gc to get Git to recognise it doesn’t need to store those big objects anymore and free-up disk space in that copy of the repository. git gc usually happens periodically on most hosted versions of Git, so when you push the cleaned history to your main Git server, that server will eventually free-up it’s disk space too. Perhaps surprisingly, you don’t have to wait for that git gc to run before users cloning fresh copies of your cleaned repo get just the cleaned history.

Full disclosure: I’m the author of the BFG Repo-Cleaner.

Delete and completely remove the commit from git history

I have a commits in my git history

In that 3rd and 4th commit contains a wrong code, but unknowingly I committed and pushed.

But 5th and 6th commits contains a correct code. I need this commit to work.

Now I want to Delete and Remove completely whatever I changed in 3rd and 4th commit.

I want delete 3rd and 4th commit from the git history. But not 5th and 6th.

So that my branch will be safe.

2 Answers 2

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

You can use interactive rebase in order to go back in your commit history and do things differently. For using interactive rebase, just:

is the parent of the last commit you want to edit. After executing this command, just put d or drop in front of commits you’re gonna delete or even delete the line corresponding to that commit.

Running this command gives you a list of commits in your text editor that looks something like this:

The interactive rebase gives you a script that it’s going to run. It will start at the commit you specify on the command line and replay the changes introduced in each of these commits from top to bottom. It lists the oldest at the top, rather than the newest, because that’s the first one it will replay.

But since you’ve pushed the branch to your upstream repository, do communicate this history rewrite to your possible colleagues, as the git documentation says:

Remember again that this is a rebasing command – every commit included in the range will be rewritten, whether you change the message or not. Don’t include any commit you’ve already pushed to a central server – doing so will confuse other developers by providing an alternate version of the same change.

How to remove the first commit in git?

I am curious about how to remove the first commit in git.

What is the revision before committing any thing? Does this revision have a name or tag?

How to delete commit history in github. Смотреть фото How to delete commit history in github. Смотреть картинку How to delete commit history in github. Картинка про How to delete commit history in github. Фото How to delete commit history in github

9 Answers 9

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

For me, the most secure way is to use the update-ref command:

If what you want is to merge the first commit with the second one, you can use the rebase command:

A last way could be to create an orphan branch, a branch with the same content but without any commit history, and commit your new content on it:

There is nothing before the first commit, as every commit is referring a parent commit. This makes the first commit special (an orphan commit), so there is no way to refer to a previous «state».

How to delete commit history in github. Смотреть фото How to delete commit history in github. Смотреть картинку How to delete commit history in github. Картинка про How to delete commit history in github. Фото How to delete commit history in github

I was looking for a way to undo all git commits from a repo, like they never happened.

Rebasing will work up to a point. However, the very first (chronologically the oldest git commit) is always going to be problematic, since it does not have a parent, so that will error out.

None of these answers quite solved it for me. But after lots of searching and trial-and-error, I found this to work!

Hope this helps you. Have a great day.

How to delete commit history in github. Смотреть фото How to delete commit history in github. Смотреть картинку How to delete commit history in github. Картинка про How to delete commit history in github. Фото How to delete commit history in github

Another way you can do is:

If you want to keep other branches, but for example make the master branch start anew without common history to other branches, one safe way to achieve this is to create a new repository, and push contents of that in your old one:

If you want to destroy all branches and all existing content, then just run

If you have just committed it but not pushed it then just remove .git directory and git init again.

How to delete commit history in github. Смотреть фото How to delete commit history in github. Смотреть картинку How to delete commit history in github. Картинка про How to delete commit history in github. Фото How to delete commit history in github

The answer to the question depends on whether:

You want to remove the first AND ONLY commit on a branch (whilst leaving other branches as they were), or whether

You want to remove the first commit on some branch, whilst ‘leaving in place’ the subsequent commits (and the other branches).

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *