Go back to the past

It is now time to use one of the major functionality of git: the time machine.

Erasing uncommitted changes

If you have changed a file and save theses changes (but not committed!), you can come back to the last version of the file by using

git checkout filename
This is a destructive action: changes are then deleted!

Viewing Commit history: git log

We already used the command git log that help to loop

Come back to previous commit

The git log provides the identifier of every commit, you can then use git checkout id_of_the_commit to detached HEAD (the “pointer”) to this commit:

❯ git checkout f4279af9643871dbe478a876b88ae0da2ef2764c
Note: switching to 'f4279af9643871dbe478a876b88ae0da2ef2764c'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
...

You can come back by checking out on master

❯ git checkout master
A safe way to do come back in history is to create a “dummy” branch, checking out on this branch and then, looping through history and commits within this branch.

Reset

This is a destructive action

Sometimes, nothing’s going right. A reset might be useful but please keep in mind this is a dangerous action: you need to be sure of what you are doing. To do that, git reset might be your friend:

git reset COMMIT_NUMBER

And sometimes, we need to show some muscle and force git to agree with us

git reset --hard COMMIT_NUMBER
Précédent