Git revert to previous local version after a pull request

119 views Asked by At

I am new to working with Git. I made changes to a website in a local folder. When I wanted to push them through, an error occurred:

[rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/id/repo.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.

I made the suggested pull request, unaware that this would erase all my local changes.

My terminal still shows the previous commands and commits. Is there a way to restore my local folder to the previous version, from before the pull request?

So a résumé:

  • I tried to push commits through, but they were rejected. I don't understand why.
  • I then did git pull <branch>, unaware it would erase my local changes.
  • I then did a Git reset, hoping it would erase my last action.
2

There are 2 answers

1
tomwaitforitmy On

I will try to order your thoughts quickly

  • The error message tells you exactly why you cannot push 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. In other words: on your server (remote) are changes, that you do not have locally in your current branch. Git does not know how you want to proceed with these changes.
  • A pull request is something different than git pull. From what I can tell you don't a pull request, you want do update your current local branch via git pull. Do not confuse pull requests with git pull https://git-scm.com/docs/git-pull
  • git reset is one of the few dangerous commands that may destroy code changes. Be careful using that command and learn about it first. It is not meant for "erasing your last git action".
  • I strongly suggest reading documentation and learning git with tutorials such as https://learngitbranching.js.org/ or the official docs https://git-scm.com/docs/git-push
  • If you still see your local changes in your history, they are not lost. You could for instance, create another local branch on a desired commit and check that branch out. Afterwards, you can merge that branch into your current branch and push, if there are no changes on remote.
  • I would suggest to use gitk (tool that is always installed with git) to view the history, create a branch, merge and visualize what you are doing.
  • Alternatively, use

git checkout <yourCommitHash> -b NewBranch to create a new branch on your commit and check it out

git merge <currentLocalBranch to merge your current local branch into your NewBranch

git push

0
Bill Huneke On

I think the answer provided by @tomwaitforitmy includes a lot of great advice. But for your situation, I think the solution is pretty simple:

You can checkout an old version of 'master' or any branch using something like this: git checkout master@{1}.

In that example, we are jumping back to what master was immediately before you did whatever just updated it (with git pull or git commit something.. or git reset ... If you want to go "further back" use {2} or {3} etc. You can go quite a ways back.

You can also look at these old refs without doing a checkout with: git reflog (which will show you all the activity which has changed the 'master' ref on your local dev repo).

After you do the above checkout, you will be in 'detached head' state. If/when you find a version of master that you like, you can put everything "right" again with something like this: git checkout master and then git reset master@{XXX} using whatever xxx ref you found has your old work.

After doing that, you will be on master again, but it will be your local master. Now that you have that, you can figure out what to do about properly merging the "non fast-forward" changes that now apparently exist on your central repo.