2

i wanted to squash few commits and i followed this so i did :

git reset --soft HEAD~20
git commit
git push -f

I don't know why but now my branch full of merging which wasn't before i want to revert the git reset --soft HEAD~20 command how can it be done? Thanks!!

UPDATE
i did : git reset < commit hash before the reset: moving to HEAD~20 > then
git push and im getting :

error: failed to push some refs to 'https://xxxx@bitbucket.org/xxxx/xxxx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
user63898
  • 29,839
  • 85
  • 272
  • 514
  • You can try using `git reflog` to get the sha-1 for the commit which you want your HEAD to point to . And then its `git reset --hard ` to restore your branch back to that commit. – Dev-vruper Feb 10 '21 at 13:47
  • why i need the "--hard" ? do i need to git push to also ? – user63898 Feb 10 '21 at 13:50
  • If you had any local changes and you don't want those changes again in your repo, you can use --hard option . It just brings the repo back to a state it was at the specified commit. – Dev-vruper Feb 10 '21 at 13:53
  • please see update question im getting error – user63898 Feb 10 '21 at 13:55
  • I think you need to force push again `git push origin --force` . I hope no-one else is pulling changes from your branch , as I see you already did force push once before. – Dev-vruper Feb 10 '21 at 13:58
  • Your second issue is a regular issue when your local branch has forked from the remote branch. Check your local history vs the remote history, and decide wether you should `push --force`, `merge`, `rebase` ... – LeGEC Feb 10 '21 at 13:59
  • 1
    Did resetting to that hash in the reflog solve your initial issue ? If so, you can mark @meshkati's answer as accepted. – LeGEC Feb 10 '21 at 14:00
  • 1
    FYI, don't forget to prefer `--force-with-lease` whenever you can... – Philippe Feb 10 '21 at 14:02
  • @user63898 maybe another dev has pushed something in the remote branch, since the commit you diverged (pull) – zeugor Feb 11 '21 at 10:39

1 Answers1

2

Run:

git reflog

It will show you something like this:

0475497 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~20
ab155bc HEAD@{1}: commit: commit before reset
5da1f69 HEAD@{2}: commit: commit message
c2d9604 HEAD@{3}: commit: commit message
.
.
.

Then you can checkout or reset to the commit before reset, by using its commit hash ( in the above example, it's ab155bc )

meshkati
  • 1,720
  • 2
  • 16
  • 29