Less typing means less mistakes, and more time on social media
Overview
Here is a deep dive into how you can define custom commands for Git to use.
I’ve stored several examples in a git-custom-commands folder within my repo.
https://github.com/wilsonmar/git-utilities
-
Fork it so you can make changes and add more files of your own.
-
Get my repo onto your machine using a Git UI or in a Git command line:
git clone https://github.com/wilsonmar/git-utilities
On a Mac:
-
cd into the repo.
cd git-custom-commands
The contents of this folder needs to be within the path where the operating system searches for programs and scripts.
-
Optionally, create a folder on the operating system’s path by copying the git-custom-commands folder from within the Git repository into a folder in your home directory.
Creating your own folder enables you to add more files to that.
cd $HOME mkdir git-custom-commands
Linux people often use /opt/bin, but on a Mac that is a protected area requiring sudo access.
-
Switch to a text editor to edit file ~/.bash_profile. For example, to use Sublime Text:
subl ~/.bash_profile
-
At the bottom or wherever, ADD the path to your git-custom-command files.
PROTIP: Git automatically make files available as a subcommand, just like regular executable scripts.
An example of referencing where I cloned:
export PATH="$HOME/gits/wilsonmar/git-utilities/git-custom-commands:$PATH"
This adds the folder to the front of the PATH.
-
View the PATH:
echo $PATH
-
So the changes “take”, exit terminal session windows or re-run Terminal initialization:
source ~/.bash_profile
-
cd into the git-custom-commands folder.
-
Set permissions to execute each file:
chmod 555 *
-
Skip to Try Git Echo below.
On Windows:
-
If you haven’t already, install Chocolatey, then
choco install mysysgit
-
Look for the libeexec\git-core folder:
cd / dir git-core /s
What I see:
Directory of C:\Program Files\Git\mingw64\libexec 04/21/2016 11:09 AM <DIR> git-core 0 File(s) 0 bytes Directory of C:\Program Files\Git\mingw64\share 04/21/2016 11:09 AM <DIR> git-core 0 File(s) 0 bytes
-
cd into the folder:
C:\Program Files\Git\mingw64\libexec\git-core
Older 32-bit machines would use:
C:\Program Files (x86)\Git\libexec\git-core
-
List files in git-core.
Notice several files do not have an *.exe file extension:
- git-bisect
- git-cittool
- git-cvsimport
- git-cvsserver
- git-difftool
- git-gui
- git-instaweb
- git-merge-octopus
- git-mergetool
- git-rebase
- git-request-pull
- git-send-email
- git-sh-setup
- git-sh-i18n
- git-stash
- git-subtree
PROTIP: The first line of these files define them as shell files which Git can process because Git executable on Windows has the bits to process shell files in sh.exe:
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login start "" "%SYSTEMDRIVE%\Program Files\Git\bin\sh.exe" --login
-
Copy the contents of git-custom-commands into Git client’s git-core folder found above:
Try git echo
-
Try the command git-echo, created just so we can verify whether we have it working:
git echo "hello world"
You should see response:
hello world
Instead of “hello world”, you can type in any phrase. Double quotes are not necessary if you only type one word.
PROTIP: The git echo command is handled by a file named git-echo, which has NO file extension (such as .sh). Git knows to add the dash in the name when it looks for a custom command.
The $1 is the place-holder for the message typed into the command.
Try git c “message”
The git-c custom command is the equivalent to typing this:
git add -A git commit -m '@mac: message in command line'
The command to invoke it:
git c “message in command line”
A sample response:
[master 181b537] @mac: message in command line 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 4
The underlying code:
git add -A git commit –message=”@${PWD##*/} $1”
The weird set of characters in the commit line produces the Present Working Directory (PWD).
It is not really needed to add the username because
Troubleshoot your path if you see this error message:
git: 'c' is not a git command. See 'git --help'. Did you mean one of these? checkout clone commit gc
View log
View the messages:
git log –oneline
Graphviz
Seth House did several videos on Git and GitHub, in which he showed use of a utility to create a visualization from the command line. He was nice enough to share it with me.
-
Install GraphViz using a package manager. On a Mac:
brew install graphviz
This installs dependencies libtiff, webp, gd
- http://www.graphviz.org/Download_macos.php - graphviz-2.40.1.pkg
- http://www.graphviz.org/Download_windows.php
-
Save the file from a text editor into folder
Alternately, copy the file graph-dag into that folder from the cloned folder.
-
Set permissions to execute the file:
chmod a+x graph-dag
-
Run the graph-dag that outputs a commit graph using the GraphViz “dot” tool:
git graph-dag HEAD~10.. | dot -Tpng > mygraph.png
-
Additionally, look at the other utilities at:
https://git.wiki.kernel.org/index.php/ExampleScripts
- Sorting commits by commit message line count / changed lines ratio
- Copying all changed files from the last N commits
- Setting the timestamps of the files to the commit timestamp of the commit which last touched them
Visualization of branches
git log does a good job of illustrating branches, but GitKraken provides this colorful branch graphics:
-
The master branch is the light-blue line on the left.
-
The Bug fix branch is the darker-blue line to the right of that.
-
The develop branch is the purple line to the right of that.
ASCII Art
Make some ASCII art from (part of your) history
A - B - C \ \ D - E - F
Resources
- http://mfranc.com/tools/git-custom-command/
More
This is one of a series on Git and GitHub:
- Why Git? (file-based backups vs Git clone)
- Git basics (script)
- Git whoops (correct mistakes)
- Git command shortcuts
- Git interactive merge (imerge)
- Git patch
- Git utilities
- Git hooks
- GitHub data security
- GitHub actions for automation JavaScript
- GitHub REST API
- GitHub GraphQL API
- GitHub PowerShell API Programming
- GitHub GraphQL PowerShell Module