🗑️ How to Delete All Previous Commits and Fresh Push#
This process allows you to destroy all commit history on a branch and create a single, clean ‘Initial commit’ containing the current state of your files. This is often done for repository cleanup.
⚠️ WARNING: This action is destructive and irreversible. It completely overwrites the remote branch history. Ensure you have backed up any necessary data before proceeding.
Steps#
1. Check Your Branch and Current Status#
Ensure you are in the correct repository and know the branch you intend to clean (e.g., main).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| # Check your current branch
git branch --show-current
2. Create a New, Orphaned Branch
An orphaned branch has no history linked to its parent.
Bash
# Replace 'main' with the branch you want to clean up
git checkout --orphan temp_branch
3. Stage Your Current Files
The orphaned branch contains the files from the old history. Stage them for the new initial commit.
Bash
# Stage all current files
git add -A
4. Commit as the New Initial Commit
Create the single, new root commit.
Bash
# Commit the current state as the one and only commit
git commit -m "Initial commit"
5. Delete the Old Branch Locally
Remove the branch that held the unwanted history.
Bash
# Delete the old branch locally (replace 'main' with your branch name)
git branch -D main
6. Rename the New Branch
Rename your temporary branch back to the original, desired branch name.
Bash
# Rename 'temp_branch' to 'main'
git branch -m temp_branch main
7. Force Push to the Remote
This step overwrites the remote history with your new, clean, single-commit history.
Bash
# Force push the new, clean history to the remote
git push -f origin main
Result: Your remote branch (main) now has only the single "Initial commit."
***
**To save this as a file:**
1. **Copy** the entire content of the code block above.
2. Open your preferred **text editor** (like VS Code, Notepad++, or TextEdit).
3. **Paste** the content.
4. Save the file as **`reset_git_history.md`**.
|