Home > Blockchain >  GIT how move main branch to another branch and clean the main branch?
GIT how move main branch to another branch and clean the main branch?

Time:11-26

by mistake I pushed to main branch my code, it was an empty project so main branch didn't have any commit

I need move the pushed commits to the main branch to another branch and keep the main branch as empty or at least with just a new readme file (the initial main branch was totally empty so no readme nor any other file) to make matters worse, the initial commit pushed to the main branch already contains code and not just a readme

My idea was renaming the branches but because main branch didn't have any initial commit I cannot restore to that state, I think that a cherry pick could not work neither for the same reason

is there an easy way to achieve this in a clear way? by the way, there are not other contributors in the project right now so restart the branch hopefully will not cause any trouble... thank guys!

CodePudding user response:

To create a branch with no commits, you can do

git checkout --orphan new-branch

This will create branch new-branch with no commits. All files in the working directory will become unstaged, but remain unchanged. You can now follow up with

git add README.txt
git commit

to create the first commit on new-branch that has only a readme text.

As next steps, you can now rename your branches

git branch -m main old-main
git branch -m new-branch main

to make the new branch main.

CodePudding user response:

You can clear wrong pushed data from master branch using below steps -

  1. Create a backup branch from master branch so if can have backup of pushed code.
  2. Checkout master branch in local and revert the wrong commits and push this master branch from local again so you master branch will clear and remain only old code.

I hope this might fix your issue.

Thanks

  • Related