Git Orphan Branch Example
In this post, we present a Git Orphan Branch Example.
1. Introduction
In general, when you create a new branch off branch “master” you inherit its commit history. The exception is an orphan (or disconnected) branch. An orphan branch does not have a parent-child relationship to the master branches’ commits.
Most commits have one parent commit, one obvious exception being root commits which have no parent commits. Creating an orphan branch will retain the working tree of the branch it’s based on, but without an ancestor commit.
You may want to create an orphan branch to keep the project documentation separate from the code. Or you may want to create an orphan branch if you plan to merge two unrelated repositories with different histories.
Another use case for an creating an orphan branch is to create a static site for project or organizational information directly from a GitHub repository.
In any case, orphan branches are short-term solutions for unconventional situations and are typically discarded after serving their purpose. In this example, we will demonstrate how to create an orphan branch in Git.
1.1 Tools Used in this Example
- Git 2.17
Git downloads are available here: https://git-scm.com/downloads.
Note: This example was created on the macOS platform. Git for Windows includes Git Bash and Git CMD shells to run command-line operations.
2. Git Orphan Branch Example
Let’s start by changing into the top directory of your local repository and running the Git status
command.
$ cd ebookstore GilbertopezsMBP:ebookstore gilbertlopez$ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
Note: For the purposes of this example, commit or unstage any staged files before you continue.
Change to the master branch if you are in a different branch.
$ git checkout master Switched to branch 'master'
Run the log
command to view the commit history of the master branch
$ git log --oneline fcafda0 Added Spring Security dependencies to pom.xml 945ceee Merge branch 'master' of https://bitbucket.org/gilbert_lopez/ebookstore.git 9492190 Spring Security SQL script for USERS and Authorities tables. c464a25 README updated with Spring Security Database Schema location information. 5b61da8 README.md edited online with Bitbucket 53571f6 Added Spring Security using JDBC authentication. 61d10f2 Added form field validation for Book domain model. c8659b8 Moved image file saving functionality to the service layer. a0fcb77 Added edit/update function 215bb63 Added upload file functionality. c9e85b0 Check in remaining files. a791f39 Merge branch 'master' of https://gilbert_lopez@bitbucket.org/gilbert_lopez/ebookstore.git 92b22b1 README.md created online with Bitbucket 302bb61 Initial e-Commerce project check-in
The oneline
option directs the log command to display the commit’s abbreviated hash and message only.
Run the Git ls-files
command to the view files in the master branch.
$ git ls-files README.md ebookstore/pom.xml ebookstore/src/main/java/org/lopez/ebookstore/config/AppConfig.java ebookstore/src/main/java/org/lopez/ebookstore/config/AppInitializer.java ebookstore/src/main/java/org/lopez/ebookstore/config/HibernateConfig.java ebookstore/src/main/java/org/lopez/ebookstore/config/SecurityConfig.java ebookstore/src/main/java/org/lopez/ebookstore/config/SecurityInitializer.java ebookstore/src/main/java/org/lopez/ebookstore/controller/AdminController.java ebookstore/src/main/java/org/lopez/ebookstore/controller/AppController.java ebookstore/src/main/java/org/lopez/ebookstore/controller/LoginController.java ebookstore/src/main/java/org/lopez/ebookstore/dao/AbstractDao.java ebookstore/src/main/java/org/lopez/ebookstore/dao/BookDao.java ebookstore/src/main/java/org/lopez/ebookstore/dao/BookDaoImpl.java ebookstore/src/main/java/org/lopez/ebookstore/model/Book.java ebookstore/src/main/java/org/lopez/ebookstore/service/BookService.java ebookstore/src/main/java/org/lopez/ebookstore/service/BookServiceImpl.java ebookstore/src/main/java/org/lopez/ebookstore/service/FileUtil.java ebookstore/src/main/resources/application.properties ebookstore/src/main/resources/book.sql ebookstore/src/main/resources/messages.properties ebookstore/src/main/resources/spring_security.sql ebookstore/src/main/webapp/WEB-INF/resources/css/app.css ebookstore/src/main/webapp/WEB-INF/resources/css/bootstrap-theme.min.css ebookstore/src/main/webapp/WEB-INF/resources/css/bootstrap.min.css ebookstore/src/main/webapp/WEB-INF/resources/css/justified-nav.css ebookstore/src/main/webapp/WEB-INF/resources/css/main.css ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.eot ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.svg ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.ttf ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.woff ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.woff2 ebookstore/src/main/webapp/WEB-INF/resources/js/bootstrap.min.js ebookstore/src/main/webapp/WEB-INF/views/accessDenied.jsp ebookstore/src/main/webapp/WEB-INF/views/addBook.jsp ebookstore/src/main/webapp/WEB-INF/views/admin.jsp ebookstore/src/main/webapp/WEB-INF/views/bookDetail.jsp ebookstore/src/main/webapp/WEB-INF/views/bookInventory.jsp ebookstore/src/main/webapp/WEB-INF/views/books.jsp ebookstore/src/main/webapp/WEB-INF/views/editBook.jsp ebookstore/src/main/webapp/WEB-INF/views/home.jsp ebookstore/src/main/webapp/WEB-INF/views/login.jsp ebookstore/src/main/webapp/WEB-INF/views/templates/footer.jsp ebookstore/src/main/webapp/WEB-INF/views/templates/header.jsp
To create an orphan branch, run the checkout
command with the ‘orphan’ flag and give the branch a name.
$ git checkout --orphan myorphanbranch Switched to a new branch 'myorphanbranch'
Now if you run the log
command, you will get the following output.
$ git log --oneline fatal: your current branch 'myorphanbranch' does not have any commits yet
This is because orphan branches do not inherit commits from the parent branch. Now run the status
command.
$ git status On branch myorphanbranch Initial commit Changes to be committed: (use "git rm --cached ..." to unstage) new file: README.md new file: ebookstore/pom.xml new file: ebookstore/src/main/java/org/lopez/ebookstore/config/AppConfig.java new file: ebookstore/src/main/java/org/lopez/ebookstore/config/AppInitializer.java new file: ebookstore/src/main/java/org/lopez/ebookstore/config/HibernateConfig.java new file: ebookstore/src/main/java/org/lopez/ebookstore/config/SecurityConfig.java new file: ebookstore/src/main/java/org/lopez/ebookstore/config/SecurityInitializer.java new file: ebookstore/src/main/java/org/lopez/ebookstore/controller/AdminController.java new file: ebookstore/src/main/java/org/lopez/ebookstore/controller/AppController.java new file: ebookstore/src/main/java/org/lopez/ebookstore/controller/LoginController.java new file: ebookstore/src/main/java/org/lopez/ebookstore/dao/AbstractDao.java new file: ebookstore/src/main/java/org/lopez/ebookstore/dao/BookDao.java new file: ebookstore/src/main/java/org/lopez/ebookstore/dao/BookDaoImpl.java new file: ebookstore/src/main/java/org/lopez/ebookstore/model/Book.java new file: ebookstore/src/main/java/org/lopez/ebookstore/service/BookService.java new file: ebookstore/src/main/java/org/lopez/ebookstore/service/BookServiceImpl.java new file: ebookstore/src/main/java/org/lopez/ebookstore/service/FileUtil.java new file: ebookstore/src/main/resources/application.properties new file: ebookstore/src/main/resources/book.sql new file: ebookstore/src/main/resources/messages.properties new file: ebookstore/src/main/resources/spring_security.sql new file: ebookstore/src/main/webapp/WEB-INF/resources/css/app.css new file: ebookstore/src/main/webapp/WEB-INF/resources/css/bootstrap-theme.min.css new file: ebookstore/src/main/webapp/WEB-INF/resources/css/bootstrap.min.css new file: ebookstore/src/main/webapp/WEB-INF/resources/css/justified-nav.css new file: ebookstore/src/main/webapp/WEB-INF/resources/css/main.css new file: ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.eot new file: ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.svg new file: ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.ttf new file: ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.woff new file: ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.woff2 new file: ebookstore/src/main/webapp/WEB-INF/resources/js/bootstrap.min.js new file: ebookstore/src/main/webapp/WEB-INF/views/accessDenied.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/addBook.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/admin.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/bookDetail.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/bookInventory.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/books.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/editBook.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/home.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/login.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/templates/footer.jsp new file: ebookstore/src/main/webapp/WEB-INF/views/templates/header.jsp
You will notice that although all the files were copied to the orphan branch, they are in the staged area. That is because no commits have been performed thus far in the orphan branch.
We can remove all files in the working tree with the git rm -rf .
command.
$ git rm -rf . rm 'README.md' rm 'ebookstore/pom.xml' rm 'ebookstore/src/main/java/org/lopez/ebookstore/config/AppConfig.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/config/AppInitializer.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/config/HibernateConfig.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/config/SecurityConfig.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/config/SecurityInitializer.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/controller/AdminController.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/controller/AppController.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/controller/LoginController.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/dao/AbstractDao.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/dao/BookDao.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/dao/BookDaoImpl.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/model/Book.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/service/BookService.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/service/BookServiceImpl.java' rm 'ebookstore/src/main/java/org/lopez/ebookstore/service/FileUtil.java' rm 'ebookstore/src/main/resources/application.properties' rm 'ebookstore/src/main/resources/book.sql' rm 'ebookstore/src/main/resources/messages.properties' rm 'ebookstore/src/main/resources/spring_security.sql' rm 'ebookstore/src/main/webapp/WEB-INF/resources/css/app.css' rm 'ebookstore/src/main/webapp/WEB-INF/resources/css/bootstrap-theme.min.css' rm 'ebookstore/src/main/webapp/WEB-INF/resources/css/bootstrap.min.css' rm 'ebookstore/src/main/webapp/WEB-INF/resources/css/justified-nav.css' rm 'ebookstore/src/main/webapp/WEB-INF/resources/css/main.css' rm 'ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.eot' rm 'ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.svg' rm 'ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.ttf' rm 'ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.woff' rm 'ebookstore/src/main/webapp/WEB-INF/resources/fonts/glyphicons-halflings-regular.woff2' rm 'ebookstore/src/main/webapp/WEB-INF/resources/js/bootstrap.min.js' rm 'ebookstore/src/main/webapp/WEB-INF/views/accessDenied.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/addBook.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/admin.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/bookDetail.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/bookInventory.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/books.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/editBook.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/home.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/login.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/templates/footer.jsp' rm 'ebookstore/src/main/webapp/WEB-INF/views/templates/header.jsp'
Now we have a branch with no commits and no files. Run the Git ls-files
command to the view files in the orphan branch. You’ll notice there are no files in the branch.
Let’s create and add a README file.
$ echo "#My Readme file" > README.md $ git add README.md
Next, we’ll commit the file.
$ git commit -a -m "Initial Commit" [myorphanbranch (root-commit) 92e1465] Initial Commit 1 file changed, 1 insertion(+) create mode 100644 README.md
Finally, run the log command.
$ git log --oneline 92e1465 Initial Commit
We now have an orphan branch with its own commit history and working tree disparate from the master branch in the repository.
3. Git Orphan Branch – Summary
In this post, we demonstrated how to create an orphan branch in Git. Although orphan branches are not commonly used, they are sometimes handy for writing documentation and creating static GitHub pages. They are also sometimes used to merge two unrelated repositories with different histories.