Robert L Carpenter
Robert L Carpenter

Reputation:

Using git to merge specific files from another branch commit

I have a git repository that I am using and I've screwed up. I made a few patches to the source tree and then went on to do upgrades without committing the patches first. Then I committed the whole batch together as one. My intent was to be doing the upgrades on a separate branch, which I did create before the commit.

Now at this point I have two (relevant) branches master, which is still stable but needs patched. And I have new_auth_system which has two commits ahead of master. The first is full of patches I did yesterday and committed. The second has a few more patches and an incredibly large and obscure amount of deletions and additions across several files.

I've been trying to use git checkout to pull the specific files out of the commit and tack them onto a clone of master in an attempt to get things corrected but I keep getting "fatal: reference is not a tree"

How do I go about pulling specific files across commits and into another branch?

Thanks!

Upvotes: 1

Views: 3922

Answers (2)

VonC
VonC

Reputation: 1323165

If your situation is:

 x--x--x--x (master, need patch)
        \
         y1--y2  new_auth_system, with y1 containing some patches *and* updates
                                  and y2 containing massive refactoring

I would recommand first:
(if you haven't pushed new_auth_system already)

  • a rebase --interactive to split y1 into y1a (with only your patches) and y1b (updates), and y2 in y2a (more patches) and y2b (massive refactoring)
    git checkout new_auth_system
    git rebase -i y1^

     x--x--x--x (master, need patch)
            \
             y1a--y1b--y2a--y2b  new_auth_system, with y1a containing only patches
                                                       y1b containing only updates
                                                       y2a containing only patches
                                                       y2a containing refactoring
  • a rebase --interactive to reorder 'y' commits (patches first, updates and refactoring second)
    git checkout new_auth_system
    git rebase -i y1a^

     x--x--x--x (master, need patch)
            \
             y1a--y2a--y1b--y2b  new_auth_system, with patches first (y1a, y2a)
                                                       updates and refactoring second
  • a git merge to integrate those patches in master:
     x--x--x--x------x' (master, with patches)
            \       /
             y1a--y2a--y1b--y2b  new_auth_system, with patches first, 
                                                       updates and refactoring second

It is better than trying to isolate some files from one branch to another: Git reasons in term of project (a collection of files under a common tree), not in term of individual files.

Upvotes: 2

labratmatt
labratmatt

Reputation: 1841

I'm a little confused by your question, so please let me know if my response isn't appropriate.

From what I understand, you want to roll back specific files to a specific commit. To do that:

Use git log to grab the version (hash) you're looking for:

git log -p filename

Once you have the version (hash), use git checkout to revert that file:

git revert --no-commit version

Upvotes: 0

Related Questions