Commiting changes
This is part two of a presentation on git, read the first part here.
Now that we have added the README file and commited it, git will know about its state and it will tell us about any changes done to it. So let’s make some changes!
$ echo "This is a new line" >> README
Git now tells us that the file has been modified.
$ git status
-> On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README
no changes added to commit (use "git add" and/or "git commit -a")
git diff
We can even ask git about what has changed since the last commit. Using the diff
command.
$ git diff
-> diff --git a/README b/README
index b07f0ed..0d4740e 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
This is a README
+This is a new line
git commit
We can now repeat our add/commit cycle to record the new changes to the file
$ git add README
$ git commit
or use an even shorter way to do the same
$ git commit README
You will notice that if we don’t use the -m
flag we will be taken to the system’s $EDITOR to write the commit message. On most systems this editor is vi
(and commonly aliased to vim
).
Writing a commit message in vim
If haven’t used vim
before it may feel strange. Vim is a great editor, but it can take a while to get used to. Here are the commands that you will need to know to write a commit message and exit the editor:
- Press
i
to enter Insert mode - Write your commit message
- Press
<Esc>
to exit Insert mode - Type
:wq
to save and quit
git log
Let’s take a look at our commit history so far
$ git log
-> commit af257613e14044199077bfdbe6c512b8f30208b7 (HEAD -> master)
Author: David Granström <david@davidgranstrom.com>
Date: Mon Nov 27 21:14:05 2017 +0100
Add new line of text
commit 9319ba3d14492e304e600634f89d6c470076ac0d
Author: David Granström <david@davidgranstrom.com>
Date: Mon Nov 27 20:54:17 2017 +0100
Initial commit
We can display a nice ASCII graph of the log output
$ git log --graph --pretty=format:"%h %d %s (%cr) <%an>" --abbrev-commit --date=relative
-> * af25761 (HEAD -> master) Add new line of text (2 days ago) <David Granström>
* 9319ba3 Initial commit (2 days ago) <David Granström>
We can colorize the output
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
…let’s make an alias of that
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
We can now get a colorized graph of the log output by typing git lg
If the log is larger than your current terminal height, it will be displayed in a pager program. You can then scroll through the log output using your arrow keys or by pressing j
or k
. To exit the pager press q
.
The content of this post was presented on a c0dereview meetup in December 2017, you can view the slides from the presentation here.