I would like to completely empty the master branch in Git. For now, I would also like to keep all other branches which have been branched from master.
Is this possible and how?
I would like to completely empty the master branch in Git. For now, I would also like to keep all other branches which have been branched from master.
Is this possible and how?
That's actually called "delete old master branch and create new from scratch"
This will create a new master branch pointing to initial commit:
git branch -D master
git checkout -b master <initial commit hash>
This will create a totally new master branch unrelated to whatever you had:
git branch -D master
git checkout --orphan master
git rm -rf *
But actually you can simply save current repository to some other place and create a new repository instead.
First, you need to move or delete your current master branch. Personally, I prefer to move it aside rather than delete it. After that, you simply create a new branch with no parents by using the --orphan
flag. For example:
git branch -m master old_master
git checkout --orphan master
Since the current branch now has no history, some commands may fail with errors like fatal: bad default revision 'HEAD'
until after you make your first commit on the new master branch. This is normal, and is the same behavior you see in freshly-initialized repositories.
Other answers suggest creating a fresh master branch (as you would have in empty repository). But having master without history is not a good practice. It makes it hard to use rebase as you can never rebase the first commit. The first commit of master on any repository should be empty. There is a low-level plumbing solution to this:
First you get an empty tree hash:
$ git hash-object -t tree /dev/null
4b825dc642cb6eb9a060e54bf8d69288fbee4904
Then you use the tree hash to create an empty commit
$ git commit-tree -m "inital commit" 4b825dc642cb6eb9a060e54bf8d69288fbee4904
a5c0737b92e5e5d4502e15b93d7a46d1e17b2b22
And finally you reset master to that commit
$ git reset --hard a5c0737b92e5e5d4502e15b93d7a46d1e17b2b22
Here is something that is probably bad practice but works in a jam (building on top of aragaer's solution)
git branch -D master
git checkout --orphan master
git rm -rf *
After this, to submit a PR into this branch from a development branch currently being worked on without issue, do
git checkout development-branch-name
git rebase master
git push -f development-branch-name
Now you can submit a PR from development-branch-name
into master
without running into There isn't anything to compare. Nothing to compare, branches are entirely different commit histories
issue on GitHub.
I use this when I init a repo in a currently-underway project directory and want code review done via the GitHub UI on all the code in the project directory.
You could try to reset the master branch to the first commit with git checkout master; git reset --hard ${sha-of-first-commit}
and then amend that first commit to remove the file s in it.
This should give you a pristine master branch, and leave all the others untouched, but since you are rewriting history, all repository that cloned yours will need to be fixed.
how about checkout to the master branch of your local repository and delete everything. then push this branch to the remote branch(origin)
do u remove empty the content of the master branch or the branch itself?