< Voltar ao Blog

Reset a git repo commit history

• Carregando...

sometimes you have a git repo that has accumulated years of commits, experimental branches, large files, secrets, broken history, or just a bunch of commits that you don’t want anymore.

maybe you initialized git in the wrong directory.

maybe you accidentally committed build artifacts.

maybe you want to turn an existing project into a completely new repo.

or maybe you simply want to delete the entire git history and start with a single clean commit.

whatever the reason, git makes it possible to completely reset a repo.

before you start

be very careful with these commands.

deleting Git history is destructive.

once you remove your local .git directory, old commits are no longer available through that local repo.

if you force-push a new history to a remote repo, the old history will no longer be part of the branch history either.

before resetting anything, make sure you don’t need the old commits.

if the repo is shared with other developers, coordinate with them first. Rewriting history can cause problems for anyone who has already cloned the repo.

if you are unsure, create a backup first.

create a backup

the simplest backup is to copy the entire project directory somewhere else.

you can also create a bundle containing the complete git repo:

git bundle create repo-backup.bundle --all

this creates a portable backup containing all reachable Git objects and references.

you can later clone the bundle:

git clone repo-backup.bundle repo-backup

another option is to simply rename the existing .git directory:

mv .git .git-backup

on Windows PowerShell:

Rename-Item .git .git-backup

this preserves your entire Git history while allowing you to create a completely new .git directory.

if you are absolutely sure that you don’t need the old history, you can delete it instead.

check your current repo

before doing anything destructive, check the current state:

git status

check the configured remotes:

git remote -v

check the current branch:

git branch --show-current

and check your commit history:

git log --oneline --decorate --graph --all

this gives you a quick overview of what you are about to replace.

completely reset the local repo

if you want to remove all Git history from your project and start again, the simplest approach is to delete the .git directory.

Linux and macOS

from the root of your project:

rm -rf .git

Windows PowerShell

Remove-Item -Recurse -Force .git

Windows Command Prompt

rmdir /s /q .git

after this, the directory is no longer a Git repo.

all commits, branches, tags, remotes, and Git configuration stored inside .git are gone.

your actual project files are not affected.

you can verify this:

git status

Git should respond with something similar to:

fatal: not a git repo (or any of the parent directories): .git

this means the Git history has been successfully removed.

initialize a new repo

now initialize Git again:

git init

set your default branch to main:

git branch -M main

your project is now a completely new Git repo.

check the status:

git status

you should see all your project files listed as untracked.

create the first commit

add all files:

git add .

check what is going to be committed:

git status

then create your new initial commit:

git commit -m "Initial commit"

you now have a completely fresh Git history containing only one commit.

check it:

git log --oneline

you should see something similar to:

a1b2c3d Initial commit

the old Git history is no longer part of this repo.

reconnect the remote repo

if your project already has a remote repo on GitHub, GitLab, Codeberg, or another Git hosting service, you can connect the new local repo to it.

add the remote:

git remote add origin [email protected]:username/repo.git

verify it:

git remote -v

you should see:

origin  [email protected]:username/repo.git (fetch)
origin  [email protected]:username/repo.git (push)

replace the remote history

this is the important part.

if the remote repo already contains the old Git history, simply running:

git push

will usually fail because your new repo has an unrelated history.

you need to replace the remote branch with your new history.

first, make sure you are on main:

git branch -M main

then force-push:

git push -u origin main --force

this replaces the remote main branch with your new local main branch.

the remote repo will now point to your new initial commit instead of the previous history.

use –force-with-lease instead

when rewriting history, --force-with-lease is generally safer than --force.

use:

git push -u origin main --force-with-lease

--force-with-lease checks that the remote branch hasn’t changed unexpectedly since you last fetched it.

if someone else pushed new commits to the remote branch, Git can reject your push instead of silently overwriting their work.

for this reason, prefer:

git push --force-with-lease

over:

git push --force

when possible.

however, if you are intentionally replacing a remote repo and understand the consequences, both commands can be used.

completely reset a repo including all branches

deleting .git and running git init automatically removes your local branches and tags because they were stored in the old .git directory.

however, the remote repo may contain other branches.

for example:

main
develop
feature/test
old-experiment

force-pushing main only replaces the history of main.

the other remote branches still exist.

if you want the remote repo to contain only your new main branch, you need to delete the old remote branches.

first, list remote branches:

git branch -r

you may see:

origin/main
origin/develop
origin/feature/test
origin/old-experiment

delete a remote branch with:

git push origin --delete develop

or:

git push origin --delete feature/test

repeat this for every branch you no longer want.

afterward, your remote repo should contain only the branches you intentionally kept.

delete all remote tags

tags can also preserve references to old commits.

list your tags:

git tag

to delete a local tag:

git tag -d v1.0.0

to delete a remote tag:

git push origin --delete v1.0.0

you can also use the ref syntax:

git push origin :refs/tags/v1.0.0

if you are completely resetting a repo, review your tags as well as your branches.

completely recreate a repo

sometimes you don’t want to overwrite the existing remote repo.

instead, you want to create a brand-new repo.

this is often the cleanest approach.

create a new empty repo on your Git hosting provider.

do not initialize it with a README, .gitignore, or license if you already have those files locally.

then remove the old Git history:

rm -rf .git

initialize the repo again:

git init
git branch -M main

add your files:

git add .

create the first commit:

git commit -m "Initial commit"

add the new remote:

git remote add origin [email protected]:username/new-repo.git

push:

git push -u origin main

this gives you a completely new repo without touching the old one.

this approach is preferable when you want to preserve the original repo as an archive.

remove Git history but keep the remote repo

if you want to reset the history of an existing remote repo, the process looks like this:

rm -rf .git
git init
git branch -M main
git add .
git commit -m "Initial commit"
git remote add origin [email protected]:username/repo.git
git push -u origin main --force-with-lease

the important part is:

git push -u origin main --force-with-lease

without the force option, Git will normally reject the push because the remote branch contains commits that your new local repo doesn’t have.

reset only the latest commits

you don’t always need to delete the entire repo history.

if you only want to undo the latest commit, use:

git reset --soft HEAD~1

this removes the latest commit but keeps your changes staged.

or:

git reset HEAD~1

this removes the latest commit and unstages the changes while keeping the files modified.

or:

git reset --hard HEAD~1

this removes the latest commit and deletes the associated changes from your working directory.

be careful with --hard.

any uncommitted changes that are reset this way may be permanently lost.

reset to a specific commit

you can also reset your repo to a specific commit.

find the commit hash:

git log --oneline

then:

git reset --hard <commit-hash>

for example:

git reset --hard a1b2c3d

this moves your current branch back to that commit.

if the branch has already been pushed to a remote, you will need to force-push the rewritten history:

git push --force-with-lease

this is different from completely restarting the repo because the older history still exists before the selected commit.

remove sensitive data from Git history

if you accidentally committed a password, API key, private key, or other secret, simply deleting the file and creating a new commit is not enough.

the secret still exists in the old Git history.

in this situation, you should rotate or revoke the exposed secret immediately.

after that, you can rewrite the repo history using tools such as git filter-repo.

for example, to remove a file from all history:

git filter-repo --path path/to/secret.txt --invert-paths

after rewriting history, you will need to force-push the affected branches and tags.

this is a more advanced operation and should be handled carefully, especially in shared repositories.

if the secret was exposed publicly, assume it is compromised even after rewriting the Git history.

remove a file from the latest commit

if you accidentally added a file to your latest commit and haven’t pushed it yet, you can remove it from the commit.

first, remove it from Git tracking:

git rm --cached path/to/file

add it to .gitignore if necessary:

echo "path/to/file" >> .gitignore

then amend the commit:

git add .gitignore
git commit --amend --no-edit

the file will no longer be part of the latest commit.

however, this does not remove the file from older commits.

if the file was committed earlier in the history, you need to rewrite the history.

remove all Git metadata but keep the project files

sometimes you don’t want to preserve any Git configuration at all.

the .git directory contains everything related to the repo, including:

  • commit history
  • branches
  • tags
  • remote configuration
  • hooks
  • Git settings
  • object database

deleting .git removes all of this.

your source code and other project files remain untouched.

for Linux and macOS:

rm -rf .git

for Windows PowerShell:

Remove-Item -Recurse -Force .git

for Windows CMD:

rmdir /s /q .git

after this, you have a normal directory again.

you can initialize it as a new repo whenever you want:

git init

the complete clean-slate workflow

if your goal is to completely restart an existing repo while keeping the current project files, the complete process is:

# remove the old repo
rm -rf .git

# create a new repo
git init

# use main as the default branch
git branch -M main

# review your files
git status

# stage everything
git add .

# create a fresh history
git commit -m "Initial commit"

# add the remote repo
git remote add origin [email protected]:username/repo.git

# replace the remote main branch
git push -u origin main --force-with-lease

on Windows PowerShell, replace the first command with:

Remove-Item -Recurse -Force .git

after this process, your local repo has one commit and your remote main branch has been replaced with that new history.

final checklist

before resetting a repo, ask yourself:

  • do I have a backup of the old history?
  • do I need any old commits?
  • do I need any old branches?
  • do I need any old tags?
  • is this repo shared with other developers?
  • does anyone else have a clone of the repo?
  • am I removing secrets or sensitive information?
  • do I need to rotate exposed credentials?
  • am I sure I want to force-push the new history?

the most important thing to remember is that there are several levels of “resetting” a Git repo.

you can undo a single commit.

you can reset a branch to an earlier commit.

you can rewrite the entire history.

or you can delete the .git directory and create an entirely new repo.

choose the least destructive option that solves your problem.

if you truly want to start from scratch, deleting .git, initializing a new repo, creating a new initial commit, and force-pushing the new main branch is the simplest and most complete approach.