Ken Otwell
Ken Otwell

Reputation: 365

Git unstage without losing merge?

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?

Upvotes: 6

Views: 6048

Answers (5)

jthill
jthill

Reputation: 60585

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 $_

Upvotes: 0

bgusach
bgusach

Reputation: 15205

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

git reset "*"

(The quotes are there to avoid shell expansion.)

Upvotes: 0

foobar
foobar

Reputation: 2941

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

git revert -m 1 <commit-hash>

Upvotes: -1

Chef Pharaoh
Chef Pharaoh

Reputation: 2406

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>

Upvotes: 5

manzur
manzur

Reputation: 692

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

Upvotes: 2

Related Questions