Saw an article mentioning the use of command line aliases for routine Git commands. Saves some typing, and in my case avoids typos. I am forever typing git satus
or some such. So, decided to check out the documentation, create some aliases and write up a post on the subject. Will do my best to make it my own words and such.
Let’s start by having a look to see if there are any aliases already defined.
PS R:\learn\py_play> git config --global -l
user.email=barkncats@example.com
user.name=koach aka Rick K
Status
Well doesn’t appear to be. But you’ll have a better idea once we actually define one and repeat the above command. So, let’s start with that satus command. Which will also show how to define an alias. Now I don’t do anything really fancy with the sttus command. So, basic it is.
## define a simple alias
PS R:\learn\py_play> git config --global alias.st status
## let's see if it's there
PS R:\learn\py_play> git config --global -l
user.email=who@example.com
user.name=koach aka Rick K
alias.st=status
## yup, let's try it
PS R:\learn\py_play> git st
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
I expect sttus typos are a thing of the past. And, when I went over to another repository. There it was. Lovely!
Now onto a few more that might be worthwhile or simply good for lazy people.
config
And, why not alias the listing of the global .gitconfig file.
PS R:\learn\py_play> git config --global alias.cfg 'config --global -l'
PS R:\learn\py_play> git cfg
...
alias.st=status
alias.cfg=config --global -l
Push
PS R:\learn\py_play> git config --global alias.p push
PS R:\learn\py_play> git cfg
...
alias.st=status
alias.cfg=config --global -l
alias.p=push
PS R:\learn\py_play> git p
Everything up-to-date
And, I just made a commit on this post so far. Note the git st and git p. I might really get to like this.
PS R:\hugo\tooOldCode> git add .
PS R:\hugo\tooOldCode> git st
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: content/post/git_alias/index.md
PS R:\hugo\tooOldCode> git commit -m "feat: start new post, git_alias/*" -m "going to cover using git command line aliases to perhaps speed things up and reduce typos"
[master 8312022] feat: start new post, git_alias/*
1 file changed, 52 insertions(+)
create mode 100644 content/post/git_alias/index.md
PS R:\hugo\tooOldCode> git p
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 16 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 1.41 KiB | 362.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:?????
27c5c78..8312022 master -> master
Commit
git commit -m “” is another one I seem to have trouble with for some reason. Now, I am not sure I should use an alias, but I do at times stumble typing it out. And, almost always forget the first -m. So, I thought I’d see what I could do.
PS R:\hugo\tooOldCode> git config --global alias.cm 'commit -m '
PS R:\hugo\tooOldCode> git cfg
...
alias.st=status
alias.cfg=config --global -l
alias.p=push
alias.cm=commit -m
PS R:\hugo\tooOldCode> git st
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: content/post/git_alias/index.md
no changes added to commit (use "git add" and/or "git commit -a")
PS R:\hugo\tooOldCode> git cm "testing"
error: pathspec 'testing' did not match any file(s) known to git
Oops! Seems to be a problem with the parameter using this alias? Turns out it was the space at the end of the above alias definition.
PS R:\hugo\tooOldCode> git config --global alias.cm 'commit -m'
PS R:\hugo\tooOldCode> git cm "testing"
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: content/post/git_alias/index.md
no changes added to commit (use "git add" and/or "git commit -a")
Directly Editing .gitconfig
You can also directly edit the global .gitconfig and add an alias. Let’s try that. But, where is the file on a Win10 system. Turns out that Git, since version 2, can help us with that. I am going to trim the ouput down to the bit that counts.
PS R:\hugo\tooOldCode> git config --list --show-origin
...
file:E:/appDev/Git/etc/gitconfig pull.rebase=false
file:C:/Users/somedir/.gitconfig user.email=who@example.com
file:C:/Users/somedir/.gitconfig user.name=koach aka Rick K
file:C:/Users/somedir/.gitconfig alias.st=status
file:C:/Users/somedir/.gitconfig alias.cfg = config --global -l
file:C:/Users/somedir/.gitconfig alias.p=push
file:C:/Users/somedir/.gitconfig alias.cm=commit -m
file:.git/config core.repositoryformatversion=0
...
So, my global .gitconfig
is located in C:/Users/somedir/. Makes sense, as that is where $HOME
points. But nice to confirm that location. I opened the file and it looks like this (with an edit of the email value.).
[user]
email = who@example.com
name = koach aka Rick K
[alias]
st = status
cfg = config --global -l
p = push
cm = commit -m
Log Last Commit
I added the following to the [alias]
section.
last = log -1 HEAD
And, at the command line:
PS R:\hugo\tooOldCode> git last
commit 8312022123be914cf38ad13d2960050cfdcc49f0 (HEAD -> master, origin/master)
Author: koach aka Rick K <who@example.com>
Date: Thu Mar 11 16:42:20 2021 -0800
feat: start new post, git_alias/*
going to cover using git command line aliases to perhaps speed things up and reduce typos
Nice! And, git cfg
gives me the following.
PS R:\hugo\tooOldCode> git cfg
...
alias.st=status
alias.cfg=config --global -l
alias.p=push
alias.cm=commit -m
alias.last=log -1 HEAD
Branch
Every so often I feel a need to list all the branches related to a repository. And, I like the extra info the verbose parameter provides. So, once again editing the file I still have open, I added:
br = branch -lav
And,
PS R:\hugo\tooOldCode> git br
* master 8312022 feat: start new post, git_alias/*
remotes/origin/master 8312022 feat: start new post, git_alias/*
remotes/to2cssh/master 8eaba19 execution_timing.md: draft currently considered finished
PS R:\hugo\tooOldCode> git cfg
...
alias.st=status
alias.cfg=config --global -l
alias.p=push
alias.cm=commit -m
alias.last=log -1 HEAD
alias.br=branch -lav
One Last Example
The commit from the last day. Everything but this current section which I just added now.
PS R:\hugo\tooOldCode> git st
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: content/post/git_alias/index.md
no changes added to commit (use "git add" and/or "git commit -a")
PS R:\hugo\tooOldCode> git add .
PS R:\hugo\tooOldCode> git st
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: content/post/git_alias/index.md
PS R:\hugo\tooOldCode> git cm "feat: add more aliases to post" -m "add/update/complete following sections: 'config', 'Push', 'Commit', 'Directly Editing .gitconfig', 'Log Last Commit', 'Branch'" -m "debate calling it quits for this one"
[master 33c0103] feat: add more aliases to post
1 file changed, 166 insertions(+), 4 deletions(-)
PS R:\hugo\tooOldCode> git p
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 16 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 3.12 KiB | 799.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:?????
8312022..33c0103 master -> master
Until Next Time
I am sure there are any number of aliases I could consider. Expect there are a good many potential ones related to logging and perhaps diffs. But, this should give me a good start. Until next time.
Resources
- 2.7 Git Basics - Git Aliases