Skip to content

Instantly share code, notes, and snippets.

@ozh
Last active April 8, 2025 20:49
Show Gist options
  • Save ozh/4734410 to your computer and use it in GitHub Desktop.
Save ozh/4734410 to your computer and use it in GitHub Desktop.
Create a new empty branch in Git
$ git checkout --orphan NEWBRANCH
$ git rm -rf .

--orphan creates a new branch, but it starts without any commit. After running the above command you are on a new branch "NEWBRANCH", and the first commit you create from this state will start a new history without any ancestry.

You can then start adding files and commit them and they will live in their own branch. If you take a look at the log, you will see that it is isolated from the original log.

@manukurubalaji
Copy link

how to create new empty branch in repository plz update command

@airtonix
Copy link

airtonix commented Jun 24, 2022

deploy_empty_branch(){
  commit_id=$(git commit-tree -m "New empty $1 branch" 4b825dc642cb6eb9a060e54bf8d69288fbee4904)
  git push origin ${commit_id}:refs/heads/$1
}

deploy_empty_branch gh-pages

https://www.edwardthomson.com/blog/pushing_an_empty_branch.html

@hman278
Copy link

hman278 commented Aug 1, 2022

instead of creating a new empty branch this wiped all my project files and weeks of work are now gone :)

@Staubgeborener
Copy link

instead of creating a new empty branch this wiped all my project files and weeks of work are now gone :)

Yeah, because you nuked everything with rm -rf without doing a git checkout before.

@DeanAyalon
Copy link

instead of creating a new empty branch this wiped all my project files and weeks of work are now gone :)

Don't experiment with obscure git commands on repositories you do not have backed up, a single remote or even a local copy would've saved you the headache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment