Premise
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 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.
~ In this post ~
- ๐ Log
- ๐ Amend commit
- โ Reword commit message
~ ๐ GIT LOG ~ โ
โHeaven gives its glimpses only to those not in position to look too close.โ
~ Robert Lee Frost
Take a step back so you can see the succession of all the commits made. You have two ways of doing this.
Let's put the second aside until you've learned some jiu-gitsu.
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.
You get a descending list of all commits (made on the branch in question). Each is associated with their SHA, 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:
~ Too much stuff?
git log --oneline
๐
No more than one commit per line, just the first seven characters of the SHA. And of course the message.
~ Or too little?
git log --stat
๐
In addition to what you get by default, you see files modified in the commit, the number of lines modified. Even a summary.
~ Need to see the differences between the last two commits?
git log --patch
๐
~ Could use filtering the commits by author
git log --author="John Doe"
You can even use regexp.
~ 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"
~ 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.
Once these are digested, you may want to indigestion - Binge, please ๐ฅง๐ฐ.
~ ๐ Amend Commit ~ โ
Use case
You just committed the creation of a file.
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:
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.
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.
~ ๐ Reword Commit Message ~ โ
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.
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.
~ Epilogue ~
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!
~ Command output ๐งฎ ~
output ๐
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
[--outline] output ๐
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
[--stat] output (fragment) ๐
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(-)
[--patch] output ๐
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
Top comments (2)
โฅ๏ธ for Title ๐๐
ahahah ๐