Git Alias

Git is a powerful version control system with a plethora of commands to streamline your development workflow. However, typing lengthy commands repeatedly can be time-consuming. Git provides a solution—aliases. We'll explore various ways to use the git alias command, enabling you to create custom shortcuts for common Git commands and enhance your productivity.

1. Setting a Simple Alias:

To create a simple Git alias, use the git config command:

bash
git config --global alias.co checkout

This creates an alias co for the checkout command. Now, you can use git co instead of git checkout.

2. Creating Aliases with Parameters:

You can create aliases with parameters to make them more versatile. For example:

bash
git config --global alias.lg "log --oneline --graph --all"

This alias, named lg, displays a compact, graph-based log of all branches. Use it with:

bash
git lg
3. Chain Commands in an Alias:

Aliases can also execute multiple commands in sequence. For instance:

bash
git config --global alias.sync "fetch --prune origin && pull"

The sync alias fetches changes from the remote repository, prunes deleted branches, and pulls the changes into your local branch.

4. Alias for Creating a New Branch and Switching to It:

Combine commands to create a custom alias that streamlines creating a new branch and switching to it:

bash
git config --global alias.nb "checkout -b"

Now, you can create and switch to a new branch with:

bash
git nb feature-branch
5. Listing All Aliases:

To view a list of all your configured aliases, use:

bash
git config --get-regexp alias

This command displays a list of aliases and their corresponding commands.

6. Editing Aliases Directly in Config File:

You can edit the Git configuration file directly to add or modify aliases:

bash
nano ~/.gitconfig

Under the alias section, add your aliases:

plaintext
alias co = checkout lg = log --oneline --graph --all

Save the file to apply the changes.

7. Removing an Alias:

If you want to remove an alias, use the --unset option:

bash
git config --global --unset alias.alias-name

Replace alias-name with the name of the alias you want to remove.

8. Creating Shell Aliases for Git Commands:

You can also create shell aliases for Git commands directly in your shell configuration file (e.g., .bashrc or .zshrc):

bash
alias gs="git status" alias ga="git add" alias gc="git commit"

This allows you to use shortened versions of Git commands in your terminal.

9. Global and Local Aliases:

Git aliases can be configured globally or locally within a specific repository. Use the --global flag for global aliases and omit it for local ones:

bash
git config --global alias.g "log --oneline --graph --all" git config alias.l "log --oneline"
10. Creating a Shortcut for Force Push:

To create an alias for force-pushing to the remote repository:

bash
git config --global alias.fp "push --force-with-lease"

Now, you can force-push with:

bash
git fp
11. Aliases with Options:

You can create aliases with options to customize their behavior. For example:

bash
git config --global alias.undo "reset --soft HEAD^"

This alias, named undo, soft resets the last commit. Use it with:

bash
git undo
12. Alias for Showing the Last Commit:

To create an alias for showing the details of the last commit:

bash
git config --global alias.last "log -1 --stat"

Now, you can view the last commit with:

bash
git last