The Artima Developer Community
Sponsored Link

Java Buzz Forum
One weird trick for powerful Git aliases

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Mathias Bogaert

Posts: 618
Nickname: pathos
Registered: Aug, 2003

Mathias Bogaert is a senior software architect at Intrasoft mainly doing projects for the EC.
One weird trick for powerful Git aliases Posted: Oct 3, 2014 6:44 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Mathias Bogaert.
Original Post: One weird trick for powerful Git aliases
Feed Title: Scuttlebutt
Feed URL: http://feeds.feedburner.com/AtlassianDeveloperBlog
Feed Description: tech gossip by mathias
Latest Java Buzz Posts
Latest Java Buzz Posts by Mathias Bogaert
Latest Posts From Scuttlebutt

Advertisement

(Yes the title of this post is a pun, apologies!) I have written about aliases before! See for example a collection of my favorite git aliases or peruse my personal list on Bitbucket. Recently at our Summit I showed a simple technique that can really unleash the power of your Git command line. Several people commented that it was very useful and I am happy to elaborate on it some more here. Let me know your genius creations in the comments! (If you’re an old time git wrangler you know this already but it already but if not, read on!) Simple aliases are simple: you just add a line in the [alias] section of .gitconfig with your shortcut. For example: 1ls = log --oneline Will allow you to type git ls to get a bare list of short commit ids and commit messages. Or one of my favorites: 1caa = commit -a --amend -C HEAD Which will take all uncommitted and un-staged changes currently in the working directory and add them to the previous commit, amending it before pushing the change up. I use this all the time. Limitations of simple aliases There are many simple and useful aliases that you can craft like the above, but once you start to streamline your day to day routine you quickly realize there are some limitations to what you can do with simple aliases: Normal aliases can’t have parameters. You can’t execute multiple git commands in a single alias. You can’t use | (pipes) or grep. Or can you?! Actually there is a solution! Advanced alias template git allows you to shell out in aliases – that is to escape to a shell (like bash or zsh) using the ! (bang). This opens a new world of possibilities for your aliases. By forking a shell process we are able to access a whole lot of things: Shell expansions and parameters Use multiple git commands Pipes, greps and all Unix command line tools as needed BEAUTY! Here is the template for an advanced alias that requires parameters: 1my_alias = "!f() { 〈your complex command〉 }; f" The trick is to wrap our git command in an “anonymous” bash function – or better said a function named “f“. Doing it like this we have access to command line variables and shell expansions as the following: $1 to mean the first parameter passed to the command. $2 to mean the second parameter passed to the command. (and so on for $3, $4, etc.) $@ to mean all command line parameters passed. We can also now chain git commands with && and use the entire Unix toolkit. Example advanced aliases Now that I whet your appetite, let’s see a few examples of what you can do with all this power. Here a few ideas: Quickly add a remote pointing to your colleagues forks 123ra = "!f() { \         git remote add $1 https://bitbucket.org/$2.git; \       }; f" Define the above […]

The post One weird trick for powerful Git aliases appeared first on Atlassian Blogs.

Read: One weird trick for powerful Git aliases

Topic: Java : Collection Framework : ArrayList (Add Objects based on Index) Previous Topic   Next Topic Topic: Gradle Goodness: Running Groovy Scripts as Application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use