Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a commit without a parent in Git? [duplicate]

Tags:

git

I have a small project I was working on without any kind of version control software, and I ended up with three different versions of the same script, where each one does something slightly different from the others. I initialized a git repo with one of the versions, and I'd really like to commit each of the other two without parents. That way, I can merge the differences as though each of the three versions of the script were the tips of different branches.

I'm not even sure whether this is necessarily a sane or reasonable way to go about solving this problem... Any advice on how to do things this way, or advice on a better way to handle this situation would be much appreciated.

like image 226
user648855 Avatar asked Apr 16 '11 22:04

user648855


People also ask

What is the parent of a commit?

The parent commit is the commit this current commit is based on. Usually: When you git commit normally, the current commit becomes the parent commit of the new commit that's introduced by the command.

Can a git commit have multiple parents?

A git commit can have an arbitrary number of parents. This can be the case, for example, when you use git merge to merge more than one commit into your current branch. Hope you found this post useful.

Can you make commits without pushing?

So commiting changes without pushing allow the save-load behaviour done locally during development. Once you are happy with your work, you then commit AND push.

Can two people commit to same branch?

Multiple people can work on the same branch at the same time. When you pull (or have the other person push) their changes to you git will merge the changes together resulting in a branch with both of your changes.


2 Answers

From my comment:

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

Which I think would agree with ctcherry's answer as well.


But if you really do want to create a completely different "branch" that is unrelated to master, then you can do it with git checkout --orphan. The manual for git checkout describes to work of the option --orphan:

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.

You might use it like this:

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), then you can use the alternate command sequence from “Creating New Empty Branches” in the Git Community Book:

Ocasionally, you may want to keep branches in your repository that do not share an ancestor with your normal code. Some examples of this might be generated documentation or something along those lines. If you want to create a new branch head that does not use your current codebase 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' 
like image 122
manojlds Avatar answered Oct 07 '22 14:10

manojlds


Why not start with an 'Empty' repo (e.g. a single readme of your problem statement above). Then you can branch the three times and add the different script versions into each of the branches, and commit them separately.

You can then branch/edit/commit/merge and try out all you options in a well managed way.

like image 45
Philip Oakley Avatar answered Oct 07 '22 14:10

Philip Oakley