10

I modified some files in my branch and did a

git add --all

But this added some files that I didn't intend to add for the commit.

So I did a

git reset --soft HEAD~2 (instead of doing git reset HEAD)

But the previous commit was made by someone else and it caused a lot of files to be in modified/added/deleted status. Is there a way to get back to a stage where the only changes I see are the files added/modified by me? Since I didnt make an actual commit, is there a way to move my head back to master without blowing away my changes? git pull is causing merge conflicts as I didn't actually commit my changes.

Thanks!

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
kriver
  • 1,588
  • 3
  • 20
  • 33

3 Answers3

14

For future reference, you can review the commits that your behavior causes or leaves behind by calling git reflog , which will include commits that are no longer in your working tree.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • Does that mean I can move back to a previous stage in my branch that is recorded in reflog? If yes, how? – kriver Jan 03 '11 at 23:40
  • Looks like I can do this by a git reset --hard . But my problem is there is no reflog for the changes I added as I never commited them. :( – kriver Jan 03 '11 at 23:44
  • 3
    Oy. `git reset --hard` is a synonym for "delete some stuff if necessary", so you have to use it as a last last resort, when you're sure you've nothing that needs backing up. Sorry, I should have specified that I was giving a +1 vote to dan's command, the reflog is useful for added info, and will show -everything- that git does record. I believe that leaves adding stuff to the index out of the running. – Kzqai Jan 04 '11 at 00:06
  • Looks like git reflog doesn't record info about changes added but not committed. Thank you Dan and Tchalvak for the answers. – kriver Jan 04 '11 at 00:46
7

Just do git reset master. This will only update the index and what HEAD points to. It will not modify your work tree files.

Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
  • 1
    Doesn't look like it will work because I did a git reset --hard HEAD@{1} to undo my soft reset (from reflog). It blew away my changes. So is there a way to get the changes back after hard reset if you never commited (but added and soft reset'ed) those changed files? Thanks! – kriver Jan 03 '11 at 23:34
  • Unfortunately, no. If you've never committed a change and that change is removed from the work tree, there is no way to get it back. – Dan Moulding Jan 04 '11 at 13:53
-1

A visual solution: open git gui:

git gui

At bottom at the Commit Message box choose the radio button labeled Amend Last Commit. You'll see at the left the Staged Changes area. It contains the files you have added in the last commit. Click on the icons of the files you don't want in your commit to remove them from the index. Afterwards just commit again by pressing the Commit button.

Paul Pladijs
  • 18,628
  • 5
  • 28
  • 31
  • 1
    Keep in mind that git gui is not installed by default when you install git. I don't actually know what that command is so you may want to clarify what it is and how to get it so people don't downvote your answer. – Elijah Lynn Mar 06 '14 at 14:00