DEV Community

Bulat
Bulat

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
```



You can write your own format, all variables described at [documentation](https://git-scm.com/docs/git-log#_pretty_formats)

## 2. `git cs ${COMMIT_HASH}`

Find branches that contains specific commit
![Screenshot_20210915_110120](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz5kohmfvamiqzny4sor.png)

Alias for this command:


Enter fullscreen mode Exit fullscreen mode

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




## 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:


Enter fullscreen mode Exit fullscreen mode

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




## 4. `git cleanup`

Removes all branches that already merged.

Alias for this command:


Enter fullscreen mode Exit fullscreen mode

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




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)


Enter fullscreen mode Exit fullscreen mode

[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"




Maybe you are using something interesting too?
Enter fullscreen mode Exit fullscreen mode

Top comments (0)