6

I have some incorrect commit/push on my branch, then I want to revert to an old correct commit and push it on the current branch.

I have a problem with:

git log
    new: xxxxxxx
    ...
    old: ac758a3

git checkout ac758a3
git commit -m 'revert to old branch (ac758a3) to push it on current branch'

Out:

HEAD detached at ac758a3
nothing to commit, working directory clean

[EDIT]

Schematically:

  • A(correct commit that I want) --- B(incorrect commit) --- C(incorrect commit)

So I want to have the following flow:

  • A --- B --- C --- A

I want revert to A(old commit) and push it on current branch (the same branch).

What is the fast solution?


Thanks in advance.

Community
  • 1
  • 1
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • Please explain exactly what the final outcome should look like. Do you want to get rid of the commits? Do you want to revert them? Do you want to pretend they never happened but leave them in? – Lasse V. Karlsen May 22 '18 at 07:46
  • @LasseVågsætherKarlsen I edited the question as schematically. – Benyamin Jafari May 22 '18 at 07:54
  • There is more than one method. See https://stackoverflow.com/q/4114095/1256452 and also https://stackoverflow.com/q/2007662/1256452 (note the answer using `git read-tree`). – torek May 22 '18 at 14:35
  • @torek my problem is not these, I haven't any problem with rollback or revert to an old commit. my problem is after revert to an old commit what should I do to push it on current branch above last commit. – Benyamin Jafari May 22 '18 at 18:07
  • Both of those show you how to do the reversion, after which `git push` does the trick. – torek May 22 '18 at 18:21
  • @torek No, please read my question fully. thanks. – Benyamin Jafari May 22 '18 at 18:33
  • Please read [the specific answer I pointed out](https://stackoverflow.com/a/35245957/1256452) fully (direct link to answer this time), and then consider: `git checkout ; git read-tree ; git commit; git push`. – torek May 22 '18 at 18:47

3 Answers3

12

git reset --hard <commit-id> is your friend dear.

Follow below steps:

  1. On your current branch run command: git log
  2. Copy correct commit id from the log on which you want to go back.
  3. Run command: git reset --hard <commit-id-copied-in-step-2 above>
  4. Push your branch to remote: git push origin <my-feature-branch> -f.

Note: You will not be able to push it normally, you have to push it forcefully since we have changed the history of that branch and is not in sync with remote.

beingmanish
  • 1,040
  • 8
  • 21
  • 7
    This will work but will remove all the commits from the old commit to your current one. It will also alter the history of the branch so other people who have already pulled it will experience merge conflicts and will send you back the code again. – Noufal Ibrahim May 22 '18 at 06:59
1

These steps seems better to me, going back to a previous commit as a new commit:

First choose the commit-id you want to return to (using $ git log for example).

Then:

$ git checkout <commit-id> .
$ git commit -m "Reverting to <commit-id>"
$ git push

Re-posting from (thanks, Tolani):

https://medium.com/swlh/using-git-how-to-go-back-to-a-previous-commit-8579ccc8180f

arod
  • 13,481
  • 6
  • 31
  • 39
0

if you have already pushed commits and people have pulled it already, best solution is to add a new commit which effectively undo previous commits. Then things are normal.

you can undo series of commits using git revert

%git revert --no-edit  SHA1..SHA2

This will apply patches in reverse sequence and undoing changes. Please see git revert documentation for more information.

code707
  • 1,663
  • 1
  • 8
  • 20