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

532    Asked by debbieJha in Data Science , Asked on Jul 8, 2021

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 synchronized 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 - through this would probably create a lot of work!

ETO: There are specific directories/files that are untracked - if possible I would like to maintain the untracking of these files. what is git initial commit? And What does git initial commit do?


Answered by Carole Thom

Here I am solving this question with a brute-force approach.

Note: This approach will not work if your repository has submodules. For repository having submodules, you should use interactive rebase.

Let’s see the brute-force approach

Step1: Remove all history(make sure you have the backup) using:

cat .git/config  # note 
rm -rf .git

Step2: Reconstruct the git repo with the current content:

git init
git add .
git commit -m "Initial commit"
Step3: Push the commit to github
git remote add origin
git push -u --force origin master

Thus, you can make current commit to the only commit in a git repository.

Git initial commit: For projects that already have some code files written and ready to go, the initial commit usually includes all of those code files in one big chunk. This commit acts as the starting revision for the project in Git. Developers will build off of this to add more features and functionality to the project.

What Does Git Initial Commit Do?

The git commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.



Your Answer

Interviews

Parent Categories