Skip to content
Mark Fisher edited this page Jan 16, 2024 · 3 revisions

I have the following I add to every git config I have on every machine I'm ever working on. They simplify doing certain git commands:

This section goes in the file ~/.gitconfig

[alias]
   st = status
   hist = !sh -c 'git log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short -${1:-20} ${2:-HEAD}' -
   amend = !git log -n 1 --pretty=tformat:%s%n%n%b | git commit -F - --amend
   ls = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate
   ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
   dr  = "!f() { git diff "$1"^.."$1" ${2:-}; }; f"
   lc  = "!f() { git ll "$1"^.."$1" ${2:-}; }; f"

Here's some examples of using them:

Git history, with connections showing parents

Use git hist. Without a parameter it will default to 20 lines. With a parameter, it will limit it to less. Also allows targeting other git refs. See git docs to understand that, the simplest version is:

$ git hist 5
* eeee68bb 2023-12-30 | Add ipify app to test network-lib json functions (HEAD -> master) [Mark Fisher]
* dc641090 2023-12-29 | Talk to my site via HTTPS; new version bump (tag: apod-2023-12-29, upstream/master, origin/master, origin/HEAD) [Bill Kendrick]
*   257d5683 2023-12-29 | Merge branch 'master' of https://github.com/FujiNetWIFI/fujinet-apps [Bill Kendrick]
|\
| *   a241e9ac 2023-12-28 | Merge commit 'a853c6a7a0eb343c355428dbe40218498744558b' [idolpx]
| |\
| | * a853c6a7 2023-12-26 | clean up jsontest crossplatform makefile [Mark Fisher]

Git history with just titles, no connected lines

You can use git ls with or without a parameter for the number of lines to show. Without, it will show everything.

$ git ls -5
eeee68bb (HEAD -> master) Add ipify app to test network-lib json functions [Mark Fisher]
dc641090 (tag: apod-2023-12-29, upstream/master, origin/master, origin/HEAD) Talk to my site via HTTPS; new version bump [Bill Kendrick]
257d5683 Merge branch 'master' of https://github.com/FujiNetWIFI/fujinet-apps [Bill Kendrick]
a241e9ac Merge commit 'a853c6a7a0eb343c355428dbe40218498744558b' [idolpx]
4913aae7 don't translate underscore to petscii glyph [idolpx]

git ll is similar, but also shows the change summary within the commits:

$ git ll -2
eeee68bb (HEAD -> master) Add ipify app to test network-lib json functions [Mark Fisher]
1       0       ipify/Makefile
1       0       ipify/README.md
1       0       ipify/ipify.c
1       0       ipify/ipify.h

dc641090 (tag: apod-2023-12-29, upstream/master, origin/master, origin/HEAD) Talk to my site via HTTPS; new version bump [Bill Kendrick]
3       0       apod/CHANGES.txt
11      11      apod/fetch.c
3       2       apod/screen_helpers.c
2       2       apod/version.h

Show git "diff" for a particular commit

Use git dr <ref> to see what that commit contains:

$ git dr dc641090
diff --git a/apod/CHANGES.txt b/apod/CHANGES.txt
index 863160ab..714f2d16 100644
--- a/apod/CHANGES.txt
+++ b/apod/CHANGES.txt
@@ -1,6 +1,9 @@
 apod (Astronomy Picture of the Day) Client for #FujiNet
 Change Log

+ * 2023-12-29
+   + Talking to my websever over HTTPS now.
+
  * 2021-06-10
    + Split colorbars into a test version of the APOD app,
      so the main app has a little more breathing room.
... etc.

Note you can get the ref from the first part of git hist, it is the short commit id hash.

You can also use "HEAD" or any other valid git reference.

Amending your top commit with new changes

The command git commit --amend can be used to update your latest commit message (THAT YOU HAVE NOT PUSHED TO GITHUB!!) as previously mentioned.

However it is more usual to use it to add new file changes into your current commit without creating a new commit on top, so you only have 1 git commit you're constantly updating with your changes.

Here's an example session

# ... we edit some files, and commit them
$ git add -A
$ git commit -m 'Add ipify app'     <----- (1)

# then you notice there was a typo, or you want to change some functionality
# so you edit the files, then at the end:
$ git add -A           # this adds your changes to git

# this uses the alias to change the commit we have previously made at (1)
# without creating a second commit.
# Note, don't run this if you haven't already done part (1) else you'll amend
# the current HEAD commit that's probably something from upstream!
$ git amend

This will apply git commit --amend but use the same commit message you previously typed, skipping the message dialog completely.

There is an important thing to realise here: you are forever changing your previous commit, so you cannot get back to the previous version easily if your "amend" needs to be rolled back, it can't. Usually this isn't a problem, you forgot a small part, and want to just update the current commit you made rather than having 2 commits you later squash. Simply 'amend' the current commit with the new changes. This alias helps you do that without the annoying commit message dialog.

But remember, do not use amend if you have already pushed the change to a shared repository. That will lead to a lot of problems. It's a simple rule, if you've pushed a change to a remote repository, don't amend the commit locally. Only amend commits you haven't yet sent anywhere else. This includes rebasing, which is just another form of amending commits.

Clone this wiki locally