How to create a commit without a parent in Git?

Possible duplicate:
In git, is there an easy way to inject an unrelated branch into the repository?

I have a small project that I worked on without any version control software, and I ended up with three different versions of the same script, where each of them does something a little different from the others. I initialized the git repository with one of the versions, and I really would like to commit each of the other two without parents. In this way, I can combine the differences as if each of the three versions of the script were the tips of different branches.

I'm not even sure if this is necessarily a reasonable or sensible way to solve this problem. Any advice on how to do this, or advice on the best way to handle this situation, will be appreciated.

+64
git
Apr 16 '11 at 10:11
source share
3 answers

From my comment:

Why don't you create two branches and add two other versions to them, and then work with the branches?

I think I agree with ctcherry's answer.




But if you really want to create a completely different โ€œbranchโ€ that is not related to the wizard, you can do this with git checkout --orphan . The manual for git checkout describes how the --orphan option --orphan :

Create a new orphan branch named <new_branch>, starting with <start_point> and switch to it. The first commit made in this new branch will not have parents, and it will become the root of the new story, completely disconnected from all other branches and commit.

You can use it as follows:

 git checkout --orphan version2 git rm -rf . <add version2 script> git add your files git commit -m 'Initial commit for version 2' 

If your Git is older than 1.7.2 (when git checkout --orphan was introduced), you can use the alternating sequence of commands from "Creating a New Empty Branches" in the Git Community Book :

Sometimes you may want to keep branches in your repository that do not have a common ancestor with your normal code. Some examples of this might be generated documentation or something like that. If you want to create a new branch branch that does not use your current code base as a parent, you can create an empty branch like this:

 git symbolic-ref HEAD refs/heads/newbranch rm .git/index git clean -fdx <do work> git add your files git commit -m 'Initial commit' 
+120
Apr 16 2018-11-21T00:
source share

You can create a branch for each version of the script and pass them to these separate branches. Then combine them one at a time back into the master. This should have the effect you are looking for.

+2
Apr 16 '11 at 10:16
source share

Why not start with an โ€œemptyโ€ repo (for example, just reading your problem statement above). You can then branch out three times and add different versions of the script to each of the branches and pass them separately.

Then you can fork / modify / commit / merge and try all options in well-controlled mode.

+2
Apr 25 2018-11-21T00:
source share



All Articles