Git

Git Aliases

Using git with small commands

Table of Contents
  1. Clone and CD into the folder
  2. Useful aliases
  3. Old aliases
  4. Full list of aliases

Working with git is awesome. I used to just type every command by hand. I've even learned some flags to make things a bit easier, for example using -u instead of writing --set-upstream. Typing full commands when you are doing one or two commits per day, it's fine. But on my new job, on this first week, I've been cloning repos, creating branches, committing and pushing quite frequently.

So I decided to tick something that had been on my to-do list for ages - create short git aliases!

On this article, I'll share with you the aliases that I've created, but also some others that I have been using for years and I can't live without. Hopefully, this will be useful to you.

Clone and CD into the folder

This is one thing that I find a bit boring, cloning and then changing directory to the cloned repo. There has to be a better way to do this, well you could do something like git clone <url> && cd $_ but that would mean write more - yeah I'm lazy at times.

I decided to go in search of a good way to deal with this and came across this stackoverflow answer. It looked like it did what I wanted, after testing, it does!

bash
1gclone() {
2 git clone "$1" && cd "$(basename "$1" .git)"
3}

Now I can run gclone <url> and git will clone the repository and then change the directory into the new folder!

Useful aliases

There are a few commands that I tend to write all the time.

  • git commit -m
  • git commit -am
  • git push -u

Since I tend to use these so often, why not just creating an alias for each?

bash
1alias gpuo='git push -u origin'
2alias gpu='git push -u'
3alias gcam='git commit -am'
4alias gc='git commit -m'
5alias gbd='git branch -D'

I've decided to create an alias for when I'm trying to push a new branch to my remote repo which is always origin. This allows me to use the command as gpuo my-feature to push my-feature branch to my remote repository. I'm still trying to see if I will ever use the smaller alias gpu, if I don't I will probably just replace gpu with the gpuo one.

Old aliases

I've been using these two aliases since day one. The log one shows a nice graph of the git logs, while status one shows the git status in a short way. These work great, but I still have to type git first, so I decided to make them even shorter because why not.

bash
1alias gst='git status -s'
2alias glog='log --oneline --decorate --all --graph'

Full list of aliases

Just in case you want to copy all of these without having to click on each code block, here's the full list of my aliases. Let me know if you have any suggestion for these or if they are useful to you.

bash
1alias gpuo='git push -u origin'
2alias gpu='git push -u'
3alias gcam='git commit -am'
4alias gc='git commit -m'
5alias gbd='git branch -D'
6alias gst='git status -s'
7alias glog='log --oneline --decorate --all --graph'
8
9gclone() {
10 git clone "$1" && cd "$(basename "$1" .git)"
11}

Webmentions

0 Like 0 Comment

You might also like these

This article contains notes about what I've learned from lesson 6 of Missing Semester - Version Control (git).

Read More
Git

Git - Understanding version control

Git - Understanding version control

How to ignore a file globally without having to add it every single time to your .gitignore file.

Read More
Git

Gitignore a file globally

Gitignore a file globally

Learn how to deal with a git conflict, why it happens and how to fix conflicts directly from your editor.

Read More
Git

Git: Merge Conflicts

Git: Merge Conflicts

Learn about git rebase, pulling changes from a master branch and how to keep your git history under control.

Read More
Git

Git: Commit history

Git: Commit history