1

I have a project in git, and I want to branch a few select files (so the main git repo should have all the files in a certain directory, but the branch should only have a select 10).

CodeWizard
  • 92,491
  • 19
  • 110
  • 133
Charles Shiller
  • 873
  • 2
  • 9
  • 31

3 Answers3

1

You can do the following:

Start an orphan branch and checkout checkout only the desired files

# create orphan branch = branch without any history
git checkout --orphan <branch name>

# now checkout the desired files from the given branch/commit
git checkout <commit> path/to/file

--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.

The index and the working tree are adjusted as if you had previously run git checkout <start_point>.

This allows you to start a new history that records a set of paths similar to <start_point> by easily running git commit -a to make the root commit.

This can be useful when you want to publish the tree from a commit without exposing its full history. You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code.

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.

Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc.

Community
  • 1
  • 1
CodeWizard
  • 92,491
  • 19
  • 110
  • 133
0

You could create pre-commit hook, which makes it impossible to commit those files on any other than that very specific branch. Git hooks can be written in any languange that you fancy, such as python or bash. The latter makes it easy to combine the powers of other git commands as well as python.

jhoepken
  • 1,743
  • 2
  • 16
  • 22
0

You can cut the branch and then delete the files you don't want over there as the first commit on that branch. Would that work?

Noufal Ibrahim
  • 66,768
  • 11
  • 123
  • 160