git - useful aliases

10/09/2021
git

Aliases - What are those

git aliases are just short hand terms for longer commands. For example you can define an aliases which does pull with rebase as strategy: git pure instead of git pull --rebase.

How to defined Aliases

The general pattern looks the following:

$ git config --<scope> alias.<aliasname> <command>

Okay let's go through this real quick:

scope: There are 3 levels. Your repository, your user or your machine. You can define aliases on all of those 3 levels. If you define an aliases machine-wide then every user in every repository can use it.

Scope Description
local This will only effect your current repository you are in. git config will basically put it's stuff into .git/config. So if you define an alias on a local scope, you are not able to use the alias somewhere else. That can make sense if they are highly specialized to your repository.
global This is the scope of your current (logged in) user. If you define an alias for the global scope you will have access everywhere in every repo when you are logged in. In Windows you will this under %USERPROFILE%/.gitconfig (e.g. C:/Users/your_user/.gitconfig and under ~/.gitconfig in UNIX based systems. Other users on the system will not have access to your aliases saved in there.
system This is the highest scope: machine-wide. Aliases defined here are valid for all users. On Windows it can be found under: C:\ProgramData\Git\config and under /etc/gitconfig in UNIX based systems.

Note: local overrides global and global overrides system.

aliasname: Your new name for the command. See below for some examples. command: The original command

An example would be:

git config --global alias.pure "pull --rebase"

We introduced the command pure which basically does a pull with rebase instead of merge. You can use the command like this: git pure. We defined it in the global scope. That means I will have access throughout all my repositories. This command will add an entry to your gitconfig

...
[alias]
	pure = pull --rebase

So if you want you can also extend just the git config

Overview of my most used aliases

Coming what you are looking for. Here is a list of my most used git aliases:

Alias Command Description Usage
pure pull --rebase Does a pull with rebase instead of a merge. For me useful when working on shared branches. git pure
co checkout Does just a checkout. You can also use switch. git co feature/my-featurebranch
cm commit -am Includes all currently changed files in this commit and creates a commit with the given message. git cm "My Message"
cma commit --amend --no-edit This will amend the current commit to the last one and use the commit message of the last commit. Useful if you missed something (like a typo) and don't wanna have a new commit. DANGER: You are rewriting the history. Don't do this on public / shared branches. Your co-workers will hate you for that! If so please always use git push --force-with-lease so you do not overwrite your co-workers stuff unintentionally. If you want to know more about that: Here you go: git git cma
pushu push -u origin HEAD This pushes your branch to origin. This is especially helpful after you locally created a new branch which does not exist on remote. git pushu
recent ! git reflog | grep -Po "checkout: moving from .to\s(\K.)" | awk '!x[$0]++' | head -5 This one is a bit special. Basically it prints you out the last 5 branches you worked on. Sometimes I have to change my branches quite often and forgot where I was. git recent
1
An error has occurred. This application may no longer respond until reloaded. Reload x