Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git unstage without losing merge?

Tags:

git

merge

unstage

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?

like image 356
Ken Otwell Avatar asked Feb 03 '15 17:02

Ken Otwell


People also ask

How do you Unstage without losing changes?

Unstage commits softly Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index. As a consequence, your modifications are kept, they are just not in the Git repository anymore.

Is it allowed to Unstage files in git?

Unstage Committed FilesThe git reset command can allow changes to files and directories that have already been committed.

How do I Unstage merge changes?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do I Unstage everything in git?

If you've accidentally staged all your changed files you can unstage them all by using git reset . This should put you back in the state you were before staging all your changes files. Allowing you to stage changed files individually before you commit.


2 Answers

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>
like image 94
Chef Pharaoh Avatar answered Oct 11 '22 15:10

Chef Pharaoh


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

like image 23
manzur Avatar answered Oct 11 '22 15:10

manzur