Delete All Commit History of A Certain GitHub Repository
StackExchange answer1 gives a way to delete all commit history of a certain GitHub repository:
Deleting the .git
folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:
-
Checkout/create orphan branch (this branch won’t show in
git branch
command):1
git checkout --orphan latest_branch
-
Add all the files to the newly created branch:
1
git add -A
-
Commit the changes:
1
git commit -am "commit message"
-
Delete
main
(default) branch (this step is permanent):1
git branch -D main
-
Rename the current branch to
main
:1
git branch -m main
-
Finally, all changes are completed on your local repository, and force update your remote repository:
1
git push -f origin main
PS: This will not keep your old commit history around. Now you should only see your new commit in the history of your git repository.
The answer is detailed. However, during the process of using it for my blog image hosting repository2, an error occurred when I executed the last piece of code (pushing code):
1
fatal: unable to access 'https://github.com/HelloWorld-1017/blog-images.git/': Failed to connect to github.com port 443 after 21082 ms: Timed out
This is because I didn’t use the right connection port34.
My computer is using a proxy (VPN) to access GitHub et al. websites, and it works. Previously, I always used GitHub Desktop to pull, commit, and push changes to GitHub repositories, rather than native Git Bash. Like other applications, GitHub Desktop could use proxy by default, but Git Bash couldn’t3. So, when I executed above commands in Git Bash and encountered the last pushing command, the error happened. To solve this problem, I need to configure proxy using commands like4:
1
2
git config --global http.proxy http://127.0.0.1:<port>
git config --global https.proxy http://127.0.0.1:<port>
where <port>
is proxy port, which can be found in (for Windows 11):
After deleting commit history, the repository looks like:
which show the deletion is successful.
However, the corresponding statistics of commit doesn’t disappear:
and repository size doesn’t decrease (still 884 MB), either:
that is my initial purposes of deleting commit history aren’t reached 😂 Perhaps the website hasn’t updated. Maybe I should check it out again tomorrow.
Jan. 17, 2025
To date, previous amount of commits didn’t disappear:
but the repository size has been decreased:
Maybe this feature is just as it.
References