DEV Community

Cover image for Git shortcuts (alias)
CSJcode
CSJcode

Posted on

Git shortcuts (alias)

Shortcuts and aliases I was compiling as I was setting up a new laptop, and thought I'd share them.

Lets get situated with where we are starting

$ cat ~/.gitconfig

[user]
        name = Foo Bar
        email = example@example.com
[init]
        defaultBranch = main
[alias]
        co = checkout
        br = branch
        ci = commit
Enter fullscreen mode Exit fullscreen mode

Git Shortcuts: (1) Git alias or (2) Git+Shell alias

  • Make git shortcuts in a git alias - pattern (note: --global is normally used for a dev's user account, but you could use --system for all accounts, --local for the local repo only)

$ git config --global alias.<alias> <commands>

  • Make git shortcuts in shell (bash/zsh etc.) alias - pattern

$ alias <alias>='<commands>'

Example: use shortcut "gl" to list git user aliases, including adding, listing and removing the alias.

$ git config --global alias.gl 'config --global -l'

$ git gl

[...]
init.defaultbranch=main
alias.co=checkout
alias.br=branch
alias.ci=commit
[...]

$ alias gconf='git gl'

$ gconf

[...]
init.defaultbranch=main
alias.co=checkout
alias.br=branch
alias.ci=commit
[...]

$ alias -p

alias gconf='git gl'

$ unalias gconf

$ gconf
bash: gconf: command not found

$ alias -p
Enter fullscreen mode Exit fullscreen mode

Use could also use this to see settings:

git config --list

file:/Users/foobar/.gitconfig  alias.com=checkout main
file:/Users/foobar/.gitconfig  alias.p=push
file:.git/config        core.repositoryformatversion=0
file:.git/config        core.filemode=true
file:.git/config        core.bare=false
file:.git/config        core.logallrefupdates=tru
Enter fullscreen mode Exit fullscreen mode

$ git config user.email

example@example.com
Enter fullscreen mode Exit fullscreen mode

$ git config user.name

Foo bar
Enter fullscreen mode Exit fullscreen mode

$ git config --global user.email "myname@gmail.com"

$ git config --global user.name "firstname lastname"

Basics

$ git config --global alias.co checkout

$ git config --global alias.com "checkout main"

$ git config --global alias.br branch

$ git config --global alias.a add

$ git config --global alias.cm 'commit -m'

$ git config --global alias.p push

$ git config --global alias.mr merge

$ git config --global alias.rb rebase

$ git config --global alias.rbc rebase --continue

$ git config --global alias.st status

$  git st

On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        git-aliases.md

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

Git Reset

Important: do not use this config unless you are sure you know what you are doing, it could result in losing code permanently if you misuse this or make a mistake.

Use a test repo first, until you are sure about what is going on.

see: What's the difference between git reset --mixed, --soft, and --hard? https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard

As stated in the link above:

--soft: uncommit changes, changes are left staged (index).
--mixed (default): uncommit + unstage changes, changes are left in working tree.
--hard: uncommit + unstage + delete changes, nothing left.
Enter fullscreen mode Exit fullscreen mode

Undo last local commmit, to HEAD (uncommit, keeps in stage)

$ git config --global alias.rs reset --soft HEAD~1

Undo last local commmit, to HEAD (uncommit unstage)

$ git config --global alias.rmx reset --mixed HEAD~1

Undo local edits to HEAD (DANGER, permanent, caution)

$ git config --global alias.rh reset --hard HEAD~1

List of all commits - summary

$ git config --global alias.l1 'log --oneline'

61eee34 (HEAD -> main, origin/main, origin/2-first-app-counter, 2-first-app-counter) Counter app
e9a23b9 (2-first-app) Hello world
bcc8bf2 Start fresh
abf9f0a (origin/1-hello-world, 1-hello-world) Basic hello world
Enter fullscreen mode Exit fullscreen mode

Last commit

$ git config --global alias.last 'log -1 HEAD --stat'

git last

Author: Foo bar <example@example.com>
Date:   Fri Dec 31 14:54:47 2021 -0800

    Counter app example

 counter.sol | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
Enter fullscreen mode Exit fullscreen mode

Remote comfigured repos

$ git config --global alias.rv 'remote -v'

$ git rv

origin  https://github.com/foobar/Hello-World.git (fetch)
origin  https://github.com/foobar/Hello-World.git (push)
Enter fullscreen mode Exit fullscreen mode

Search for specific strings (put string after -F)

$ git config --global alias.se '!git rev-list --all | xargs git grep -F'

Cherry-pick

more info: https://www.atlassian.com/git/tutorials/cherry-pick

$ git config --global alias.ch cherry-pick

Change the commit message:

git config --global commit.template ~/.gitmessage.txt

Discussion (2)

Collapse
pranaysama profile image
Pranay Velisoju

My all time favourite alias
git adog for
git log --all --decorate --oneline --graph

Collapse
markus1607 profile image
Mark Sarpong

Awesome, just what I needed. Thanks for sharing