3

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.
aynber
  • 22,380
  • 8
  • 50
  • 63
Noname
  • 91
  • 3
  • 11
  • 2
    I'd suggest starting from scratch, deleting the git repository (this will help you take git away from the folder without losing files) https://stackoverflow.com/questions/4754152/how-do-i-remove-git-tracking-from-a-project) and starting a new one. – AJFaraday Jun 03 '19 at 12:36

3 Answers3

6

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
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
1

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

Krupa Suthar
  • 670
  • 3
  • 14
1

You can also try the following steps with your repository:

  1. git checkout master: make sure you are in master.
  2. git checkout -b my-fancy-new-branch: create your new feature branch.
  3. git checkout master: switch back to master
  4. git reset --hard rootcommit: reset master to the state before your own commits.
  5. optional and if you have a remote you pull from: git pull --ff (if this fails because the pull is not fast-forward, you have to reconsider rootcommit. It contains some of your work)

I tried to do this on my test repository, it appears to work.

I have taken a reference from this answer, that can help in finding other approaches too.

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38