0

I've made several commits to a branch, but want to revert back to specific commit.

I use git log to find the commit id, then I use git reset --hard <commit id> but nothing happens. Then, when I try to push up to Github I get a non-fast-forward message.

Would anyone know what I should be doing?

MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • Can you explain exactly what your end-goal is? Are you trying to completely wipe out commits later than the point you are resetting to? Or are you simply trying to create a new commit that undoes commits up to that point? – nanofarad Jun 04 '18 at 01:32
  • The former. The last 3 or so commits I made were testing something. I now want to remove them – MeltingDog Jun 04 '18 at 01:33
  • Presumably you already pushed the commits? – jhpratt Jun 04 '18 at 01:45
  • Yes I have pushed them - they appear in my pull request on github – MeltingDog Jun 04 '18 at 01:51
  • Possible duplicate of [What does "Git push non-fast-forward updates were rejected" mean?](https://stackoverflow.com/questions/4684352/what-does-git-push-non-fast-forward-updates-were-rejected-mean) – phd Jun 04 '18 at 04:13
  • If you were just testing something maybe that’s a good opportunity to make another branch next time. – Dave Newton Jun 04 '18 at 04:14

2 Answers2

1

Once you perform a hard reset, your working tree and index are now at the state of the commit that you performed the reset to. The next time you create any commit you will have a new, divergent history for your repository (and the branch you were working on).

To resolve this, use --force when pushing to Github; this will indicate that you acknowledge that you will lose history by forcibly pushing your new, rewritten history to the repository.

Caveats: In general, --force is dangerous, especially if you're unsure of how you got to a state of needing it. There is typically no way to easily undo a faulty forced push. That said, if the commits you're trying to wipe out had something sensitive such as API keys, passwords, or the like, I'm not certain that this will guarantee that the commits will be irrecoverably wiped out.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

As from your question and comments what I understand that you make three commits on a branch and pushed that branch on the remote. and now you want to remove those 3 commits from remote and local branch.

I am not sure what options you are already tried and what is the current status of your local branch, so first, I synch your branch with remote and then remove top 3 commits and then pushed them on the remote.

I assume your branch name is xyz replace xyz with your branch name.

Follow below steps:

  1. get fetch origin #to fetch remote history
  2. git reset --hard origin/xyz # synch xyz with remote
  3. git reset --hard HEAD~3 # remove top 3 commits
  4. git push origin xyz --force-with-lease # push your branch on the remote.
rahul mishra
  • 1,390
  • 9
  • 16