Skip to content

Latest commit

 

History

History
186 lines (138 loc) · 4.71 KB

3. Custom Git Aliases.md

File metadata and controls

186 lines (138 loc) · 4.71 KB

Custom Git Aliases

Git aliases are shortcuts for frequently used Git commands, saving you time and keystrokes. This page explains how to create, manage, and use custom Git aliases effectively.


1. What are Git Aliases?

Git aliases allow you to define custom shortcuts for long or complex Git commands. For example:

  • Replace git status with git st.
  • Combine multiple Git commands into a single alias.

2. Why Use Git Aliases?

  • Save Time: Shorten repetitive commands.
  • Increase Efficiency: Simplify complex workflows with custom shortcuts.
  • Personalize Git: Tailor Git to match your workflow preferences.

3. Creating a Git Alias

To create a Git alias, use the git config command.

Syntax

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

Example

  1. Create an alias for git status:

    git config --global alias.st "status"
  2. Use the alias:

    git st

4. Types of Aliases

Simple Aliases

Shorten commonly used commands:

git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.cm "commit"

Parameterized Aliases

Pass arguments to aliases:

git config --global alias.undo "reset HEAD~1"

Run the alias to undo the last commit:

git undo

Complex Aliases

Combine multiple commands:

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

Use git lg for a visual commit history:

git lg

5. Viewing Aliases

To list all configured aliases:

git config --get-regexp alias

Example output:

alias.st status
alias.co checkout
alias.cm commit

6. Removing an Alias

To delete an alias:

git config --global --unset alias.<alias-name>

Example:

git config --global --unset alias.st

7. Examples of Useful Git Aliases

Alias Command Usage
st status git st for git status.
co checkout git co for git checkout.
br branch git br for git branch.
lg log --oneline --graph --decorate --all git lg for a compact commit history.
last log -1 HEAD git last to view the last commit.
undo reset HEAD~1 git undo to undo the last commit.
save stash save -u git save to stash all changes.
rbi rebase -i git rbi HEAD~n for interactive rebase.
amend commit --amend --no-edit git amend to modify the last commit.

8. Using a .gitconfig File

Instead of running git config commands, you can define aliases directly in the .gitconfig file.

Steps:

  1. Open your global Git configuration file:

    nano ~/.gitconfig
  2. Add aliases under the [alias] section:

    [alias]
        st = status
        co = checkout
        br = branch
        lg = log --oneline --graph --decorate --all
  3. Save and close the file.


9. Best Practices for Git Aliases

  • Keep Aliases Simple: Use aliases for commands you frequently type.
  • Use Descriptive Names: Choose alias names that are intuitive and easy to remember.
  • Document Custom Aliases: If sharing repositories, provide documentation for custom aliases in use.

10. Example Workflow

Step 1: Create Aliases

git config --global alias.co "checkout"
git config --global alias.cm "commit -m"
git config --global alias.st "status"

Step 2: Use Aliases

  • Check the status of your repository:
    git st
  • Commit changes with a message:
    git cm "Add new feature"

Step 3: View Configured Aliases

git config --get-regexp alias

Conclusion

Git aliases are a simple yet powerful way to enhance productivity and streamline workflows. By customizing Git to fit your needs, you can focus more on development and less on repetitive commands.


Next Steps: Code Reviews