Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Two initial commits

Tags:

git

branch

I have a project which recently began using version control.
There is an older attempt at this project with completely different code, which I want to add to version control for posterity but which is neither an ancestor nor a descendent of the initial commit of the current master branch.

I wish to create a new parallel branch for this with no previous history (i.e. to have multiple "initial commits". Is this possible with Git without having to create a second repository? The commit tree would look something like this:

o---o---o   master
     \
      o---o   branch

o   old implementation

Such that I can simply use git checkout old to access the code. It must not be cleaned up by the garbage collector after 90 days, and may optionally be tagged.

like image 414
Nicholas Shanks Avatar asked Sep 11 '13 14:09

Nicholas Shanks


2 Answers

git checkout has an option for exactly such use case: --orphan.

From the Git manual:

--orphan <new_branch>

Create a new orphan branch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

Also, the man says explicitly:

If you want to start a disconnected history that records a set of paths that is totally different from the one of <start_point>, then you should clear the index and the working tree right after creating the orphan branch by running "git rm -rf ." from the top level of the working tree.

like image 78
Marcin Łoś Avatar answered Oct 11 '22 01:10

Marcin Łoś


Apart from using the checkout --orphan method, you can as well create a separate repository in your other code base, make the initial commit, and then push to the other repository (or from the other repository pull from this one)

cd old-code
git init
git add .
git commit -m 'Initial commit of historical codebase'
git push ../path/to/current/code/repository master:historical

After that you can remove the repository again (rm -rf old-code/.git) if you don't need it (but it shouldn't hurt)

like image 27
knittl Avatar answered Oct 11 '22 01:10

knittl