How to get the code to the previous commit back in GIT which has already been pushed to the server

870 Views Asked by At

I have made some changes to my code and then did a: git add . git commit git push origin master.

Now i realise that i have made some mistakes and i want to get back the code of the previous commit and then re do the changes and push them back to the server.

This is the git log output:

commit 3254c7668610a86eb930798c3c50fb5e1c298cbf
Author: Subhayan Bhattacharya <[email protected]>
Date:   Wed Sep 6 00:53:39 2017 -0700

    Made changed to downloadpkgapp.sh

commit c6577344eaa7ed1ac7fc911892cf85f66a0ac0b9
Author: Subhayan Bhattacharya <[email protected]>
Date:   Wed Aug 16 02:56:17 2017 -0700

    commiting changes to file CallProgram.py

commit 3839fa51edb20812c10d96f40b36aaa4fada9200
Author: dheeraj.mittal <[email protected]>
Date:   Sun Jun 11 11:17:41 2017 +0200

    added execute permission command for application package

Someone please help me with the same. I have trouble understanding GIT a lot. So if someone can also explain whatever steps are suggested that would be great.

3

There are 3 best solutions below

0
On

I suggest you to use an git graphical interface like Git Extensions. It will be more easy to follow what is happening...

With Git Extensions you can do:

1) be sure that you don't have any change that is not commited. Include any change in a commit to be sure you don't loose it

2) create a new branch in your last commit: click your last commit, press right button and select "Create new branch". In the dialog do not select the "Checkout after create option". Call the new branch something like "incorrect"

3) select your last correct commit and press right button and select "Reset current branch to here". Select "Hard" reset type.

4) redo all the code modifications with the commits you need

5) press "push" button and in the push dialog select "Options" and check "Force with lease" this will force the update of the server to your new commit.

Be careful!: if anyone has pulled your changes with your previous commits you will create a lot of confusion if you change the commit history in the server. If there is any chance someone has your previous commit you should maintain your commit history with the errors and do the corrections in new commits.

When you are sure you will not need it you could remove the "incorrect" branch.

0
On

If you have 3 commits (say A, B, and C) on a branch master, push them to a remote (say origin) and then decide that you've made a mistake with C and want to revert it, you should first make sure that no one else has pulled the branch. If they have, your commit is with them.

If not, you can first reset your master branch to B like so,

 git reset --hard B

Then force push master again using

 git push --force origin master

However, this is altering the history of the project so do this only if you understand what the consequences are.

3
On

Be carefull when you have pushed to remote repository, because others may have pulled your wrong commit.

I think you should consider just leave it there, and do some revert operation.

First, if your commit is just normal one, you could use git revert <last-commit> to simply revert your changes and push to repository.

Second, if your commit is a merge commit, you should be care full. Here you should use git revert <last-commit> -m 2 to revert your state to source branch. You could study Undoing Merges or git revert --help to learn something about it.