1

I have modified my source directory by deleting certain files and done a commit. And I did a push origin master to remote host, github.

Now I came to know that I need those files for proper functioning. Now I need a way to go back my prev commit and then push to my remote host.

Is it possible to do so? I'm very new to git, I'm confused with it.

Thanks in advance.

batman
  • 4,728
  • 8
  • 39
  • 45

4 Answers4

5

Since you've already pushed, I would recommend against Nikhil's solution and recommend that you do a "git revert" on your latest commit like so git revert HEAD and then push. The revert command will create a new commit that undoes the effect the specified commit and adds it to your repository.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 3
    when I do `git revert HEAD`. I get an error : `error: Your local changes would be overwritten by revert. hint: Commit your changes or stash them to proceed. fatal: revert failed` – batman Aug 01 '12 at 06:36
  • 1
    @batman This means that the files on disk don't match the files on your head commit. Git's protecting you from confusing the changes you've already made with the changes the revert will make. I'd recommend using `git diff` to look at the changes. If those changes are important, use `git stash`, then `git revert HEAD` will succeed, then `git stash pop` to re-apply the important changes. If those changes are unimportant, *and you're really fine with losing them*, you can use `git reset --hard` to go back to the head commit, as described [here](http://stackoverflow.com/q/7846699/960195). – Adam Mihalcin Aug 01 '12 at 06:39
  • Ahhhh!!!! Its again throwing different errors. Let me put this way. I need to go back to commit `b9cb7fd3b2fcab5246b85a2437f154e38f9e38a3` and loose all the changes done after that commit. After that I need to `push` to remote! – batman Aug 01 '12 at 06:48
  • Look at the manual page for `git revert`. That gives you information on how to revert a list of changes. – Noufal Ibrahim Aug 01 '12 at 06:53
5

git revert creates a new commit that undoes one or more previous commits. This is usually the best way to undo commits that have already been pushed.

For example, this command would revert everything from the commit abc1234 up to and including the latest commit (HEAD):

git revert abc1234..HEAD

You could also use git reset, but this command changes history and would cause problems for anyone else who is using the repository. In general you should only use this command if you want to discard changes that haven't been pushed (and that you're sure you won't want to come back to in future).

georgebrock
  • 28,393
  • 13
  • 77
  • 72
2

Try on of these,

git reset --hard SHAsumOfYourCommit
git reset --hard HEAD [your current head point]
git reset --hard HEAD^ [your previous head point]

OR you can Delete the last commit

Let’s say we have a remote myrepo with branch master that currently points to commit dd61ab32, you can remove the last commit byusing the command.

git push myrepo +dd61ab32^:master
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41
  • 2
    All these three commands modify history. When you push them, you'll get a merge conflict. You can force a push but if there are other people who have already pulled, it's going to be a pain. – Noufal Ibrahim Aug 01 '12 at 06:31
  • I'm not sure your `push` command will work since that commit is already there on the remote. – Noufal Ibrahim Aug 01 '12 at 07:26
  • Yes it will work. git push myrepo +dd61ab32^:master for this command, it will First reset the branch to the parent of the current commit, then force-push it to the remote. We can also write it like this git reset HEAD^ --hard git push myrepo -f – Nikhil Dinesh Aug 01 '12 at 09:40
0

What's up friends, look, I was trying so hard, I even discovered a way, it works for me :)

git reset --hard commitId //delete your local commits
git push -f origin yourOrigin //delete your github remotes commits

I hope help you.

Algorithm
  • 21
  • 3