r/git • u/zaphodikus • Oct 23 '26
Cherry-pick just 2 files
I've got 14 commits on a branch called Refactor_Python. It's 30 files, I have been asked by my boss to stage just 2 of the files for develop and also stage the same 2 files for another branch called release/v4.11. Now as I understand git I cannot stage changes for 2 branches at once without pushing them up to remote (yeah he likes going direct, none of this branchy thing which I prefer, not judging, it's just my confidence level is not there yet.) So this is a question about doing it with no branches. I already pushed my Refactor_Python branch, so I cannot rebase it without some level of pain, I'm happier to just delete my personal branch afterwards. If my boss had spoke earlier I would have just made 2 branches on with 2 files and one with 28 files, but I'm keen to learn:
So I suspect if I cherry-pick all commits that those 2 files are in, using git cherry-pick -n <commit> and then git add file1 file2 the result as a single commit, into develop, I would be able to push it and then just cherry-pick that single commit over again, but, into the release/v4.11 branch instead after I pushed it up to develop?
My second question is, if I than want to pick all the changes to the remaining 28 files and merge them to develop, that if I later cherry-pick all 14 commits with -n again, that git will just effectively let me merge the result into develop without too much arguing, or will it merge conflict me when I try to push because the changes to the first 2 files are already there?
18
u/JaguarMammoth6231 Oct 23 '26
To get just a few files from another branch, you can do
git checkout branch_name -- path/to/file1.py path/to/file2.py4
u/CartographerDue5382 Oct 23 '26
skip the cherry-pick -n plan. that puts every file from those commits into the index, so if you add 2 files and commit without clearing the rest, the other 28 ride along.
from develop:
that only touches those two paths. then check out release/v4.11 and cherry-pick that new commit. one branch at a time, two pushes. you can't land one commit on two branches in a single push.
for the other 28 later, checkout just those paths the same way. replaying the original 14 commits will conflict on the two files you already brought over, anywhere a commit touched them.
2
u/Broad-Promise6954 ancient Oct 24 '26
Both u/JaguarMammoth6231 and u/CartographerDue5382 have "the" right answer here (I put "the" in quotes because, what with Git being a set of tools, there are a lot of ways to get the same results, it's just that using
checkoutorrestorewith a specific commit and specific set of path names is easy and works great).It's worth taking a moment to realize why this works, and why cherry-pick is definitely the wrong way to do it. We should start with a couple of fundamental thing about Git.
(Branch names are special in a way that other names aren't, but there's no need to get into that here.)
The original
checkoutcommand primarily does one of two different things, which many years ago were finally split out into two separate commands,switchandrestore. You can use eithercheckoutor the separateswitch+restoreas you please. If you're new to Git I'd generally recommend using the separate ones, but that's up to you, depending on your personal tastes.The "thing we want done", in this case, is "extract the branch-tip version of two files from the commit whose hash ID is stored under the name
Refactor_Python". So:does exactly that (and
git checkout, given that same set of arguments, goes into "restore" mode and also does exactly that).When you use
git cherry-pick, by contrast, you give Git the OID (or a name for the OID) of a commit (or perhaps multiple commits but let's just use one at a time for our purposes here) and are telling Git: "Look at the commit I named. It's a full snapshot of every file. Look at its immediate parent commit as well, which is also a full snapshot of every file. Figure out what changed from the parent, which I only named implicitly, to the commit whose OID I just named. Then, try to make the same changes to the current working tree. If it's bleeding obvious how they fit in, just fit them in, if not, use your fullgit mergemode to work it out."Now, suppose the only changes you made to file1 and file2, in all 14 commits you made, were specifically in that very last commit, and you made no other changes in that last commit (with respect to its parent). In that case, cherry-pick would also do exactly what you want! But if you changed a third file, or if you made some changes in some earlier commit(s), cherry-pick would not do what you want.
As long as you keep in mind what each tool actually does, it will become a lot clearer (and faster) to use Git to get the result you want. It's a big tool set, and you don't have to learn every tool (many people may never touch
git rererefor instance, or a bit more obscure,git name-revorgit index-packorgit-unpack-objects, and so on), but you should certainly know commit, diff, fetch, log, merge, push, rebase, and status; and to know how rebase works (repeated cherry-picking) you'll need to knowgit cherry-pick. Hence the long-ish description above.1
u/dmazzoni Oct 23 '26
For your first question, there are many possible ways to do it, here are a few.
Either way, once you have a commit with just the changes to the two files, you can cherry-pick it to as many branches as you want, one at a time.
For your second question, it's quite likely git will give you merge conflicts when you try to push, but there are a couple of different solutions:
1
u/jjangg96 Oct 23 '26
one thing on your second question. if you copy the two files over with checkout like above and later merge Refactor_Python into develop (not cherry-pick the 14 commits), git does a three-way merge against the common ancestor. both sides ended up with the same content in those two files, and when both sides made the identical change git just takes it, no conflict.
the replay route is the one that conflicts, since each commit gets applied on top of a file that already has the final version. only caveat is if someone else touched those two files on develop in the meantime.
1
u/Medical-Captain1550 Oct 25 '26
skip cherry-pick and just run git checkout Refactor_Python -- file1 file2 on develop
1
u/WoodyTheWorker Oct 25 '26
git cherry-pick --no-commit <commit>
git commit -- file1 file2