Git: How to reset a remote Git repository to remove all commits?
Categories:
Git: How to Reset a Remote Repository and Erase All Commits

Learn how to completely wipe the commit history of a remote Git repository, effectively resetting it to an empty state. This guide covers the necessary steps and important considerations for this powerful operation.
Resetting a remote Git repository to remove all commits is a drastic but sometimes necessary action. This might be required if you've accidentally pushed sensitive data, want to start fresh with a clean history, or need to completely re-architect your project's version control from scratch. Unlike a simple revert or git reset --hard
on a local branch, resetting a remote repository involves force-pushing changes that rewrite history, which can have significant implications for collaborators.
Understanding the Impact of a Remote Reset
Before proceeding, it's crucial to understand that resetting a remote repository is a destructive operation. It will permanently delete all commit history from the remote server. Any local clones of the repository will become out of sync and will require special handling to either re-clone or force-update. This action should only be performed when you are absolutely certain you want to discard all previous history and understand the consequences for anyone else working on the repository.
flowchart TD A[Start Reset Process] --> B{Confirm Intent to Erase History?} B -- No --> C[Abort] B -- Yes --> D[Backup Important Data Locally] D --> E[Create New Orphan Branch Locally] E --> F[Commit Initial State on Orphan Branch] F --> G[Force Push to Remote (e.g., origin/main)] G --> H[Inform Collaborators] H --> I[End Reset Process]
Flowchart of the remote Git repository reset process.
Step-by-Step Guide to Resetting Your Remote Repository
This process involves creating a new, empty branch locally, committing your desired initial state to it, and then force-pushing this new branch to overwrite the existing remote branch. This effectively erases all previous history on the remote.
1. Step 1: Backup Your Repository (Optional but Recommended)
Before doing anything irreversible, make a full backup of your local repository. You can do this by simply copying the entire repository directory to another location. This ensures you have a copy of the old history and files in case something goes wrong or you need to retrieve something later.
2. Step 2: Create and Switch to an Orphan Branch
An orphan branch is a branch that has no history connected to any other branch. It starts completely fresh. Use the following command to create and switch to an orphan branch named new_start
(you can name it anything you like):
3. Step 3: Remove All Files from the Working Directory (Except .git
)
Once on the orphan branch, your working directory will still contain files from the previous branch. You need to remove them to prepare for a clean slate. Be careful with this step.
4. Step 4: Add Your Desired Initial Files and Commit
Now, add the files you want to be the very first commit in your new history. If you want a completely empty repository, you might just add a .gitignore
or README.md
file. If you want to keep the current state of your project but erase its history, copy your project files back into the directory and add them.
5. Step 5: Force Push to Overwrite the Remote Branch
This is the critical step where you overwrite the remote branch. Replace main
with the name of the branch you want to reset on the remote (e.g., master
, develop
).
6. Step 6: Inform Collaborators
If others are working on this repository, they must be informed immediately. Their local repositories will be out of sync and they will need to re-clone the repository or perform a specific git pull --rebase
or git reset --hard
to align with the new remote history.
# Step 2: Create and switch to an orphan branch
git checkout --orphan new_start
# Step 3: Remove all files from the working directory
git rm -rf .
# Step 4: Add your desired initial files and commit
# Example: Create a new README.md
echo "# New Project Start" > README.md
git add README.md
git commit -m "Initial commit for new project history"
# Step 5: Force push to overwrite the remote branch
git push -f origin new_start:main
# Optional: Delete the temporary orphan branch locally
git branch -D new_start
Commands to reset a remote Git repository to a new, clean state.
git push -f
(or git push --force
) command is extremely powerful and dangerous. It overwrites remote history without warning. Use it with extreme caution and only when you fully understand the implications. Always double-check the branch name before executing.Handling Collaborators After a Remote Reset
After you've force-pushed and reset the remote repository, any collaborators will find their local branches diverge significantly from the remote. They will not be able to simply git pull
. They have two main options:
- Re-clone the repository: This is the safest and most straightforward option for collaborators. They should delete their old local clone and then clone the repository again from scratch.
- Force update their local branch: If re-cloning is not feasible, they can force their local branch to match the new remote history. This will discard any uncommitted local changes and align their branch with the new remote
main
(or whatever branch was reset).
# Option 2 for collaborators: Force update local branch
# WARNING: This will discard local changes and history!
git fetch origin
git reset --hard origin/main
Commands for collaborators to force update their local repository after a remote reset.
git push --force-with-lease
instead of git push -f
. --force-with-lease
is a safer alternative because it only force-pushes if the remote branch hasn't been updated by someone else since your last pull, preventing accidental overwrites of other people's work.