简体   繁体   中英

Git New Master Branch

I have several old branches that have been merged to the master branch. I want to start fresh, ie, I want to delete all the previous branches except the master branch.

The master branch will be the new base branch with the latest changes for future development. How can I achieve this.

After this change, if someone checks out the project, they get a working master branch with all the functionality.

Any help with command examples will be appreciated. Thanks!

You can delete local branches with git branch -d or git branch -D (if the branch you want to delete hasn't been fully merged).

To delete brances on the remote, you can use git push REMOTE --delete BRANCH (or the git push REMOTE :BRANCH syntax if your git version is older). Make sure to substitute your actual remote's name there - the default remote is called origin , but you may have multiple remotes, etc.

'git branch -d' will delete the branch. If the branch is not merged and you want to forcefully delete it, instead of '-d' use '-D'.

May be a bit more than you need if you want to keep the original master , but if your really starting fresh you can use

git checkout --orphan new_master
git rm -r --cached .
git clean .
git commit --allow-empty -m 'initial commit'
git tag -a init -m 'repository start'

The git clean . may need to be run twice; cleaning the .gitignore file uncovers files that had been ignored.

What this does is create a new base branch with an empty commit to start over from and tags the first commit for easy reference.

Then using git rebase -I --onto init --root <whatever you want to move over> start bringing the commits you want to save over to this new branch.

When you have everything you want from your old branches, git branch -D <branch_name> removes the branch reference. if you need to remove a tag git tag -d <tag_name> .

Recreate any new branch names and tags you want to keep after they have been removed from the original.

Once the local repository looks the way you want, push it to origin with git push origin <local branch>:<remote_branch> --force . Old remote branches can be removed with git push origin :<remote_branch> --force .

At this point the commits that made up the old branches will be unreferenced and should not show up in a git log --all --oneline --graph and you new 'history' should look as you want it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM