Automating your repitative git commands the right way
Tags: github, git, tutorial, productivity
Published: 2023-08-22 11:51:23

## Table of Contents
&nbsp;1. [What exactly is the problem?](#what-exactly-is-the-problem)

&nbsp;2. [Git commands are not much a hassle](#git-commands-are-not-much-a-hassle)

&nbsp;3. [Method 1: Creating a bash script](#method-1-creating-a-bash-script)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.1. [How to implement this solution?](#how-to-implement-this-solution)

&nbsp;4. [Method 2: Using git alias command](#method-2-using-git-alias-command)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.1. [How to implement this?](#how-to-implement-this)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.2. [Some useful blogs and resources](#some-useful-blogs-and-resources)

&nbsp;5. [Final thoughts](#final-thoughts)


## What exactly is the problem?


![Addressing the problem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pso2z33w31dt9iuynipp.png)

Well, we all have been using git commands a lot, while working on projects, I bet everyone has spammed `git commit` and `git add .` more than they've realized it.

Sometimes, writing long git commands can take up time, and writing them multiple times can build up frustration. Even some smaller commands can take time, which on compounding can show that you may have wasted hours by not using these shortcut tricks.

This blog post helps you to solve the exact problem. I'll show you different methods, to save your time and you can use any of the following depending on your workflow.

## Git commands are not much a hassle


![Git commands are not much a hassle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgsorn3t76h9udq52j50.png)

Well some of you may already come for me in comments saying these repitative git commands ain't that much hassle, but sometimes writing few letters less can help us not to lose sanity fast.

Let's talk about our first method

## Method 1: Creating a bash script


![Method 1: Creating a bash script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98aay1z2anyc53e87yfr.png)

We can indeed create a bash script which can help us to eliminate writing multiple commands. For e.g. you can eliminate writing `git add .`, `git commit -m ""`, `git push -u origin main`, and do it using a single custom command.

Following is an example of how to eliminate writing above three commands:

```bash
#!/bin/bash

# Recommend to use!
# Advance script to automate repitative commands

currentBranch="$1"
message="$2"

# Check if current branch and commit message are provided
if [ -z "$currentBranch" ]; then
    echo "Usage: $0 <currentBranch> <commitMessage>"
    exit 1
fi

if [ -z "$message" ]; then
    echo "Usage: $0 <currentBranch> <commitMessage>"
    exit 1
fi

# Check if git command is available
if ! command -v git &> /dev/null; then
    echo "Git command not found. Aborting."
    exit 1
fi

# Check if the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    echo "Not inside a Git repository. Aborting."
    exit 1
fi

# Check if there are changes to commit
if [ -z "$(git status --porcelain)" ]; then
    echo "No changes to commit. Aborting."
    exit 1
fi

# Add all changes
git add .

# Commit changes
git commit -m "$message"

# Push changes to origin
git push -u origin "$currentBranch"

echo "Automated Git tasks completed successfully."
```

### How to implement this solution?
1. Create a new file in your working directory named `git-quick`
2. Paste the above script in it
3. Make it executable using ```bash
chmod +x git-quick
```
4. Now you can run the file using ```bash
./git-quick
```

This was one of many use cases, you can automate other commands like this depending on your workflow.

> **Bonus:** You can also make it usable anywhere by following the following guide:
> 
> ![Guide on how to make your commands accessible across various sessions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7ajzygvopjlh5lkiw5z.png)

But there'll some people who like to do multiple commits before pushing the code.

Well there's another method you may try, which includes using the `git alias` command.

## Method 2: Using git alias command

![Method 2: Using git alias command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rmljqtw2phqbnj21ok9.png)

`git alias` is probably the most useful command when it comes to save that hassle when it comes to type longer commands

For e.g: `git push -u origin main` can be easily simplified as `git push` or even `git p`. As simple as it gets!

### How to implement this?
Well atlassian has a detailed guide related to using `git alias` command, you can check that out or here's a quick summary.

- Aliases can be created by either using `git config` as shown below, or it can also be created by editing `$HOME/.gitconfig` for global path, for local you can edit `./.gitconfig` of the active repository. Following is an demonstration about how to create aliases using both methods:
```js
// By using 'git config' command
$ git config --global alias.co checkout

// By editing './.gitconfig' file (paste the below code)
[alias]
 co = checkout
```

- You can use git alias to add short names to regular git commands.
```js
$ git config --global alias.co checkout
// Can be used as git co

$ git config --global alias.br branch
// Can be used as git br

$ git config --global alias.ci commit
// Can be used as git ci

$ git config --global alias.st status
// Can be used as git st
```

- You can also create some custom commands using git alias
```js
git config --global alias.unstage 'reset HEAD --'

// This can make following two commands equivalent
$ git unstage fileA
$ git reset HEAD -- fileA
```

### Some useful blogs and resources
- [Official documentations of git aliases](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)
- [Synk's blog discussing about -  git alias for faster workflow](https://snyk.io/blog/10-git-aliases-for-faster-and-productive-git-workflow/)
- [Gist discussing about git alias command](https://gist.github.com/mwhite/6887990#my-aliases)

## Final thoughts


![Final thoughts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cj9afg8px2ghqssjp927.png)

In order to become a **Git Ninja** you can use any of the two methods mentioned above depending on your workflow.

If you have some more tips that I may have missed, then feel free to share them in the comments.

If you found any mistake in this blog then correct me up in the comments.

Thank you for reading this far! ❣️

---

Follow me on Twitter [@eagledev_](https://twitter.com/eagledev_) to recieve some useful tips and tricks.

---