69

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?

Javis
  • 843
  • 1
  • 9
  • 16
  • possible duplicate of [How can I completely clear just one branch in a git repo?](http://stackoverflow.com/questions/11313514/how-can-i-completely-clear-just-one-branch-in-a-git-repo) – KL-7 Jul 14 '12 at 22:25
  • 2
    These questions have some similarities, but they are not exact duplicates. – Todd A. Jacobs Jul 15 '12 at 18:06

3 Answers3

139

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Thank you @CodeGnome,it looks pretty interesting, I just want to add one more question: lets say I create this new _foo_ branch, add some folder from master and do my changes. If in the future I do a change on master, can I merge it with _foo_? – Javis Jul 14 '12 at 23:39
  • You can treat an orphaned branch like any other branch when it comes to merging, cherry-picking, or checking out individual files from another branch into *foo* (e.g. `git checkout master -- quux`). The only real difference is that the orphaned branch won't have an initial commit until you make one, and git will need to perform merges without reference to ancestry. – Todd A. Jacobs Jul 15 '12 at 00:23
  • then delete `rm -rf * .*` – wieczorek1990 Nov 13 '14 at 21:23
  • 6
    @wieczorek1990 That will delete the `.git` folder.. Use `git clean -fd ` instead. – User 12345678 Nov 24 '14 at 18:10
  • +1 for `git rm --cached $(git ls-files)` . very important when you want to set up a documentation branch. You don't want all the changes of the ancestor. Plus it's noteworthy that you can only see the orphan branch after you added and committed ! – Matt Bannert Oct 26 '16 at 09:48
-4

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
Hans Ginzel
  • 8,192
  • 3
  • 24
  • 22
-5

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.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
  • yeah but my worry is how to merge the future changes on the files that I do on master with the same files on my new branch when I already added them. – Javis Jul 14 '12 at 23:42