Programming

13 posts

Useful Git Aliases

Here is a list of useful aliases for git that can be used on the command line.

All of them for /etc/gitconfig or ~/.gitconfig

[alias]
    a = !git add -A && git commit -m
    ap =  "!f(){ git add -A && git commit -m \"$1\" && git push; };f "
    b = branch
    br = branch
    bra = branch
    cl = clone
    co = checkout
    cob = checkout -b
    com = commit
    ct = commit
    df = diff
    dif = diff
    hist = log --oneline --graph --decorate --all
    lg = log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30
    main = !git checkout main && git pull
    nb = !git checkout main && git pull && git checkout -b
    open = "!$(which xdg-open 2>/dev/null || which open  2>/dev/null || which start  2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g\")"
    pr = "!$(which xdg-open 2>/dev/null || which open  2>/dev/null || which start  2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g;s#\\$#/compare/main...$(git symbolic-ref --short HEAD)#g\")"
    recent = branch --sort=-committerdate --format=\"%(committerdate:short) %(align:25)%(authorname)%(end) %(refname:short)\"
    s = status
    st = status
    stat = status
    undo = reset --soft HEAD~1

Most of these are short abbreviatons so you don't have to type the entire git command, but a few of them are more complicated and are explained in detail below:

Add and commit with a commit message

Use case: you want to commit everything.

Without the alias: git add -A && git commit -m 'Feature X'

With the alias: git a 'Feature X'

Installing the alias: git config --global alias.a '!git add -A && git commit -m'

Add, commit with a commit message, and push to origin

Use case: you want to commit everything and push your work

Without the alias: git add -A && git commit -m 'Feature X' && git push

With the alias: git ap 'Feature X'

Installing the alias: git config --global alias.ap '"!f(){ git add -A && git commit -m \"$1\" && git push; };f "'

Show the git log with branch history graphed

Use case: you want to see the the git log with colors showing which commits were made on branches.

Without the alias: git log --oneline --graph --decorate --all

With the alias: git hist

Installing the alias: git config --global alias.hist 'log --oneline --graph --decorate --all'

Show the git log with colors

Use case: prettier git log

Without the alias: git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30

With the alias: git lg

Installing the alias: git config --global alias.lg '!git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30'

Create a new branch with the latest main

Use case: Starting a new feature branch and you want to make sure you have the latest changes from upstream

Without the alias: git checkout main && git pull && git checkout -b my-branch-name

With the alias: git nb my-branch-name

Installing the alias: git config --global alias.nb '!git checkout main && git pull && git checkout -b'

Open the repository in your web browser

Use case: You want to see the repository's origin in your web browser, for example on Github.

Without the alias: Easiest just to bookmark it

With the alias: git open

Installing the alias: git config --global alias.open '"!$(which xdg-open 2>/dev/null || which open 2>/dev/null || which start 2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g\")"'

Open the pull request URL in your web browser

Use case: Create or view the pull request webpage, for example on Github

Without the alias: Easiest to search on Github

With the alias: git pr

Installing the alias: git config --global alias.pr '"!$(which xdg-open 2>/dev/null || which open 2>/dev/null || which start 2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g;s#\\$#/compare/main...$(git symbolic-ref --short HEAD)#g\")"'

View recently touched branches

Use case: View the list of branches, ordered by newest first

Without the alias: git branch --sort=-committerdate --format="%(committerdate:s hort) %(align:25)%(authorname)%(end) %(refname:short)"

With the alias: git recent or git recent -a to see all recent branches including remote branches

Installing the alias: git config --global alias.recent 'branch --sort=-committerdate --format=\"%(committerdate:short) %(align:25)%(authorname)%(end) %(refname:short)\"'

Undo the last commit

Use case: You committed the wrong thing but haven't pushed yet. You want to undo the commit and put the committed files back in the working set without losing the changes.

Without the alias: git reset --soft HEAD~1

With the alias: git undo

Installing the alias: git config --global alias.undo 'reset --soft HEAD~1'

Solve Perl “Wide Character in Print”

Here is a simple Perl program that outputs a capital gamma character (Ɣ): print "\x{194}\n". When you run it, you will probably get a warning from Perl about a "Wide character in print at line 1".

$ perl -e 'print "\x{194}\n"'
Wide character in print at -e line 1.
Ɣ

You can solve this using several different methods.

Continue reading

TripAdvisor’s Hotel Availability Comparison

Since I spent about six months of my professional life working on TripAdvisor's hotel availability comparison feature, I thought a little writeup about it here would be appropriate. If you fill in the form over on the right you will be taken to a page on TripAdvisor that shows hotels that are available for the dates that you chose.

Continue reading

Finding a Loop in a Singly Linked List

Motivation

A singly linked list is a common data structure familiar to all computer scientists. A singly linked list is made of nodes where each node has a pointer to the next node (or null to end the list). A singly linked list is often used as a stack (or last in first out queue (LIFO)) because adding a new first element, removing the existing first element, and examining the first element are very fast O(1) operations.

When working with singly linked list, you are typically given a link to the first node. Common operations on a singly linked list are iterating through all the nodes, adding to the list, or deleting from the list. Algorithms for these operations generally require a well formed linked list. That is a linked list without loops or cycles in it.

Continue reading

Convert a Java OutputStream to an InputStream

If you have ever programmed using Java IO, you will quickly run into a situation in which a class creates data on an OutputStream and you need to send it to another class that expects to read the data from an input stream. You'll soon be asking the question, "How do I convert an OutputStream to an InputStream?"

Nowhere in Java will you find a OutpStreamToInputStreamConverter class. Luckily, there are several ways to go about this.

Continue reading

Convert a Java Writer to a Reader

If you have ever programmed using Java IO, you will quickly run into a situation in which a class creates data on a Writer and you need to send it to another class that expects to read the data from a Reader. You'll soon be asking the question, "How do I convert a Writer to a Reader?"

Nowhere in Java will you find a WriterToReaderConverter class. Luckily, there are several ways to go about this.

Continue reading

Finding Comments in Source Code Using Regular Expressions

Many text editors have advanced find (and replace) features. When I'm programming, I like to use an editor with regular expression search and replace. This feature is allows one to find text based on complex patterns rather than based just on literals. Upon occasion I want to examine each of the comments in my source code and either edit them or remove them. I found that it was difficult to write a regular expression that would find C style comments (the comments that start with /* and end with */) because my text editor does not implement the "non-greedy matching" feature of regular expressions.

Continue reading