63

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?

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • 1
    I understand that you want the files in other branches to be untouched. Do you want to merely remove the files in the `master` branch or just remove the branch `master` completely ? – Tuxdude Mar 16 '13 at 20:51
  • 2
    I want to remove all files in master but keep it. As if starting from scratch. No files, no history. But yes, all other branches remain untouched. – Marty Wallace Mar 16 '13 at 20:51

6 Answers6

73

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.

aragaer
  • 17,238
  • 6
  • 47
  • 49
  • 1
    Make sure there is at least a tag pointing to the current master, before removing the master branch, otherwise the OP can potentially lose commits. – Tuxdude Mar 16 '13 at 21:09
  • 1
    Also note that `git checkout --orphan` is present only with git `1.7.2` and above. – Tuxdude Mar 16 '13 at 21:15
  • If you create totally empty master (second set of commands) then execute `git commit --allow-empty -m "empty repo" ` at the end. – Ilya Serbis Aug 30 '18 at 08:31
  • but what if that branch is linked to aws deployed app...will it wipe the aws configuration that was linked to the deleted branch?? – vInEnDeRsInGh Jul 14 '21 at 10:36
48

Create an Orphan Branch

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • 1
    This is the best way to do it. But note that the `--orphan` option was only added in git 1.7.2. – Anubian Noob Jan 19 '15 at 20:53
  • 2
    This is actually the best answer in my opinion. The answer suggesting to reset the master to the initial commit isn't exactly what is needed, as the initial commit isn't necessarily in a clean state. – bestform Jul 22 '15 at 10:20
  • This worked great for me. Since I already had my orphan, after moving master, I checked out my orphan and then created master from there: `git checkout my_orphan` `git checkout -b master` – stephen.hanson Dec 08 '16 at 23:44
11

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:

Fix using low-level plumbing commands

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

It's further described in my blogpost.

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
4

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.

Spencer MacBeth
  • 147
  • 1
  • 4
  • 1
    Recipe works quite alright but there's couple of gotchas. First: `git rm -rf *` does not remove hidden files like `.gitignore`. Second: you have to commit something into `master` after `rm -rf`. Otherwise new `master` will disappear after you checkout `development-branch-name`. – Alik Feb 04 '19 at 18:06
3

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.

Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
1

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?

Shibbir Ahmed
  • 1,350
  • 13
  • 19
  • This makes sense. But it keeps the history wouldnt it? I want rid of all history too – Marty Wallace Mar 16 '13 at 20:53
  • I'm not sure. may b this link can help you: [link1](http://stackoverflow.com/questions/4515580/how-do-i-remove-the-old-history-from-a-git-repository) [link2](http://stackoverflow.com/questions/13091928/git-remove-history-commit) – Shibbir Ahmed Mar 16 '13 at 20:56