DEV Community

Cover image for ZSH cheatsheet for git plugin
Camilo Martinez
Camilo Martinez

Posted on • Updated on

ZSH cheatsheet for git plugin

Git is one of the most used plugins in oh-my-zsh for adding quick and short aliases.

There are a lot of them and can be overwhelming and they can be listed using the alias | grep git command. But day to day we only are going to use a shortlist of them.

That's why I created a short version cheatsheet with a graph interpretation for better comprehension.

cheatsheet

Basics

Alias Command Description
g init git init Initialize a local Git repository
g clone <repo> [<path>] git clone <repo> [<path>] Create a local copy of a remote repository

But sincerely, I prefer to use my own take <repo-url> command for cloning.

Snappshoting

Alias Command Description
gst git status Check status
gaa git add --all Add all new and changed files to the staging area
grh git reset Removes all files from staging area
ga <file> git add <file> Add a file to the staging area
gru <file> git reset -- <file> Remove a file from staging area
gcmsg "<message>" git commit -m "<message>" Commit changes with message description
gcam git commit -a -m "<message>" Add all new files and commit changes with message description.

Stash

Alias Command Description
gsta -m <name> git stash push -m <name> Create stash with name
gstall -m <name> git stash --all -m <name> Create stash with all files, including untracked and ignored files with name
gstl git stash list List down all your stashes
gstaa stash@{n} git stash apply stash@{n} To apply a stash and keep it in the stash stack
gstp stash@{n} git stash pop stash@{n} To apply a stash and remove it from the stash stack
gstd git stash drop Remove a single stash entry from the list of stash entries
gstc git stash clear Remove all stashed entries

Branching

Alias Command Description
gb git branch List branches (the asterisk denotes the current branch)
gba git branch -a List all branches (local and remote)
gb <branch-name> git branch <branch-name> Create new branch
gbd <branch-name> git branch -d <branch-name> Delete a branch
gcb <branch-name> git checkout -b <branch-name> Create a new branch and switch to it
gco <branch-name> git checkout <branch-name> Switch to a branch
gco - git checkout - Switch to previous branch

Remote

Alias Command Description
gra origin <path> git remote add origin <path> Add a remote repository
grset origin <path> git remote set-url origin <path> Set remote repository
gf git fetch Gets status of 'origin'. Does not change your working directory or local repository
gf <repo> <branch-name> git fetch <repo> <branch-name> Get status of remote on
gfa git fetch --all --prune Fetch all remote branches, delete branch if upstream is gone
gl git pull Incorporates changes from 'origin' into local repo
gl <repo> <branch-name> git pull <repo> <branch-name> Incorporates changes from remote on into local repo
gp git push Incorporates changes from local repo into 'origin'
gpsup git push --set-upstream origin <currentbranch> Set upstream current branch
gp <repo> <branch-name> git push <repo> <branch> Incorporates changes from local repo into remote on
gp -d <remote> <branch> git push -d <remote> <branch> Delete remote branch

Merging

Alias Command Description
gd <source-branch> <target-branch> git diff <source-branch> <target-branch> Preview changes before merging
gm <branch-name> git merge <branch-name> Merge a branch into the active branch
gm <source-branch> <target-branch> git merge <source-branch> <target-branch> Merge a branch into a target branch
gma git merge --abort Cancel the whole merge process
glog git log --oneline --decorate --graph View changes

Oh Shit!

Alias Command Description
gc! git commit -v --amend I need to change the message on my last commit
gcn! git commit -v --no-edit --amend I committed and immediately realized I need to make one small change.

⚠ You should never amend commits that have been pushed up to a public/shared branch!. Amend commits that only exists in your local copy or you're gonna have a bad time.


Sources:


That’s All Folks!
Happy Coding 🖖

beer

Top comments (5)

Collapse
 
moopet profile image
Ben Sinclair

You're talking about plugins for oh-my-zsh, right, not for zsh itself?

I honestly find all these aliases way harder to remember than the commands themselves.

Collapse
 
sathishkannan162 profile image
Sathish Kannan

Believe me. It will save lots of time when you have to work with others in a shared repository. You would have to use lots of git commands. git log --oneline is my most used command, using glog will save lots of time.

Collapse
 
moopet profile image
Ben Sinclair

I work with others in shared repositories every day, though? For the example you gave, I have my git log already set up with a format I like in its central configuration file.

I just ran a test. Without rushing, I timed how long it took me to type glog<cr> 10 times, compared to git log<cr> and the results were 12s vs 17 seconds. I remain underwhelmed :)

Collapse
 
alexanderkjeldaas profile image
Alexander Kjeldaas

^Rlog?

Collapse
 
equiman profile image
Camilo Martinez

Yeah right! for oh-my-zsh. I've updated the information to be more specific.

I used to have the same problem once I started using it I forgot the long command way. The problem is when I use another computer without the plugin 😅. That's why I also added the complete command.