Git unstage without losing merge?
Asked Answered
O

5

6

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?

Oversell answered 3/2, 2015 at 17:46 Comment(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.Lole
I tried to clarify a bit - but I already like Manzur's answer # 1 below.Oversell
T
5

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>
Twinkle answered 8/3, 2018 at 16:6 Comment(1)
Actually I found you don't need to specific the file(s), just do git reset HEAD.Twinkle
P
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

Puttee answered 3/2, 2015 at 19:32 Comment(4)
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.Oversell
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 <sha1>. Make sense?Oversell
Sure, you can use sha1 instead of branch name. They're interchangeable for almost all cases(look at the .git/refs/heads/<branchname> it contains sha1).Puttee
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.Puttee
F
0

Similar to Chef Pharaoh's answer, but shorter and more ergonomic:

git reset "*"

(The quotes are there to avoid shell expansion.)

Ferromanganese answered 15/3 at 15:31 Comment(0)
I
0

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.

Then make a commit with just those dozen or so files changed, and merge that.

git checkout -b frotzybitsoffeature $(git merge-base feature release)
git checkout feature -- thisfile thatfile theseothers/
git commit -m 'selecting the frotzy subset of feature'

git checkout release
git merge frotzybitsoffeature

git checkout feature            # now tell git the frozybits work is all still in feature
git merge frotzybitsoffeature
git branch -d $_
Illimani answered 15/3 at 16:0 Comment(0)
B
-1

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

git revert -m 1 <commit-hash>
Basidiospore answered 22/4, 2020 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.