I´ve educated myself a little about git, since it´s integrated in atom and therefore I can easily revert commits I did without having to do backups of my code all the time (If I understood GIT correctly).
Most of these tutorials about GIT also talk about Github, where you can then publish your master branch. But I´d like to develop everything locally, not publishing anything. Therefore, github is not an option. I want to only use GIT.
In Atom, the only way to revert commits you have done is by following the PUSH and PULL workflow as a github user. Since I´m not signed in to github in atom, I am not able to do anything more than initialize git in a directory, "stage" specific files/changes and commit them.
I´ve also downloaded git from the official webpage and configured it in cmd. How am I able to revert commits locally (via atom)?
via atom or a command-line-interface
I will show you two command-line-interface options.
Let's start out in a new repository.
$ mkdir how_to_undo_stuff_in_git
$ cd how_to_undo_stuff_in_git
$ git init # Initialize git (only needed once)
First, we'll create one file, myfile
, with the content foo
.
$ echo foo > myfile
$ git add myfile
$ git commit -m "Add foo content"
Next, we are going to apply a second commit that we want to undo.
$ echo bar >> myfile
$ git add bar
$ git commit -m "Add bar content (bad commit)"
The git revert
command can "cancel out" a commit for you. It does it by applying a new commit that, well, reverts your changes.
Simply run
$ git revert HEAD
You can now inspect the myfile
contents and see that the "bar" string disappeared. You can also run git log
and it should look like this
Author: Alan Turing <alan@hotmail.com>
Date: Sat Mar 9 15:28:51 2019 +0000
Revert "bar content"
This reverts commit bc483fc6fd069c70f6822a8b840f74ced64d32c8.
commit bc483fc6fd069c70f6822a8b840f74ced64d32c8
Author: Alan Turing <alan@hotmail.com>
Date: Sat Mar 9 15:28:35 2019 +0000
bar content
commit f6450431be631220210eb83c152d907058a6337d
Author: Alan Turing <alan@hotmail.com>
Date: Sat Mar 9 15:27:23 2019 +0000
foo content
git
also offers the option to really "erase" a commit, as opposed to creating a new commit that cancels a prior commit. Be careful when running git reset
because you can really loose parts of your work when you accidentally supply wrong arguments to it. Taken from: https://ohshitgit.com/#accidental-commit-master
$ git reset HEAD~ --hard
This will erase the last commit.