Tuesday, March 15, 2016

Git aliases: Shortening Git commands

So today we'll see, how we can shorten the long git commands, 
which can help programmers save time and a few keystrokes while working with git.

So lets work with git a bit. (Git a bit. Lol :p, a rhyming scheme. Does that help me qualify to be a poet, song writer?, umm... a rapper maybe? Maybe i should write a rap on this, yo mayne...! )

We'll see 2 ways of adding aliases to our git commands.

How to add aliases to git commands: 

Way 1: Using the `git config` command

Use the git config command to do the same.

So lets add an alias to the `status` command of git as `s`

git config --global alias.s status

Lets add another for `add`

git config --global alias.a add

Another for `commit`

git config --global alias.ci commit

And another

git config --global alias.cim "commit -m"

Now, we can just fire the short commands instead of the long ones and see the result of the command

for eg. git status, we can write,

git s

Lets break down the above command & take a look at the options we passed:

config: This option tells git, that everything following should be treated as the configuration for git, and can be added in the .gitconfig (or .git/config file depending on other options) file.

--global: The `--global` option tells git to add the alias in your '.gitconfig' which is in your home folder,

which will be treated as your global settings for all git repos on your system, 
unless you have another .git/config file inside a particular repo, overriding the global .gitconfig. 
Try using the `--global` option since it will help you keep the same aliases across all repositories on your system, if you have a number of them.
Without the `--global` option git will consider the local repository by default.
Caveat: If you already have the <short_name> as an alias to some other previsouly added command in the config, the --global option will replace that previous one.

alias.<short_name> <git_command_name>: Tells git that <git_command_name> particular command will have <short_name> as its alias form.

Way 2: Using the .gitconfig file directly

If you see in the ~/.gitconfig (~ = your_home_folder which is, /home/<your_login_name>/.gitconfig ) file,
you will notice that the shortened commands have gotten added under the [alias] section
Like this:

So instead of firing a `git config` command, lets try adding, the aliases directly into this file.

Lets open the file,

vim ~/.gitconfig

go under the [alias] section & type 

rl = reflog

& save it & the file now looks like this.

And now we can check the `reflog` command too, by typing
git rl

So, there we have it.

Let us know, if the above post was of any help to you.
Until later.

Oh ya! The complimentary rap i mentioned.


Rap starts...

Hey yo! Let me tell you a few things about git,

but i'll teach you them, uh! bit by bit.
aye! but, bit by bit, is a 1 bit. (Suddenly, the rapper gets into Math/CS/Binary mode & talks about, Division/Binary division, as, bit by bit, bit_divided_by_bit, is 1bit/1bit is 0b0001/0b0001 = 0b0001 = 1),
oh my gosh!!! what is this sh*t??? (And.. the audience go...). 



1 comment:

Raga WebGraphics said...

Nice, will be helpful for the ones doing Git Programming.