git alias

If you’re not using git as your source controller, you’re missing out.

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

I have been using it for several years now and always seem to learn new things about it. I have been familiar with its concept of “aliases”, but I’ve never actually taken the time to use them. As part of the workflow for the project I’m on, I end-up with a lot of feature branches left to rot in my fork — they sit there after they have been pulled into master via a pull request and corresponding merge.

After a while, I decide it’s time to do some housekeeping — and delete the older branches. I usually find myself googling “git delete remote branch“, (since I can never remember the command). Sometimes I even forget what the command is to delete a local branch too. With git aliases though we can combine both of these actions into a simplified command that is easy to remember.

Navigate to wherever your global .gitconfig resides on your computer — for example on my Windows box it’s here C:\Users\DavidPine\.gitconfig. Open the file in your favorite text editor. We are looking for an [alias] block, if one does not exist let’s create one at the bottom of the file. Under this block you can create multiple aliases where the left operand is the alias name followed by an = sign signifying that the right operand is the command.

With the alias above, you can now invoke commands like, git del myFeatureBranch. This will delete the remote branch first and then the local branch. The nice thing about the “&&” syntax is that command after each double ampersand will only execute if the command in front succeeded. I simply have a few echos and then the core git commands that I always forget. The $1 is the placeholder for the variable, in my case the “myFeatureBranch” that is being targeted for deletion.

DISCLAIMER:  Do not delete a branch that you currently have checked out, or that has a pending pull request, or has commits that haven’t been synced — in other words, use common sense.

For more on git aliases visit here. Enjoy!

 

Advertisements

One thought on “git alias

  1. Nice, here’s my list of alias’ for anyone interested:

    sub-init = “!init() { git checkout master && git pull && git submodule update –init –recursive; }; init”
    sub-up = “!up() { git submodule foreach git checkout master && git pull; }; up”
    sub-diff = “!diff() { git submodule foreach ‘echo $path git diff‘; }; diff;”
    springcleaning = for-each-ref –sort=-committerdate –format=’%(refname:short) %(committerdate:short)’
    unstage = reset HEAD —
    pu = !=”git fetch origin -v; git fetch upstream -v; git merge upstream/master”
    vim = !.git/hooks/vim

    br = branch
    # show all branches, even across remotes
    branches = branch -a –color -v
    # remove all local branches included in the current branch
    cleanup = !sh -c ‘git branch –merged | grep -E “^[^]” –exclude “^\smaster” | xargs -n1 git branch -d’
    co = checkout
    ctags = !.git/hooks/ctags
    # Show the diff between the latest commit and the current state
    d = !”git diff-index –quiet HEAD — || clear; git diff –patch-with-stat”
    # git di $number shows the diff between the state $number revisions ago and the current state
    di = !”d() { git diff –patch-with-stat HEAD~$1; }; git diff-index –quiet HEAD — || clear; d”
    # Diff
    df = diff
    dt = “!d() { git diff-tree –no-commit-id –name-only -r $1; }; d”

    Get short SHA-1 for object

    hash = rev-parse –short HEAD
    lc = log ORIG_HEAD.. –stat –no-merges
    # just for fun
    l33t = !”git shortlog | l33t”
    last = log -1 HEAD
    lol = log –abbrev-commit –pretty=format:’%h – %an, %ad : %s’ –graph
    lola = log –abbrev-commit –pretty=format:’%h – %an, %ad : %s’ –graph –all
    loln = log –abbrev-commit –pretty=format:’%h – %an, %ad : %s’ –graph –name-status
    ls = ls-files
    # depends on hub
    mypulls = browse — pulls/kingbin
    # Find FIXME, TODO, etc, optimized by @x3ro https://gist.github.com/3908985
    n = !”f() { git ls-files $1 | xargs notes | awk -F: ‘{ print $1,$2; print $3,$4; print $5}’ | grcat conf.notes | less -r; }; f”
    pullm = !”git pull origin master”
    pushm = !”git push origin master”
    rb = rebase
    rbc = rebase –continue
    # Interactive rebase with the selected number of latest commits
    reb = “!r() { git rebase -i HEAD~$1; }; r”tags = tag -l
    # track a remote branch
    track = checkout -t
    st = status
    # update all submodules
    subs = submodule foreach git pull origin master
    # clean up fully merged branches
    sweep = !git-sweep
    # list only untracked files
    untracked = ls-files –others –exclude-standard
    # remove a file from the index
    unstage = reset HEAD —
    up = “!git remote update -p; git merge –ff-only @{u}”
    wdiff = diff –color-words
    visual = !gitk
    #stash functions
    sshow = “!f() { git stash show stash^{/$} -p; }; f”
    ssave = “!f() { git stash save $
    ; }; f”
    sapply = “!f() { git stash apply stash^{/$*}; }; f”

    statust = “!f() { git status -uno; }; f”

    Like

Comments are closed.