-3

I have a branch called: feature_media.

I wanted to squash commits by running this command :

git reset $(git merge-base master feature_media)

This command cancels my last commits and unstages the changes. I want to undo the result of this command and recovering last commits.

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • I'm not familiar with that dollar syntax. What does it do? – isherwood Sep 16 '20 at 20:31
  • 5
    @isherwood that's just a shell expansion - it evaluates the command on the inside then passes that as the arg. – Daniel A. White Sep 16 '20 at 20:34
  • You could check the revision you were on before running reset by running `git reflog`. When you yet the Id of the revision, git reset there. But what you did looks ok. Try with `git reset --soft` and you should get all your changes in the index ready to commit a single commit with all the changes (squashed). – eftshift0 Sep 16 '20 at 20:49
  • 4
    Does this answer your question? [How to undo 'git reset'?](https://stackoverflow.com/questions/2510276/how-to-undo-git-reset) – krisz Sep 16 '20 at 20:52
  • 1
    @isherwood It's [Command substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution). See also https://wiki.bash-hackers.org/syntax/expansion/cmdsubst and http://mywiki.wooledge.org/CommandSubstitution – phd Sep 16 '20 at 23:22

2 Answers2

2

You can get a list of the commits that your current branch pointed at, from most recent to least recent, by running

git reflog [branch-name]

where [branch-name] is the current branch name. So what you could do is run this command to look at the history of the branch, find the ID of the commit ([commit-id]) it pointed at before you ran your reset command, and then do

git reset [commit-id]

You might use the --hard option if you want to discard any uncommitted changes in the working directory.

A possible shortcut for this is

git reset [branch-name]@{1}

which resets the branch to the last commit it pointed at before its current state. That will work if and only if the reset you want to undo is the last thing you did to the branch. If you've made any commits or other changes to the branch since then, this won't do the right thing, and that's why I suggested looking at the reflog.

You can also use things like

git reset '[branch-name]@{5 hours ago}'

if you have a good idea of when the branch last pointed at the thing you want it to point at.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • Is there way to do wit with Gitlens ( VScode) ? – Menai Ala Eddine - Aladdin Sep 17 '20 at 13:02
  • Possibly, but I don't know. That might be a good thing to ask as a separate followup question. – David Z Sep 20 '20 at 02:00
  • I used `git reset HEAD^1` to come to commit id `a` and then in `git reflog` I can see that the commit I want to recover is at `HEAD@{6}`, so I used `git reset HEAD@{6}`. Nothing recovered, `git log` shows that I am still at commit `a`. – Tiina Jul 24 '23 at 09:05
1

Run git reflog. Assuming you haven't made further changes, it should produce output like:

af32a10 HEAD@{0}: reset: moving to af32a1
7be9d53 HEAD@{1}: commit: foo bar
...

Now pick the commit that you wish to restore to. In your case, you likely can run:

git reset 'HEAD@{1}'
nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29