Reputation: 1563
I have some commits of a branch and want create to another branch but for only certain files.
Let's
git checkout mybranch
switching from master to mybranch and there already exist some commits which apply changes to different files.
Now I want to create a branch where I see similar commits but this time only with changes for files I selected. How could I do that?
My idea would be taking every commit and create a second commit for each one, where the selected files changes appear. But that can lead to a lot of manual work depending how many commits you have. Also I doubt those can be easily merged later on.
What is the best way to merge only changes of selected files? Is it even possible?
Upvotes: 2
Views: 246
Reputation: 52236
You can use git filter-branch
to rewrite a part of your branch.
In your case, you could use --index-filter
:
# create a copy of your branch :
git branch extracted mybranch
git checkout extracted
# all flavors of git filter-branch basically take a script to apply on each commit
git filter-branch --index-filter 'git reset HEAD && git add [the files you want]'
master..extracted
You can also look into a tool like git-filter-repo
Upvotes: 1
Reputation: 1330102
What is the best way to merge only changes of selected files? Is it even possible?
In your second branch, all you have to do is create changes (and commit said changes) only for those files.
Even though your second branch will have all files, when you merge it back to master, only the files you have modified and committed will be merged.
Upvotes: 1