The main branch of my remote repository(github) is totally wrong, I have pushed the good code to another branch named kitos which is quite different from the main code. I want to "delete" the main branch content and put the kitos content inside main, I don't want any code that was on the main branch, only to put kitos content on the main content. What is the simplest way and correct way to change this?
2 Answers
There are many ways to solve such a situation, here are the (IMO) two simplest/most-common:
Soft reset and push
Arguably the safest option.
Keeping the origin/main
history, make the local main
branch what you want it to be and then push as normal:
$ git fetch
$ git checkout main
$ git pull
$ git reset --soft origin/kitos
$ git commit -m "Make match origin/kitos branch"
$ git push
This will:
- Make sure your local references are up to date
- Check out the main branch locally
- Make sure
main
matchesorigin/main
- Make the working copy match origin/kitos without changing history
- Committing the result of
- Push this one commit to
origin/main
This maintains the existing history in origin/main
, and achieves the desired end-state with a 'normal' commit; This is an appropriate thing to do if for example the repository if for you-only and you decide to replace one experimental solution with another.
Hard reset and force push
Ignoring the origin/main
history, make the local main
branch what you want it to be and then overwrite the remote branch:
$ git fetch
$ git checkout main
$ git reset --hard origin/kitos
$ git push --force-with-lease
This will:
- Make sure your local references are up to date
- Check out the main branch locally
- Rewrites history to make the local main branch match origin/kitos
- Replace the history of
origin/main
with the history oforigin/kitos
This obliterates the existing history in origin/main
, replacing it with the history of origin/kitos
. Be careful using this option, rewriting history can be problematic or confusing if there are any other contributors or clones, google 'is force push bad' or similar to find references like this one if in doubt :).

- 63,116
- 12
- 91
- 123
I would handle it with the following steps.
- Make 'kitos' the main branch.
- Delete the branch named 'main'
- Rename 'kitos' to 'main'

- 21
- 9
-
2This misses the final step required to force push the result to main. Implicitly that makes the branch shuffling unnecessary. – AD7six Apr 09 '23 at 13:31