Reputation:
I have branch1 with 10 files, while creating new branch as branch2, I want to select only four files into the new branch.
Is it possible in git, because while creating a new branch?
Upvotes: 13
Views: 26301
Reputation: 488203
You might take a look at What exactly do we mean by "branch"? Branch names are merely pointers, pointing to a specific commit. Each commit contains a snapshot, of whatever is in the index at the time you make the snapshot. (Each commit also "points back" to its parent commit(s) as well, and of course has an author, committer, and commit message.)
Making a new commit first writes out the index, then writes a commit referring to the saved index (a tree object) and also referring back to the current, or HEAD
, commit. Last, the new commit becomes the current / HEAD
commit, by writing its ID into the branch name. The branch name then points to the newest commit (the HEAD
file contains only the branch name; it's the branch-name that points to the commit).
If you want a new commit to contain some specific set of files, you should add those files to the index and remove any other files from the index. Whatever is in the index will become the snapshot contents of the next commit you make.
To see what is in the index right now, run git ls-files
. For a more verbose / detailed output, use git ls-files --stage
, which shows which index slots (there can be more than one, during a conflicted merge; but otherwise everything is in slot zero) are occupied as well.
As in Bertrand Martel's answer, you can copy files into the index using git checkout
:
git checkout <commit-specifier> -- <path> [ <path> ... ]
copies the file for each <path>
from the specified commit—a branch name points to the tip commit on that branch, but you can use anything from gitrevisions
here—into the index, and then from the index into the work-tree.
You can also copy a file into the index by taking it from the work-tree:
git add <path> [ <path> ... ]
This copies whatever is in the work-tree at the given path into the index (specifically into slot zero, wiping out any other slots, which is why this resolves a merge conflict).
To remove a file from the index, use git rm
, similarly to git add
. Note, though, that this removes it from both the index and the work-tree; to remove from the index only, use git rm --cached
.
For complex scripting operations, you can use git update-index
to make changes directly to the index, but this requires a lot more familiarity with the Git internals (you have to individually hash blob objects first, for instance, and know when to use --add
, --replace
, and so on).
Upvotes: 11
Reputation: 45382
Create your new branch, remove all files in that branch, retrieve some files from another branch and commit changes :
# create a new branch
git checkout -b branch_name
# remove all files for this branch
git rm -rf .
# retrieve some files from master branch
git checkout master -- file1 file2 file3 file4
# commit changes
git commit -m "create new branch"
Upvotes: 26