Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

My git aliases

April 18, 2023

When embarking on the journey of code development, one tool that becomes indispensable is Git. It’s like a magic diary that keeps a record of all your changes and organizes your project’s history. But typing out full commands in the console can sometimes feel tedious. That’s where Git aliases come into play—they’re like nicknames for those longer commands you frequently use. They save time and reduce the chance of errors.

Let’s say you’re working on a project and you want to check the current status of your files. Normally, you’d type git status. But with an alias, you can simply type git st. Here’s how you can set this up and some more examples to make your coding life easier:

  1. Status Check: Instead of typing git status, you can just use git st.

    To set up this alias, run the following command in your console:

    1
    
    git config --global alias.st "status"
    
  2. Commit Changes: To save a snapshot of your changes with a message, you might use git commit -m "Your message". With an alias, you can shorten this to git cm "Your message".

    Set it up like this:

    1
    
    git config --global alias.cm "commit -m"
    
  3. Switching Branches: The command to switch branches in Git is git checkout <branch-name>. An alias could be git co <branch-name>.

    Here’s the command to create the alias:

    1
    
    git config --global alias.co "checkout"
    
  4. Creating and Switching Branches: If you want to create a new branch and switch to it in one go, you’d normally use git checkout -b <branch-name>. A handy alias for this is git cob <branch-name>.

    Create this alias by typing:

    1
    
    git config --global alias.cob "checkout -b"
    
  5. Pulling Changes: To update your local repository to the newest commit, execute git pull. An alias? Try git pl.

    Set it up with:

    1
    
    git config --global alias.pl "pull"
    
  6. Pushing Changes: When you’re ready to send your local branch commits to the remote repository, you’d use git push. An alias can make it git ph.

    To create this alias, enter:

    1
    
    git config --global alias.ph "push"
    
  7. Force Pushing Changes: Sometimes you need to overwrite the remote repository with your local changes using git push --force. This is risky but can be aliased to git phf.

    Here’s how to alias it:

    1
    
    git config --global alias.phf "push --force"
    
  8. Stylized Log: Looking at the log of changes can be made easier and more informative with git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit. An alias might be git lg.

    Set this alias with:

    1
    
    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    

    I especially like the log alias, which produces comprehensive output:

    Sample git lg output

By setting up aliases, you can customize your Git experience to suit your workflow. To create an alias, you use the git config --global command followed by alias.<your-alias> and then the Git command you want to shorten. The --global flag means that the alias will be available across all your Git projects. If you want an alias to be specific to a single project, omit the --global flag and run the command within the project’s directory.

Remember, while git push --force (aliased to git phf) is powerful, it should be used with caution as it can overwrite changes in your remote repository.

Get creative and build a suite of aliases that fits your coding style!

I configured my git aliases as following:

AliasMeaning
git stgit status
git cm "<message>"git commit -m "<message>"
git co <name>git checkout <name>
git cob <name>git checkout -b <name>
git plgit pull
git phgit push
git phfgit push --force
git lggit log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --


➡️ Embracing the Interface Segregation Principle in your codebase


⬅️ C containers library


Go back to Posts.