Compact Guide to Time Travel with Git 🔙 - I
Tags: github, git, beginners
Published: 2020-11-03 21:39:32

#### *Premise*
<center>*This serie of posts is inspired by* **[Sebastiaan van Stijn][Sebastiaan_van_Stijn_twitter_link]***'s unexpected comment on my previous post* - [Undoing commits][Undoing_commits_link]</center>

***

Who wouldn't like to travel back and forth in time? Mess up the order of events a bit, maybe tweak them to self-liking. Who knows, maybe erase one or two of them from existence.
Well, in **[Git][Git_official_website]**  ~~unlike in dull real life~~ you are given the control to do all of this. And once you learn to wield this power, you will only have to gain.

<a name="index"></a>
# <center><b>~ In this post ~</b></center>
1. 📃 [Log](#git_log)
1. 🙏 [Amend commit](#git_amend)
1. ✍ [Reword commit message](#git_reword_message)

***

<a name="git_log"></a>
### <center><b>~ 📃 GIT LOG ~</b> [☝](#index)</center>

> <i>“Heaven gives its glimpses only to those <b>not</b> in position to look too close.”</i>
~ **[Robert Lee Frost][Robert_Lee_Frost_wiki_link]**

Take a step back so you can see the succession of all the commits made. You have two ways of doing this.
<center>`git log` || `git reflog`</center>
Let's put the second aside until you've learned some <i>jiu-<b>git</b>su</i>.

You've been working on a branch for a while, who knows how many times you've committed - you're lost. You realize that to understand where to go you need to understand where you come from.
<a name="git_log_1"></a>
<center>`git log` [👁](#git_log_output)</center>
You get a descending list of all commits (made on the branch in question). Each is associated with their <abbr title="Secure Hash Algorithm">*SHA*</abbr>, *author*, *date*, *message*.

Here are some useful techniques that may come in handy on your time travel - you can use flags to modulate the output to your liking:

<a name="git_log_outline_flag"></a>
> ~ *Too much stuff?*
`git log --oneline` [👁](#git_log_oneline_output)
No more than one commit per line, just the first seven characters of the SHA. And of course the message.

<a name="git_log_stat_flag"></a>
> ~ *Or too little?*
`git log --stat` [👁](#git_log_stat_output)
In addition to what you get by default, you see files modified in the commit, the number of lines modified. Even a summary.

<a name="git_log_patch_flag"></a>
> ~ Need to see the *differences* between the *last two commits*?
`git log --patch` [👁](#git_log_patch_output)

<a></a>
> ~ Could use *filtering* the commits *by author*
`git log --author="John Doe"`
You can even use regexp.

<a></a>
> ~ The commit you are looking for was made between two specific dates? *Filter by date* then
`git log --before="2020-4-20 --after="2020-6-9"`

<a></a>
> ~ Do you need a *range* of commits?
`git log --skip 1 --max-count 2`
Starting counting from the last commit, skip one and return two.

<a></a>
Once these are digested, you may want to indigestion - Binge, please [🥧][git_log_official_docs][🍰][git_basics_viewing_commit_history].

***

<a name="git_amend"></a>
### <center><b>~ 🙏 Amend Commit ~</b> [☝](#index)
</center>

#### Use case
You just committed the creation of a file.
<center>`git commit -m "added foo.bar"`</center>
However, you immediately realize that it is incomplete. You could still implement the changes and make a new commit. By doing so, however, you would dirty the commit history.

#### Solution
Leave the commit where it is for now. Just focus on making the changes to the file, turn it into what it should have been before. So, add it to the staging area with `git add foo.bar`.
Then you just have to say, softly:
<center>`git commit --amend -m "making amends"`</center>
Then take a look at the commits timeline with `git log --oneline`. You will notice that the former commit, *added foo.bar* in this case, has been replaced by the latter.

> If you don't need to change the message use the `--no-edit` flag.

<a></a>
> You are not operating on the previous commit, but you are uprooting it and placing something new in its place. Take a look at commit it, before and after.

***

<a name="git_reword_message"></a>
### <center><b>~ 🙏 Reword Commit Message ~</b> [☝](#index)</center>
#### Use case
The commit content is okay but you made a typo (*ùùù* in the case below) in the message. Since you are a perfectionist you already know that you will lose sleep on it.
```
58e59c1 (HEAD -> master) number 3
c962644 number 2ùùù
c827324 number 1
```

#### Solution
The `git rebase` command will be explained later in the post. However, it can already be of tremendous help when used with the `-i` (*interactive*) flag. By specifying the range of commits you want to act on, it provides a set of tools.
<center>`git rebase -i HEAD~3`</center>
An editor opens. Above the series of selected commits (`HEAD~3` means the last three commits) followed by a series of commands.
```
pick c827324 number 1   
pick c962644 number 2ùùù
pick 58e59c1 number 3

# Rebase 68c0983..58e59c1 onto 68c0983 (3 commands)
#
# Commands:
# ...
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# ...
```
By pressing **i** you can interact and schedule a series of actions for git. Move the cursor to the felony commit, in this case the second one, and replace the `pick` command with `reword`
```
pick c827324 number 1   
reword c962644 number 2ùùù
pick 58e59c1 number 3
```
To exit the editor press **esc**, then type `:wq` and press **enter**. Now git will go through the schedule, allowing you to take action as required. 
In this case you have requested only a reword and you are shown the commit editor (the same editor that gets if you don't specify the `-m` flag at commit time).
Once again, press **i** to interact, edit the message and confirm with **enter**. Exit the editor by typing `:wq` and press **enter**.
Take a look at commits with `git log --oneline`. You have successfully renamed the commit message.
```
3d23de8 (HEAD -> master) number 3
22f3bcf number 2
c827324 number 1
```

> Note that the id of the commit in question has changed.

***
##### <b>~ Epilogue ~</b>
These simple commands allow you to take your first steps in the timeline. If you didn't know them before, you are probably happy now 🤠.

Just wait until you learn how to *reset*, *revert*, *delete*, *rearrange*, *split* **Git**.

Stay tuned and try not to tear space-time!

***

##  <center><b>~ Command output </b>🧮 ~</center>

###### <a name="git_log_output"></a>output [👆](#git_log_1)
```
commit 6abf4c864d54d2811cbe116a9bf04120cae19d4e (HEAD -> master, origin/core-add_index, core-add_index)
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:15:47 2020 +0100

    Update index.html

commit 6f4abbb647104e90fb63b5a1a17bfb3353f2906e
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:14:03 2020 +0100

    Add index.html

commit baa07c3a5c1fecce6876e2736b1bd0f9b3196761 (origin/master)
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:13:02 2020 +0100

    Update README.md

commit 9dfbdb52c6124943f0ff1def75bfa2a5ea0569be
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:12:26 2020 +0100

    Initial commit

```

<a name="git_log_oneline_output"></a>
###### [--outline] output [👆](#git_log_outline_flag)
```
6abf4c8 (HEAD -> master, origin/core-add_index, core-add_index) Update index.html
6f4abbb Add index.html
baa07c3 (origin/master) Update README.md
9dfbdb5 Initial commit
```

<a name="git_log_stat_output"></a>
###### [--stat] output (fragment) [👆](#git_log_stat_flag)
```
commit 6abf4c864d54d2811cbe116a9bf04120cae19d4e (HEAD -> master, origin/core-add_index, core-add_index)
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:15:47 2020 +0100

    Update index.html

 git_travel_in_time/index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
```

<a name="git_log_patch_output"></a>
###### [--patch] output [👆](#git_log_patch_flag)
```
commit 6abf4c864d54d2811cbe116a9bf04120cae19d4e (HEAD -> master, origin/core-add_index, core-add_index)
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:15:47 2020 +0100

    Update index.html

diff --git a/git_travel_in_time/index.html b/git_travel_in_time/index.html
index 059a4af..789ac5b 100644
--- a/git_travel_in_time/index.html
+++ b/git_travel_in_time/index.html
@@ -6,6 +6,6 @@
    <title>Compact Guide to Time Travel with Git</title>
 </head>
 <body>
-   
+   Here will go the link to the post
 </body>
 </html>
\ No newline at end of file

commit 6f4abbb647104e90fb63b5a1a17bfb3353f2906e
Author: John Doe <john.doe@email.com>
Date:   Mon Nov 2 01:14:03 2020 +0100

    Add index.html

diff --git a/git_travel_in_time/index.html b/git_travel_in_time/index.html
new file mode 100644
```

<!-- Links -->

<!-- External -->
[Sebastiaan_van_Stijn_twitter_link]: https://twitter.com/thaJeztah "Sebastiaan van Stijn's twitter"
[Undoing_commits_link]: https://dev.to/didof/git-github-made-simple-undoing-commits-1gc "previous post"
[Robert_Lee_Frost_wiki_link]: https://en.wikipedia.org/wiki/Robert_Frost "wiki"
[git_log_official_docs]: https://git-scm.com/docs/git-log
[git_basics_viewing_commit_history]: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
[Git_official_website]: https://git-scm.com/