DEV Community

Kein
Kein

Posted on

Git aliases that I use weekly

I know that there is a lot of posts like "My top bash commands", "My favorite git commands".
But this morning I said to myself: "Come one! You know nice tricks too! Share them!".
So this aliases I'm using a lot of time because I prefer to work with git by CLI. I will not repeat common and simple aliases like commit -m.

1. git h

Git history. A picture is worth a thousand words
Screenshot_20210915_110054

Alias for this command:

h = log --pretty='format:%Cred%h%Creset %C(yellow)%d%Creset - %s %C(green)%ar%Creset %C(blue)%an%Creset' --graph --all --decorate
Enter fullscreen mode Exit fullscreen mode

You can write your own format, all variables described at documentation

2. git cs ${COMMIT_HASH}

Find branches that contains specific commit
Screenshot_20210915_110120

Alias for this command:

cs = "!f() { git branch -r --contains $1; }; f"
Enter fullscreen mode Exit fullscreen mode

3. git bd ${BRANCH_1}..${BRANCH_2}

Difference between branches. Output same as for git h, but shows only commits between this branches pointers.

Alias for this command:

bd = "!f() { git log --graph --pretty=format:\"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset\" --abbrev-commit --date=relative $1; }; f"
Enter fullscreen mode Exit fullscreen mode

4. git cleanup

Removes all branches that already merged.

Alias for this command:

cleanup = "!git branch  --no-color --merged | /usr/bin/grep --color=none  -v '\\*\\|master\\|develop' | xargs -n 1 git branch -d"
Enter fullscreen mode Exit fullscreen mode

As you can see I've hardcoded branches master and develop, but you can have different names for branches.

How to add this aliases

You can add those commands at git config(for global aliases ~/.gitconfig or .git/config for project specific)

[alias]
    h = log --pretty='format:%Cred%h%Creset %C(yellow)%d%Creset - %s %C(green)%ar%Creset %C(blue)%an%Creset' --graph --all --decorate
    cs = "!f() { git branch -r --contains $1; }; f"
    bd = "!f() { git log --graph --pretty=format:\"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset\" --abbrev-commit --date=relative $1; }; f"
    cleanup = "!git branch  --no-color --merged | /usr/bin/grep --color=none  -v '\\*\\|master\\|develop' | xargs -n 1 git branch -d"
Enter fullscreen mode Exit fullscreen mode

Maybe you are using something interesting too?

Top comments (0)