How to do it...

  1. We now have a branch with no parent. You can verify it by examining the commit log as follows:
$ git log
fatal: your current branch 'fresh-start' does not have any commits yet

Fresh start does not mean that you are starting from scratch. The files and directories that have been added to the repository still exist:

$ ls
README.md a_sub_directory another-file.txt cat-me.txt hello_world.c
$ git status
On branch fresh-start

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: README.md
new file: a_sub_directory/readme
new file: another-file.txt
new file: cat-me.txt
new file: hello_world.c
  1. If you need a fresh start, you can delete the files (but remember not to delete .git) as follows:
$ git rm --cached README.md a_sub_directory/readme another-file.txt cat-me.txt hello_world.c
$ rm -rf README.md a_sub_directory another-file.txt cat-me.txt hello_world.c
$ git status
On branch fresh-start

No commits yet

nothing to commit (create/copy files and use "git add" to track)
  1. You have a branch with no files and no commits. Moreover, the branch does not share any commit history with your master branch. You could add another repository and fetch all its commits using git remote add and git fetch. Instead, we will simply add a text file to illustrate it as follows:
$ echo "This is from an orphan branch." > orphan.txt
$ git add orphan.txt
$ git commit -m "Orphan"

Commit is the only thing in the history that you can verify with the command git log. If you fetch another repository into the branch, you will see all the commits and, more importantly you will have a copy of the repository's history. 

  1. Once you have your commits in place on the orphan branch, it is time to merge them into your master branch. However, your first attempt will fail. For example, check the following:
$ git checkout master
$ git merge fresh-start
fatal: refusing to merge unrelated histories
  1. As you can see, the orphan branch does not share history with the master branch, and git will not allow you to merge the branch. It shouldn't come as a surprise, since it is basically what an orphan branch is all about. However, you can still merge an orphan branch by allowing unrelated histories to be merged:
$ git merge fresh-start --allow-unrelated-histories
$ git log -3
commit aa804347c728552f7ce9298a83ab646148078dab (HEAD -> master)
Merge: 13dcada 45d1798
Author: John Doe <[email protected]>
Date: Fri May 11 08:57:45 2018 +0200

Merge branch 'fresh-start'

commit 45d179838f8f9f8fd64c6c7bf96147e09ceadbc2 (fresh-start)
Author: John Doe <[email protected]>
Date: Fri May 11 08:57:22 2018 +0200

Orphan

commit 13dcada077e446d3a05ea9cdbc8ecc261a94e42d (origin/master, origin/HEAD)
Author: John Doe <[email protected]>
Date: Fri Dec 13 12:26:00 2013 +0100

This is the subject line of the commit message

... and more output

It is unlikely that you will use orphan branches on a daily basis, but it is a strong feature to know when you need to reorganize your code base.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.56.45