Skip to content

Instantly share code, notes, and snippets.

@ozh
Last active March 3, 2024 13:42
Show Gist options
  • Star 82 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • 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.

@Ogollah
Copy link

Ogollah commented Sep 2, 2019

Hi, with this are we able to merge to master branch when done with the changes?

@Aysnine
Copy link

Aysnine commented Dec 19, 2019

Wow, Can I replace master with the NEWBRANCH ? And how :)

@uxFeranmi
Copy link

uxFeranmi commented Apr 19, 2020

You could first delete branch master.
git branch -D master.

Then create a new branch named "master" off of the NEWBRANCH.
git checkout NEWBRANCH
git checkout -b master

@NiKiZe
Copy link

NiKiZe commented Jul 8, 2020

You could first delete branch master.
.> git branch -D master.

Then create a new branch named "master" off of the NEWBRANCH.
git checkout NEWBRANCH
git checkout -b master

You could also reset master to NEWBRANCH by doing:
git checkout master
git reset --hard NEWBRANCH

which removes the need of first deleting it

@Ranguna
Copy link

Ranguna commented Aug 10, 2020

@Ogollah I know I'm late to the party but, yes you can merge master into the new banch. You can even fast forward the new branch to master's state (which will basically move the new branch's ref next to master's)

@mlambley
Copy link

Legend! Thank you.

I created a branch initial before creating master. I wanted to squash merge a single commit into master but couldn't create a PR and if I created a branch it would have just contained all of the commits I wanted to squash. I resolved this with:

git checkout --orphan master
git add .
git commit -m "Initial commit"
git push --set-upstream origin master

Then updated the Default Branch to master in Settings -> Branches.

@KhaledAhmedElmasry
Copy link

KhaledAhmedElmasry commented May 24, 2021

hello, but where do we write the code

@delano
Copy link

delano commented Aug 25, 2021

A complete example from a 2021 perspective starts with renaming master -> main. Then:

$ git checkout --orphan NEWBRANCH
$ git rm -rf .
$ touch .gitignore && git add .gitignore
$ git commit -m "Initialize with .gitignore"
$ git checkout main
$ git reset --hard NEWBRANCH

@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 :)

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