Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the current commit the only (initial) commit in a Git repository?

I currently have a local Git repository, which I push to a Github repository.

The local repository has ~10 commits, and the Github repository is a synchronised duplicate of this.

What I'd like to do is remove ALL the version history from the local Git repository, so the current contents of the repository appear as the only commit (and therefore older versions of files within the repository are not stored).

I'd then like to push these changes to Github.

I have investigated Git rebase, but this appears to be more suited to removing specific versions. Another potential solution is to delete the local repo, and create a new one - though this would probably create a lot of work!

ETA: There are specific directories / files that are untracked - if possible I would like to maintain the untracking of these files.

like image 247
kaese Avatar asked Mar 13 '12 11:03

kaese


People also ask

How do I delete all previous commits?

Removing the last commit To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.

What is an initial commit?

The first commit that a developer makes when they start a new software project is called the initial commit.


2 Answers

Here's the brute-force approach. It also removes the configuration of the repository.

Note: This does NOT work if the repository has submodules! If you are using submodules, you should use e.g. interactive rebase

Step 1: remove all history (Make sure you have backup, this cannot be reverted)

cat .git/config  # note <github-uri> rm -rf .git 

Step 2: reconstruct the Git repo with only the current content

git init git add . git commit -m "Initial commit" 

Step 3: push to GitHub.

git remote add origin <github-uri> git push -u --force origin master 
like image 174
7 revs, 4 users 73% Avatar answered Sep 18 '22 03:09

7 revs, 4 users 73%


The only solution that works for me (and keeps submodules working) is

git checkout --orphan newBranch git add -A  # Add all files and commit them git commit git branch -D master  # Deletes the master branch git branch -m master  # Rename the current branch to master git push -f origin master  # Force push master branch to github git gc --aggressive --prune=all     # remove the old files 

Deleting .git/ always causes huge issues when I have submodules. Using git rebase --root would somehow cause conflicts for me (and take long since I had a lot of history).

like image 40
Zeelot Avatar answered Sep 21 '22 03:09

Zeelot