Cool git aliases that I find useful in daily life
GitHub Repo : https://github.com/Magnus167/git-aliases
.gitconfig
[alias]
pmerge = "!f() { BRANCH=$(git branch --show-current); git checkout $1 && git pull && git checkout $BRANCH && git merge $1; }; f"
staash = stash --all
branchc = branch --show-current
clonegh = "!f() { git clone https://github.com/$1.git; }; f"
co = checkout
cop = "!f() { git checkout $1 && git pull; }; f"
cob = checkout -b
branchr = branch --sort=-committerdate
logp = log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30
logl = log --oneline
graph = log --oneline --graph --decorate
prunegone = !git fetch --prune && git branch -vv | awk '/: gone]/{print $1}' | xargs -r git branch -d
fame = !python -m gitfame
github = "!f() { URL=$(git remote get-url origin); python -m webbrowser -t $URL; }; f"
pr = "!f() { BRANCH=$(git branch --show-current); REPOURL=$(git config --get remote.origin.url); python ~/git-aliases/python/getpr.py $REPOURL $BRANCH; }; f"
co-pr = "!f() { REPOURL=$(git config --get remote.origin.url); BRANCH=$(python ~/git-aliases/python/checkoutpr.py $REPOURL $1); git checkout $BRANCH; git pull; }; f"
See this script for the pr
/getpr.py
workflow: get_pr_full.py
Index
branchc - show the current branch
Usage:
git branchc
Intended use:
Shorten the command to show the current branch.
Script:
git config --global alias.branchc 'branch --show-current'
clonegh - clone directly from GitHub
git clonegh <username>/<repo>
Clone a repository directly from GitHub, without having to copy the URL and then clone it
Script:
## Usage:
## Intended use:
git config --global alias.clonegh '!f() { git clone https://github.com/$1.git; }; f'
github - go to the repository’s home page (GitHub)
Usage:
git github
Output: Opens the repository’s home page in the default browser
Intended use:
Quickly open the repository’s home page in the default browser NOTE: Requires Python to be installed
Script:
git config --global alias.github '!f() { URL=$(git remote get-url origin); python -m webbrowser -t $URL; }; f'
graph - Show a pretty git log with graph
Usage:
git graph
Output:
* 1aa688f (HEAD -> main, origin/main, origin/add_branchr, origin/HEAD, add_branchr) added branchr w comment
* 13e46a5 (origin/add_graph, add_graph) added graph.sh
* 0d2deab added logp
* 8349514 added logl - log with one line
* cb11940 added description
* 939c655 appended description
* 5f4efe9 Create branchc.sh
* 5279dd5 Create pmerge.sh
* 8f83cd7 Create README.md
Intended use:
Shorten the command to show the git log with graph
Script:
git config --global alias.graph 'log --oneline --graph --decorate'
logl - log with one line
Usage:
git logl
Output:
cb11940 (HEAD -> main) added description
939c655 appended description
5f4efe9 (origin/main, origin/HEAD) Create branchc.sh
5279dd5 Create pmerge.sh
8f83cd7 Create README.md
Intended use:
Shorten the command to show the git log with one line per commit.
Script:
git config --global alias.logl 'log --oneline'
logp - pretty log
Usage:
git logp
Output:
8349514 - (HEAD -> main, origin/main, origin/HEAD) added logl - log with one line (2 minutes ago) [Palash Tyagi]
cb11940 - added description (6 minutes ago) [Palash Tyagi]
939c655 - appended description (6 minutes ago) [Palash Tyagi]
5f4efe9 - Create branchc.sh (18 hours ago) [Palash Tyagi]
5279dd5 - Create pmerge.sh (18 hours ago) [Palash Tyagi]
8f83cd7 - Create README.md (18 hours ago) [Palash Tyagi]
Intended use:
Shorten the command to show the git log with a pretty format.
Script:
git config --global alias.logp !git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30
pmerge - pull and merge a branch
Usage:
git checkout featureX
git pmerge main
Intended use
When merging branch main
into featureX
,
one often has to pull from main
to ensure
merging into the correct head.
Assume you’re on branch featureX
git pmerge main
executes:
BRANCH=$(git branch --show-current) # BRANCH is featureX
git checkout main
git pull
git checkout BRANCH
git merge main
Script:
git config --global alias.pmerge '!f() { BRANCH=$(git branch --show-current); git checkout "$1" && git pull && git checkout "$BRANCH" && git merge "$1"; }; f'
prunegone
Usage:
git prunegone
Intended use
Use this alias to delete all branches that are gone on the remote. It does not delete branches that are not gone on the remote, or are not on the remote at all.
Running this alias executes:
git fetch --prune
git branch -vv | awk "/: gone]/{print \$1}" | xargs -r git branch -d
Script:
git config --global alias.prunegone '!git fetch --prune && git branch -vv | awk "/: gone]/{print \$1}" | xargs -r git branch -d'