-3

I need some help using git.

I made this mistake to do all the work in the master branch. Due to some issues with that I want to do the following:

  1. Save the current state of the master branch in another branch (e.g. the not yet existing branch backup).
  2. Reset the master branch to the initial commit

I did not find any solution for this problem yet, may someone of you help me? Best regard

Derek Brown
  • 4,232
  • 4
  • 27
  • 44
dreichen
  • 21
  • 4
  • 4
    Possible duplicate of [How to revert Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit) – Derek Brown Nov 29 '17 at 20:03

2 Answers2

1

First, you need to find the hash of the initial commit:

git log --all --grep='initial commit'

Then create a backup branch:

git checkout -b backup

Finally, return to the master branch and reset to the initial commit:

git checkout master
git reset --hard <HASH>
Derek Brown
  • 4,232
  • 4
  • 27
  • 44
0

Create the backup branch.

git checkout master
git branch backup

If you want to keep one or more of the original commits on master, then you can just use reset to move the master branch to an earlier commit. You need a name for the target commit. This could be the commit ID. Or if you know that it's, say, 5 commits before the commit at the current tip of the master branch, you could say master~5. If the name were T you would say

git reset --hard T

If you don't want master to keep any of the existing commits, then instead you can do this:

git checkout --detach
git branch -D master
git checkout --orphan master

This will revert master to the state of an "unborn" branch, and your next commit will start a new history. Don't forget that your work tree and index may still contain state from the backup branch, so make sure you get the index looking how you want it before committing.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52