2

I am currently using Git for deploying app to DigitalOcean VPS server. If something wrong with the deployment or the app broken due to bugs in the new source code. how can i rollback to the previous git commit?

I have found the following link useful but i am confused which command i should use.

How to revert Git repository to a previous commit?

Things i want to achieve:

  • I want to rollback to previous state of the app.
  • The rollback command should only change my server(DigitalOcean) codebase not the Github repository
  • The command can be automated.

I have read few answers about git checkout which required a commit hash i.e. 0d1d7fc32. However, This approach required some extra steps and might not be useful for automated script.

Thanks in Advance.

Community
  • 1
  • 1
Anam
  • 11,999
  • 9
  • 49
  • 63

2 Answers2

2

If you want to roll back to a previous commit just use: git checkout HEAD~

HEAD references the current commit

HEAD~ references the previous commit

HEAD~2 references the commit before the previous commit

To re-examine the most recent commit use git checkout <remote> <branch> which is probably git checkout origin master. This will effectively reverse the original command.

CorbinMc
  • 588
  • 2
  • 8
  • `git checkout origin master` what this command does? does it change the remote repository? – Anam May 22 '14 at 02:29
  • that will reverse the first command. The first command allowed you to view and work with an older commit. The second command will allow you to move forwards to the commit you were originally on, the one you feared contained source code bugs. – CorbinMc May 22 '14 at 02:36
1

Ok, so if you want to abandon all the work since your previous commit, the command is:

git reset --hard HEAD~1

The ~1 tells git to rollback to the previous commit. ~2 is the commit before that, and so on (~3, ~4, ...).

Doing this will not affect your GitHub repos, unless you push the modifications.

To have this command automated, you can write a script that executes this command on your repository folder.

PS: there are a lot of different ways you can do this. You should choose the option you're most comfortable with.

Victor Valente
  • 761
  • 9
  • 24
  • `The command can be automated`, I will use an script to do the job for me. is there any command which will detect the previous commit and rollback to that commit. – Anam May 22 '14 at 02:27
  • Please, accept the correct answer when/if you find it. It makes things easier for someone who has the same doubt. – Victor Valente May 22 '14 at 03:01