Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github

LinkedIn

Less typing means less mistakes, and more time on social media

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

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
   
  1. Fork it so you can make changes and add more files of your own.

  2. 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:

  3. 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.

  4. 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.

  5. Switch to a text editor to edit file ~/.bash_profile. For example, to use Sublime Text:

    subl ~/.bash_profile
    
  6. 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.

  7. View the PATH:

    echo $PATH
    
  8. So the changes “take”, exit terminal session windows or re-run Terminal initialization:

    source ~/.bash_profile
    
  9. cd into the git-custom-commands folder.

  10. Set permissions to execute each file:

    
    chmod 555 *
    
  11. Skip to Try Git Echo below.

    On Windows:

  12. If you haven’t already, install Chocolatey, then

    
    choco install mysysgit
    
  13. 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
    
  14. 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
    
  15. 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
    
  16. Copy the contents of git-custom-commands into Git client’s git-core folder found above:

    Try git echo

  17. 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.

  1. 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

  2. Save the file from a text editor into folder

    Alternately, copy the file graph-dag into that folder from the cloned folder.

  3. Set permissions to execute the file:

    
    chmod a+x graph-dag
    
  4. Run the graph-dag that outputs a commit graph using the GraphViz “dot” tool:

    
    git graph-dag HEAD~10.. | dot -Tpng > mygraph.png
    
  5. 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:

git-utilities gitkraken dag 579x279

  • 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:

  1. Git and GitHub videos

  2. Why Git? (file-based backups vs Git clone)
  3. Git Markdown text

  4. Git basics (script)
  5. Git whoops (correct mistakes)
  6. Git messages (in commits)

  7. Git command shortcuts
  8. Git custom commands

  9. Git-client based workflows

  10. Git HEAD (Commitish references)

  11. Git interactive merge (imerge)
  12. Git patch
  13. Git rebase

  14. Git utilities
  15. Git-signing

  16. Git hooks
  17. GitHub data security
  18. TFS vs GitHub

  19. GitHub actions for automation JavaScript
  20. GitHub REST API
  21. GitHub GraphQL API
  22. GitHub PowerShell API Programming
  23. GitHub GraphQL PowerShell Module