How to remove the first commit in git?

I am curious about how to remove the first commit in git.

What is the revision before committing any thing? Does this revision have a name or tag?

  • Git: Manage Version Control on Multiple Single Files in a Repo
  • git -submodules / repo - How to use commit and branch for a bunch of branches in separate modules?
  • GIT remote cannot fetch via SSH
  • How to git clone a repo in windows from other pc within the LAN?
  • Git hosted on my server + client
  • How to Do a Complicated Git Repository Merger?
  • Git repo sync error: branch XYZ is published (but not merged) and is now 1 commits behind
  • Can two identically structured git repos be merged when they have never had any common history?
  • 6 Solutions collect form web for “How to remove the first commit in git?”

    For me, the most secure way is to use the update-ref command:

    git update-ref -d HEAD

    It will delete the named reference HEAD, so it will reset (softly, you will not lose your work) all your commits of your current branch.

    If what you want is to merge the first with the second one, you can use the rebase command:

    git rebase -i --root

    A last way could be to create an orphan branch, a branch with the same content but without any commit history, and commit your new content on it:

    git checkout --orphan <new-branch-name>

    There is nothing before the first commit, as every commit is referring a parent commit. This makes the first commit special (an orphan commit), so there is no way to refer to a previous “state”.

    So if you want to fix the commit, you can simply git commit --amend: this will modify the commit without creating another one.

    If you just want to start all over, delete the .git repository, and make another one with git init

    You might just want to edit your first commit (as there is always a first commit in a git repo). Consider using git commit --amend --reset-author instead of the usual git commit --amend.

    If you want to keep other branches, but for example make the master branch start anew without common history to other branches, one safe way to achieve this is to create a new repository, and push contents of that in your old one:

    cd ..
    git init newrepo
    cd newrepo
    # make some initial commits
    git push ../oldrepo master:newmaster

    This creates “newmaster” branch in the old repository, with history that is not common with any of the other branches. Of course, you can just overwrite the master as well, with `git push -f´.

    If you want to destroy all branches and all existing content, then just run rm -rf .git/.

    Another way you can do is:

    1. Checkout to a branch you want to keep (say dev) git checkout dev
    2. Now, delete the branch you want to reset git branch -D master
    3. Now, create an empty branch with the same name git checkout --orphan master

    Ofcourse, all of this would depend on your usecase, but if you have more than one branch, deleting the .git directory does not make sense.

    Check out to a temporary branch:

    git checkout –orphan TEMP_BRANCH

    Add all the files:

    git add -A

    Commit the changes:

    git commit -am “Initial commit”

    Delete the old branch:

    git branch -D master

    Rename the temporary branch to master:

    git branch -m master

    Finally, force update to our repository:

    git push -f origin master

    Git Baby is a git and github fan, let's start git clone.