How to push only the latest commit to GitHub without previous history?

I’ve got a problem with my local git repo. There are many commits with sensitive info like connection strings. I don’t want to upload this history to GitHub.

Is there a way to push everything I have now but get rid of the old commits? I thought about using branches for development and only merging to master before pushing. That way master would only have the commit I want.

I tried using git rebase -i HEAD~3 to go back 3 commits and remove one. But I ran into issues with auto cherry-pick failing. It got pretty complicated.

Should I just delete the history and start over? Or is there an easier way to do this? Any advice would be great!

# Example of what I want to avoid
git log
commit abc123 (HEAD -> master)
  Latest changes
commit def456
  Added sensitive data
commit ghi789
  Initial commit

# What I want to push
git log
commit abc123 (HEAD -> master)
  Latest changes

Thanks for any help!

I’ve encountered this issue before, and there’s a straightforward solution. Create a new branch, make a single commit with your current state, and then force push it to GitHub. Here’s how:

git checkout --orphan temp_branch
git add -A
git commit -m 'Fresh start with latest changes'
git branch -D main
git branch -m main
git push -f origin main

This approach effectively creates a new history with just one commit. It’s clean, simple, and achieves exactly what you’re looking for. Just be cautious with force pushing, as it overwrites remote history. Make sure you’re the only one working on this repository, or communicate with your team before proceeding.

hey, i’ve been there! instead of messing with rebase, try this:

git checkout --orphan new_branch
git add -A
git commit -m 'clean start'
git branch -D master
git branch -m master

now u got a fresh master with just 1 commit. push that to github and ur good!