5

I have a branch with hundreds of new and different files and I just want to merge a dozen or so of these into my release branch. I tried running a normal git merge with no commit and that automatically staged hundreds of files I don't want (in addition to finding dozens of conflicts that require manual merging.) I then tried to do a git revert to unstage all of the automerged files and just add the ones I want back to the index, but that also aborted the merge and leaves all the conflict markers in the files. I thought I could just run the mergetool but it now doesn't recognize the conflict markers...

Am I going about this the totally wrong way...or is there something I'm missing?

Ken Otwell
  • 345
  • 3
  • 13
  • 2
    Merge and revert talk about *commits*, staging applies to *the index*. What exactly are you referring to where? It’s not clear what you are doing. – poke Feb 03 '15 at 17:53
  • I tried to clarify a bit - but I already like Manzur's answer # 1 below. – Ken Otwell Feb 04 '15 at 16:20

3 Answers3

4

It's as simple as using git reset. I was using git stash pop and had a merge conflict and it also staged some files with changes by me and merge. I resolved the conflict and unstaged all the files with the following command:

git reset HEAD <file_1> <file_2> ... <file_n>
Chef Pharaoh
  • 2,387
  • 3
  • 27
  • 38
2

The question is a bit ambiguous. I'll try to rephrase it and answer accordingly. Say you want to merge only the files(file2, file3) from the branchA:

1) Get the tree sha1 of the branch commit:

git cat-file -p branchA

2) Checkout the changes under that tree for file2 and file3:

git reset <sha1> -- file2 file3

3) Commit this changes:

git commit -m 'Merge of branchA'

If you've already message with the staging, do(in case all the needed changes are committed ahead of time): git reset --hard

manzur
  • 682
  • 3
  • 7
  • Manzur- thanks, that's perfect. I'll still need to check for conflicts in these files but I can do that when merging back to the release branch. At least this simplifies the merge to just the files I care about. – Ken Otwell Feb 04 '15 at 16:18
  • Experimenting with this now... it seems git checkout works better than git reset in your step 2 manzur because then I don't have the original versions showing up as unstaged changes. Also I can just use the branch name instead of the . Make sense? – Ken Otwell Feb 04 '15 at 16:30
  • Sure, you can use sha1 instead of branch name. They're interchangeable for almost all cases(look at the .git/refs/heads/ it contains sha1). – manzur Feb 04 '15 at 20:10
  • regarding `checkout` vs `reset` it depends. `reset` is better when you know what happens on the index and filesystem. `checkout` is more simple, you can checkout a file to other directory and copy-paste the changes you want. – manzur Feb 04 '15 at 20:14
0

To revert an exact merge commit, and making all the changes as staged, below command can be used.

git revert -m 1 <commit-hash>
foobar
  • 2,887
  • 2
  • 30
  • 55