3

I'm a newbie to git, and I do understand how git cherry-pick works, but here is my problem:

Recently, someone in my team, changed the directory structure in master, but not the directory structure in another branch.

Now, when I do make changes to my code in branch, I want to bring them (cherry-pick) into master. This was fine until the directory structures in both master and the branch were same.

topDir/some/subdir/file -- master
topDir/some/other/subdir/file -- branch

file, whose changes are to be brought into master is the same, but not the directory structure it is contained in. When I try to do a cherry-pick the usual way, I get an error along the lines of:

git checkout master git cherry-pick commit error: pathspec topDir/some/other/subdir/file does not exist

Now, what is the best way to go about cherry-picking in this scenario? Any pointers are highly appreciated.


OK. I just noticed that, in the same scenario, when I do a cherry-pick, git is smart enough to actually pick one of the files correctly from the commit, but does not recognize the other.

To use the same example I mentioned in my original post: In the same commit, topDir/some/subdir/file1, topDir/some/subdir1/file2.

I go to master, which has "file1" and "file2", just in a different dir structure: topDir/src/some/subdir/file1 topDir/src/some/subdir1/file2.

Now, if I do a cherry-pick, git is intelligent enough to pick up the changes in file1, even though this is in a different dir structure, but doesn't pick up the changes for file2. Any pointers? If anyone wants me to be more clear, I'll be happy to.

So, what I did to workaround the problem was do a cherry-pick, manually change the one git did not pick up, and "git commit -C .

5
  • Hmm, that's a rough one, is there a reason your directory structure changed? Is the new directory structure something you're going to want to keep in master, or is the master's directory structure ultimately what you want?
    – Will Buck
    Commented Mar 8, 2012 at 19:31
  • Thanks for the quick response Will. Not sure about the reason, and am not sure whether I'll get a better response if I were to ask for one. Yes, the one in master will be the permanent one. Commented Mar 8, 2012 at 19:32
  • Have you tried cherry picking the commit where the files' location was changed and then cherry picking the changes you want?
    – Carl
    Commented Mar 8, 2012 at 19:54
  • No carleeto, I have not tried that. Will do and let you know. Thanks for the response. Commented Mar 8, 2012 at 19:59
  • Meta-Proposal: Change the title of the question to "Git Cherry-Pick between different directory structures"
    – U. Windl
    Commented Sep 5, 2019 at 9:28

2 Answers 2

5

Happy to help :) if the one in master will be the permanent way, I would fix the file structure in the topic branch to match master, commit to the branch, *then merge the changes into master, not cherry-pick. As in

git checkout branch
mv other/subdir/file subdir/file
git status
// git add and git rm to get the commit proper
git commit -m "fixing file struct"
git checkout master
git merge branch

Really though, it depends on what the use cases of the files are, and what you want to be in the master branch, which is much more a your company specific question, not so much a technical problem anyone here can answer for you ;)

6
  • Thanks Will. I guess, I should try what carleeto suggested and if that does not work, what you've recommended here is the way to go. I shall mark this answered, but if others have any other useful suggestions, please do so, as this seems to be a scenario not many people have encountered. Commented Mar 8, 2012 at 20:01
  • Carleeto's suggestion is certainly a good option, but only if you want to move the file directory structure you have in the topic branch into your master branch. If you want the file to replace the one in master truly (which is what I think of when I see a desire to cherry-pick), I would recommend against doing that. Just out of curiosity, what kinds of files are these we're talking about? is this a code project? static document files?
    – Will Buck
    Commented Mar 8, 2012 at 20:04
  • Yes, this is a code project. The files in question are C++ files. Commented Mar 8, 2012 at 22:00
  • So for C++ it probably depends on what your makeFile or build tools expect for the directory structure, and whether you feel there would be value in having both implementations of this file available for action simultaneously. If the answer to that question is yes, then Carleeto's strategy would be optimal. If you do want this implementation to replace the other though, you'll want to merge it proper. Don't worry about losing the old implementation either way though, that's what git is for :)
    – Will Buck
    Commented Mar 8, 2012 at 22:03
  • No Problem Krishna! Don't forget to upvote answers as well as accepting them ;)
    – Will Buck
    Commented Mar 9, 2012 at 18:25
0

Suppose we have A,B,C,D,E & F nodes. The user is currently on F node and want to cherry pick with C node by using type git command like Git Cherry-Pick C then this command apply C node to F node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.