Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset master to an empty state

I created a new Rails app and pushed the code to Github directly from master (first commit in the repository). However I made a mistake, I didn't want to commit this new Rails app directly from master, but instead create a new branch from master and push the new Rails app from this new branch.

Therefore, I'd like to:

  1. Delete the commit from master in Github(remote), so master is EMPTY
  2. create a new branch from master and add the previous commit that was in master into this new branch.
  3. push it to Github.
like image 885
Noname Avatar asked Nov 21 '25 10:11

Noname


2 Answers

Delete the commit from the master in Github(remote), so master is EMPTY

You can create an orphan branch - orphan branch is branch without any history

# Create "clean" branch
git checkout --orphan <name>

# remove all existing content if you wish
git clean -Xdf && git clean -xdf

create a new branch from master and add the previous commit that was in master into this new branch.

few options:

# Option 1 - Start your branch from the last desired commit
git checkout -b <name> <SHA-1> 

# Option 2 - Set your branch to the desired commit 
git reset <SHA-1> --hard

# Add the required commit on top of your branch
git cherry-pick <SHA-1>

push it to Github.

# force the update of the new branch
git push <origin> master -f
like image 72
CodeWizard Avatar answered Nov 24 '25 02:11

CodeWizard


Please create a new branch from the master with the following command,

git checkout -b branch_name

After that checkout into your new branch and push it to Github.

Now go to the master branch and remove the last commit and push it to Github

like image 27
Krupa Suthar Avatar answered Nov 24 '25 04:11

Krupa Suthar