Benyamin Jafari
Benyamin Jafari

Reputation: 33986

How to checkout on an old commit and push it on current branch?

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:

So I want to have the following flow:

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

What is the fast solution?


Thanks in advance.

Upvotes: 9

Views: 14154

Answers (3)

arod
arod

Reputation: 14042

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

Upvotes: 1

code707
code707

Reputation: 1701

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.

Upvotes: 0

beingmanish
beingmanish

Reputation: 1110

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.

Upvotes: 13

Related Questions