1

Following normal gitflow, I have a master branch from which I have forked a feature branch for development purposes. In the feature branch, I have done tons of changes (added new files) and now need to merge back to master. Please note that these changes in the feature branch has happened over several weeks containing hundreds of commits.

However, due to requirements, I am allowed to take a sub-set of files, which have been edited/added across multiple commits.

Question is - how do I raise a pull request from feature to master containing the above sub-set of files only and ignore any other files that may have been added/edited?

Please note that its not possible to do a GIT cherry pick of commits, because

  1. There are hundreds of commit in the feature branch
  2. Any of these commits can contain the files to be merged back alongwith files that don't have to be merged back.

Thanks

sppc42
  • 2,994
  • 2
  • 31
  • 49

2 Answers2

4

You want to create a new feature2 from master and include only your sub-set of files in it, create PR from it, have it tested and reviewed.

To create such branch from certain files (not commits) in existing branch:

git checkout -b feature2 master 
# optionally git rm any checked in files in master that shouldn't be there (were removed in feature)
git checkout feature -- <path> [ <path> ... ]
git commit

where <path>s are paths to subset of files in original feature branch you want to include.

Other option could be to cherry-pick but for that you'd need to identify commits where those files were added to feature branch and probably do partial cherry-picks.

blami
  • 6,588
  • 2
  • 23
  • 31
  • When I try this `git checkout feature -- `with the path of a file which is in the `feature` branch but not in the `feature2` branch, I get : `error: pathspec '' did not match any file(s) known to git` – altius_rup Jul 04 '23 at 15:33
3

There is no such thing. Git works on a commit-by-commit basis. If the commits you have are not the ones you want, you must make new, different commits.

Generally, the way you might do this is to create a new branch whose current commit is the same starting point as the set of commits that you don't like (e.g., also starting at the tip of master) and then explicitly extract specific files from specific commits, as in blami's answer. Note that you do not need git commit -a here. I personally consider using git commit -a a bad habit because it leads you into trouble when you create new files.

(Note, by the way, that every commit contains every file, or more precisely, every file Git knew about at the time you, or whoever, made the commit. These are stored in a special, read-only, Git-only, compressed and de-duplicated format, so the fact that the last commit in feature might have the same copies of the same files as hundreds of earlier commits in feature means that the de-duplication has saved lots of space.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks for call on `git commit -a` bas habit. I updated my answer and removed it from my example. – blami Sep 22 '20 at 10:51
  • 1
    @blami: not really a problem - it's a bit surprising, at first, that this kind of `git checkout` writes the files into both Git's index and one's working tree, and the new (since Git 2.23) `git restore` has options to pick which of those two (or both) to write, so if you only write to your working tree, you'd need to run `git add -u` or equivalent, which the `-a` shortcut saves. The thing that catches people here is that `git add -u` *doesn't* add totally-new files, and hence neither does `git commit -a`... – torek Sep 22 '20 at 10:53