惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
博客园 - 【当耐特】
H
Help Net Security
腾讯CDC
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Last Week in AI
Last Week in AI
Jina AI
Jina AI
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
Y
Y Combinator Blog
C
Check Point Blog
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

Matthias Ott

Hello Again, World This, Still Not for Everyone The Shape of Friction WeissKlang L1 – Punching Above Its Weight Continvoucly Morged Value Webspace Invaders To Affinity and Beyond The Mystery of Storytelling Amateurs! Echoes of Connection Linear() Is Not (That) Linear View Transitions: The Smooth Parts Adding AVIF and WebP Support to My Craft CMS Site Challenge Acoustic Room Treatment and Building Sound Panels, Part 1: Planning Play On Overshoot The HTML Output Element Listening Closely Compressed Fluid Typography The Lifeblood of the Web What Could Go Wrong? That’s My Rank Making Space CSS :is() :where() the Magic Happens Visual Regression Testing for External URLs With Playwright Jane Goodall’s Famous Last Words European Tech Alternatives 🇪🇺 Independent Type Foundry Advent Calendar – Day 24: NaN Independent Type Foundry Advent Calendar – Day 23: Typotheque
How to Delete Your Commit History in Git
Matthias Ott · 2022-06-09 · via Matthias Ott

Maybe you want to publish a project but don’t want everyone to see what mess you created before your initial release. Maybe you want to hand over a Git repository to a third party who should not peek into your complete git commit history. Whatever the reason, here is how you can get rid of all past commits in a branch without losing your latest work.

Disclaimer: what follows are a few Git commands for the terminal. If you are a little bit like me, you probably prefer a GUI like Git Tower (❤️). But in this case, using the command line seems to be the fastest solution. Also, make sure to install Git on your machine. And probably don’t do this in an established repo that you share with a larger team as it might break stuff for them.

First, make sure you are in a your project root directory and checkout a new branch with the --orphan flag.

git checkout --orphan latest_branch

This will create a new, empty (“orphaned”) branch without any commits. Then, add all the files in your working directory:

git add -A

Now commit all your changes.

git commit -am "first commit message"

With all your work safely stored in the new branch, it is time to delete the old branch, e.g. the main branch:

git branch -D main

Now – and you probably already guessed it, right? – rename the current branch (the one you just created) into main:

git branch -m main

And finally, update the remote repository using the force of Git. Thanks to Frederic Hemberger for pointing out that --force-with-lease is probably better than using --force because it won’t overwrite the commits of others in case the branch has changed in the meantime.

git push --force-with-lease origin main

🙌🎉

I know all this might sound obvious to some of you and yes, I found this solution on StackOverflow, but I wanted to write it down to have it at hand the next time I need it. And maybe it is useful for you as well?

BTW, do you know what’s also useful when working with Git in the command line? Oh Shit, Git!

~

9 Webmentions

1 Repost

5 Likes

ⓘ Webmentions are a way to notify other websites when you link to them, and to receive notifications when others link to you. Learn more about Webmentions.