Using git aliases to add, commit and push to git with one command

One of the ongoing minor annoyances of my work day is constantly having to insert correct branch name during git commits -- cry me a river, I know. Don't get me wrong, this isn't so much of an issue when dealing with short branch names like staging, review, or even main, but it can quickly become bothersome and monotonous when it comes to ticket-based branch names.

Wouldn't it be great if we could have a single git command to add and push to the current branch automatically?

At my current place of work, branch naming convention checks are enforced directly within our build tools, which is fantastic because it ensures that every developer across every project & team adhere to the same naming conventions and enables us to easily identify the context of a specific branch.

This means that each branch names are typically 'ticketed' - i.e, its name will represent the content of a support ticket. Our current convention is similar to:

[folder]/[location]-[name]-[type]

So let's say I was resolving an issue where a mobile menu wasn't displaying correctly, the branch name would resolve to:

bugfix/global-mobileMenuDisplay-fix

It's great that all of our branch names are consistent, straight-forward and verbose, but it can become quite long winded to keep retyping these long branch names out manually each time that I interact with git.

For as long as I can remember, I've always had the name of the current branch name displayed in the terminal (If you're not doing this, I strongly encourage that check out how to add the current git branch to the prompt), so whilst I can copy the branch name from here - I'm clumsy and will accidentally copy a parenthisis, miss out the last letter, or something.

Terminal window with current branch name.

So if we can automate something like this, then why not?

Git Aliases

I must admit that before today, I hadn't ever added any git aliases, but I really wish that I'd done so sooner because they work in a similar manner to bash aliases where you can add simple keyboard shortcuts to execute longer commands:

$ ga = !git add

Simply put, this alias means that entering ga /path/to/file.js would be the equivalent to entering git add /path/to/file.js.

If we wanted to create more advanced aliases, we can use a bang to escape to the shell and write functions:

alias-name = !f(){ // code };f

Pretty cool, right? If you wrap the git alias in an anonymous bash function (f()), git will escape to the shell and we'll have access to command line variables. This unwanted (?) behaviour allows us to expand the scope of the aliases by writing custom functions and passing in parameters.

The result

Ultimately, I wanted a command that will allow me to do three commands in one:

$ git add .
$ git commit -m "My super fun custom message"
$ git push origin [current_branch]

Here's what I came up with:

# Output the current branch name
get-branch = !git rev-parse --abbrev-ref HEAD

# Shortcut to push to the current branch and set it to track the upstream branch.
publish = !git push -u origin $(git get-branch)

# Shortcut to add, commit and push in one command.
rocket = "!f() { \
        git add . && git commit -m \"$1\" && $(git publish); \
      }; f"

What's happening here?

For us to have a one-lined command that can add, commit and push, we needed to be able to do two things: Pass in custom parameters for the commit message, and automatically get the current branch. Using functions, we can escape to the shell to pass in paramters, and we use the get-branch alias to pull in the current branch name.

The second alias, publish, exists for scenarios where a one-lined command just won't cut it, but we still want a quicker way to push to the current branch. In this scenario, it serves as a good demonstration of how an alias can be interpolated within another.

How do you use rocket?

$ git rocket "My super fun, custom message"

Beautiful.

It's already saved me a lot of time and I've barely had this for a whole working day.

While this is great, we should always take care when working with git - so please be careful if you do decide to use this alias!