How can I start a clean branch with no ancestry, then commit files progressively?

GitBranch

Git Problem Overview


I have a PHP framework versioned with GIT and I'm planning several (drastic) changes to its core.

What I want to do is to start working on the new core in a new branch, but since this change might require some restructuring on the filesystem too, I'd like to start this new branch as cleanly as possible.

I want the clean branch to include only with the core files. As I'm finishing my changes, I want to add the rest of the modules from the framework to the working directory one by one, but keep the ability to merge if I do changes on master.

How can I do that?

Git Solutions


Solution 1 - Git

Branch with No Ancestors

You want the --orphan flag. For example:

git checkout master
git checkout --orphan foo

# Unstage all the files in your working tree.
git rm --cached $(git ls-files)

will create a new branch named foo with no ancestors, but it will preserve the current working tree from whatever branch you were on when you called the command (in this case, the master branch). You can then modify your working tree to suit, and then make a commit to start that branch's history afresh.

Incremental Staging of Files

To perform incremental additions to your history, use git add to stage just the files you want for each commit. The git-add(1) manual page has this to say about adding files selectively:

> Fileglobs (e.g. *.c) can be given to add all matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively.

Solution 2 - Git

If you must work with an old version of git:

 mkdir some_dir
 cd some_dir
 git init
 echo a>some_file; git add some_file; git commit -m 'initial commit'
 cd ..
 git fetch ./some_dir/ master:new_independent_branch
 rm -rf some_dir

Solution 3 - Git

What you can do is simply moving to a new branch, git co -b my_new_branch, clean up your code and keep the things you need, and finally commit. That commit, the first on my_new_branch, would then be a clean one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJavisView Question on Stackoverflow
Solution 1 - GitTodd A. JacobsView Answer on Stackoverflow
Solution 2 - GitHans GinzelView Answer on Stackoverflow
Solution 3 - GitSamy DindaneView Answer on Stackoverflow