Lesson 27 of 60 – Git Aliases
45%

Git Aliases

Git aliases allow you to create short names for frequently used Git commands. They can save typing time and make commonly used commands easier to remember.

Note: A Git alias is a custom shortcut for a Git command. For example, you can create git co as a shortcut for git checkout or another command you frequently use.

1. What is a Git Alias?

A Git alias is a custom shortcut that represents another Git command.

For example, instead of typing:

git status

you can create an alias such as:

git st

The alias can make frequently used commands shorter.

2. Why Use Git Aliases?

Git commands can sometimes be long. Aliases can reduce repetitive typing.

  • Save typing time
  • Create shorter commands
  • Make common commands easier to remember
  • Improve personal Git workflow
  • Create custom shortcuts

3. Basic Alias Syntax

The basic syntax for creating a Git alias is:

git config --global alias.alias-name "command"

The alias-name is the shortcut you want to create.

4. Creating a Status Alias

You can create a short alias for git status:

git config --global alias.st status

Now you can run:

git st

instead of:

git status

5. Creating a Log Alias

You can create a shortcut for the log command:

git config --global alias.lg "log --oneline"

Then use:

git lg

6. Creating a Short Log Alias

A compact log view can be created with an alias:

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

Now:

git last

shows the most recent commit.

7. Using the --global Option

The --global option stores the alias in your global Git configuration.

git config --global alias.st status

This makes the alias available for your Git repositories on that computer.

8. Repository-Specific Aliases

You can create an alias only for the current repository by leaving out --global.

git config alias.st status

This stores the setting in the repository's local Git configuration.

9. Checking Git Aliases

You can view configured Git settings using:

git config --list

You may see entries such as:

alias.st=status
alias.lg=log --oneline

10. Viewing a Specific Alias

Use git config --get to check a particular alias:

git config --get alias.st

The output may be:

status

11. Creating an Alias for Git Diff

You can create a short alias for git diff:

git config --global alias.df diff

Then use:

git df

12. Creating an Alias for Git Branch

A short branch alias can be created:

git config --global alias.br branch

Now:

git br

can be used to run the branch command.

13. Creating an Alias for Git Switch

You can create a shortcut for switching branches:

git config --global alias.sw switch

Then:

git sw main

14. Alias with Command Options

Aliases can include command options.

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

Now:

git lg

runs the configured log command with its options.

15. Creating a Useful Log Alias

A useful history alias can combine several log options:

git config --global alias.hist "log --oneline --graph --decorate --all"

You can then run:

git hist

16. Removing an Alias

To remove an alias from the global configuration:

git config --global --unset alias.st

The alias st will no longer be available globally.

17. Removing a Local Alias

If an alias was created for the current repository, remove it with:

git config --unset alias.st

18. Git Alias for Common Workflow

You can create several aliases for commands used regularly:

git config --global alias.st status
git config --global alias.br branch
git config --global alias.sw switch
git config --global alias.df diff
git config --global alias.lg "log --oneline"

19. Aliases Do Not Create New Git Commands

A Git alias is a shortcut defined in Git configuration. It does not create a new Git program or change the underlying Git command.

For example:

git st

can be configured to execute:

git status

20. Alias Names

Alias names are chosen by the user. Common short names include:

st   → status
br   → branch
sw   → switch
df   → diff
lg   → log
ci   → commit
co   → checkout

Choose names that are easy for you and your workflow to remember.

21. Git Alias for Commit

A shortcut can be created for git commit:

git config --global alias.ci commit

Then:

git ci -m "Add login page"

runs the commit command with the message option.

22. Git Alias for Pull

You can create a shortcut for pulling changes:

git config --global alias.pl pull

Then use:

git pl

23. Git Alias for Push

A push alias can be created:

git config --global alias.ps push

Now:

git ps

can run the push command.

24. Git Aliases with Shell Commands

Git aliases can also be configured to execute shell commands by starting the alias value with an exclamation mark.

git config --global alias.current '!git branch --show-current'

Then:

git current

can execute the configured shell command.

25. Viewing Where an Alias Comes From

You can inspect configuration origins with:

git config --show-origin --get-regexp "^alias\."

This can help identify which Git configuration file contains an alias.

26. Common Git Alias Mistakes

  • Forgetting whether an alias is global or local.
  • Using confusing alias names.
  • Creating aliases that are difficult to remember.
  • Using an alias without knowing the command it runs.
  • Accidentally removing the wrong configuration entry.
  • Sharing scripts that depend on personal aliases without documenting them.

27. Useful Git Alias Collection

git config --global alias.st status
git config --global alias.br branch
git config --global alias.sw switch
git config --global alias.df diff
git config --global alias.ci commit
git config --global alias.lg "log --oneline"
git config --global alias.last "log -1"

These aliases can make common Git commands shorter.

28. Practical Git Alias Workflow

# Create aliases
git config --global alias.st status
git config --global alias.br branch
git config --global alias.lg "log --oneline"

# Use aliases
git st
git br
git lg

This provides shorter commands for commonly used Git operations.

29. Checking Your Aliases

git config --get alias.st
git config --get alias.br
git config --get alias.lg

You can use these commands to verify that your aliases are configured correctly.

30. Summary of Git Aliases

Git aliases provide convenient shortcuts for Git commands. They are especially useful for commands that you use frequently.

git config --global alias.st status

After creating the alias, you can use:

git st

Aliases can be global or repository-specific and can include command options.

📌 Key Points

  • Git aliases are shortcuts for Git commands.
  • git config is used to create aliases.
  • --global creates an alias in the global Git configuration.
  • Without --global, the alias can be repository-specific.
  • git config --list can display Git configuration.
  • git config --get alias.name checks a specific alias.
  • git config --global --unset alias.name removes a global alias.
  • Aliases can include command options.
  • Choose simple and meaningful alias names.
  • Document custom aliases when they are required by a shared workflow.

🧠 Quick Quiz

Question: Which command creates a global alias named st for git status?