3

Esteemed Git users,

These post's appear to answer my question, but either i'm too new to Git to grasp & execute them correctly, or they don't in fact answer my question:

As the title indicates, i have a file, X, on branch1 and file X' on branch2. File X' was branched a few commits ago (to both file's X & X') from file X and i also changed the file name (i.e., from X to X'). The file's remain largely the same. What i'm trying to achieve is bring some of the changes made to file X over to X'. The changes are limited to two commits, commit_a and commit_b. Ideally (& optionally) i'd like to invoke my merge tool to manually accept (or not) specific lines of code within each commit_a and commit_b when applying them to X'. Is this possible and if so how? FWIW, I'm only using Git locally; no interaction with other collaborators/repositories. Trying to apply answers from the aforementioned posts, i could not focus only on the files of interest (branch1 X & branch2 X') - Git kept involving other files in the two branches, including files Y and Z (of both branches) as well as X in branch2, but never X' in branch2. I also want to keep branches1 & 2 separate so i don't see how the merge command would work here.

Being new to Git (and version control) i also question if my workflow is some how responsible for failing to achieve my objective here.

Patient exampled answers greatly appreciated!

Karl

Community
  • 1
  • 1
kbrand
  • 98
  • 1
  • 6
  • Why can't you just cherry-pick the commits that introduce the lines you want? It indicates a broken workflow if you cannot do that, namely that your commits are doing more than one thing at a time. You should either want all of the commit or none of it; if you only want part then it should have been more than just one commit the first time. – Daenyth Nov 05 '12 at 17:07
  • @Daenyth Indeed. With this i've leaned a little time spent thinking about commit granularity could save a lot more time manually adding specific lines of code from various commits. Thanks for highlighting. I'd be happy to just apply the two commits of interest but would still like to know if it's possible & how (hence '& optionally') to add specific lines of code. But note that i have completely failed even to just apply commit_a and _b as described in my case. So an explanation on just this would be great! – kbrand Nov 05 '12 at 17:19

1 Answers1

1

I would cherry-pick the commit that introduces the parts you want, then git reset HEAD^ to remove the most recent commit and leave the changes in the working copy, then git add -p the parts you want and make a new commit from that.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
  • this was looking promising- at least Git finally asked me about my file of interest - X'. But i was prompted to stage/not stage etc. the diffs between file X' on branch1 and file X' on branch2. I want to do this for file X on branch1 and file X' on branch2. Is there some way to explicitly state the file i want to 'update' (branch2 file X') and with what file and commits i want to take code or diffs from (branch1 file X, commit_a & _b)? – kbrand Nov 05 '12 at 17:54
  • @kbrand you can `git checkout -- path/to/file` to update your working copy with the contents of that file from that commit. – Daenyth Nov 05 '12 at 18:55
  • same result- i'm presented with diffs btwn file X' on branch1 and file X' on branch2. Let me be explicit to see if i'm executing/interpreting correctly: `git checkout branch2`; `git checkout 5a26b9d -- X'` -the commit & file i want to update, `git cherry-pick baf589d` -the commit on branch1 containing what i want to update with, `git reset HEAD^`; `git add -p` which then returns: `diff --git a/X' b/X'`; `index 25f8277..efbc689 100644`; `--- a/X'`; `+++ b/X'`. I don't want to stage hunks from file X' on branch1. I want to stage hunks from file X on banch1. Where am i going wrong? – kbrand Nov 06 '12 at 10:43
  • i succeeded, and although i can't be sure, i believe it was indeed the exact commands listed in my previous command. I.e., your answer. What confuses me is what's returned `diff --git a/X' b/X'`; `index 25f8277..efbc689 100644`; `--- a/X'`; `+++ b/X'`. There's no mention of X which is what i would have expected. So if i understand correctly- a/X' describes my file if i reject a hunk, and b/X' describes my file where i accept a hunk. Is this correct? Might anyone know where this is documented for, ahem, new users? Thanks a lot for your help :) – kbrand Nov 06 '12 at 16:42