Using Aliases to Speed Up Your Git Workflow

Robert Cooper - Sep 3 '18 - - Dev Community

Leveraging aliases when working with git can help make you more efficient with using the popular version control system. Listed below are some of my absolute favourite and most used aliases when working on a project that uses git.

It should be noted that when i'm talking about aliases, I mean bash/zsh aliases and not git aliases.

What's the difference? Bash/Zsh aliases are setup in a .bashrc or .zshrc file and allows you to assign a command to a shorthand version. For example, you could alias git log to be gl, which would save you a few characters of typing. Git aliases allow you to similarly assign a git command to a shorthand version, but you still have to type git prior to the shorthand. For example, you could alias the git log command to be l and then you would invoke the command by typing git l. All git aliases are set in a .gitconfig file which is usually found in a computer's HOME directory.


Adding and Amending Commits

gaa

git add -A
Enter fullscreen mode Exit fullscreen mode

Adds all changes to staging.

gcm

git commit -m
Enter fullscreen mode Exit fullscreen mode

Creates a new commit with all staged files and uses the given message as the commit's message.

Example:

gcm "This is my commit message describing the changes"
Enter fullscreen mode Exit fullscreen mode

gcma

git commit -a -m
Enter fullscreen mode Exit fullscreen mode

Adds all files to staging and makes a commit using the given message as the commit's message.

Example:

gcam "This is another commit message!"
Enter fullscreen mode Exit fullscreen mode

gca

git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Moves the currently saved files into the previous commit message. The --no-edit flag is passed to git commit --amend to keep the previous commit message (this is usually the case).

If the previous commit message should be changed, I've aliased git commit --amend --no-edit to gcae. It does the same as gca, but it opens up the git editor to edit the previous commit's message. In my case, I've setup VS Code to be my default git editor since i'm not proficient with VIM.

Previous commit message displayed in VS Code with instructions on how to change the commit message.

gcaa

git add --all && git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

This command is useful to use if some changes that have been made should be a part of the previous commit. It will add all newly modified files to staging and then it will amend the previous commit with those changes.

gnope

git checkout .
Enter fullscreen mode Exit fullscreen mode

Removes all the changes detected by Git.

gwait

git reset HEAD
Enter fullscreen mode Exit fullscreen mode

Unstages everything.

gundo

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

Undoes the last commit and moves the files in the commit to staging.

Reading history

gl

git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

Logs previous commits in a concise manner. The argument passed to --pretty is defining the information that should get displayed for each commit (i.e. commit hash, branch name, commit message, date of commit, and author). Definitely wouldn't want to type this out every time!

gco

git checkout
Enter fullscreen mode Exit fullscreen mode

Allows to switch between branches.

Example:

gco other-branch-name
Enter fullscreen mode Exit fullscreen mode

Pushing & Pulling To/From Remote

gps

git push
Enter fullscreen mode Exit fullscreen mode

Updates the remote.

gpsf

git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

Overrides the remote branch with the local branch if no one else has commited to the remote. It is considered to be a safer approach than using git push --force.

gpl

git pull --rebase
Enter fullscreen mode Exit fullscreen mode

Fetch updates from the remote and rebase the local branch with the upstream branch. This avoids any merge commits that may occur when using git pull.

Rebasing

grb

git rebase
Enter fullscreen mode Exit fullscreen mode

Rebases the current branch with another branch.

Example:

grb origin/master
Enter fullscreen mode Exit fullscreen mode

grn (shell command)

grn() { git rebase -i HEAD~"$1"; }
Enter fullscreen mode Exit fullscreen mode

This is actually a shell command which allows for variables to be passed in as arguments. The $1 is a placeholder for an argument that gets passed to the grn function. This function accepts N as an argument, where N is the number of commits to perform an interactive rebase on.

Example:

Example usage of the grn shell command

grbic (shell command)

grbic() { git rebase -i "$1" ; }
Enter fullscreen mode Exit fullscreen mode

Accepts a commit hash as an argument and performs an interactive rebase back to the passed commit hash.

Example:

Example usage of grbic shell command

grba

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

Aborts an interactive rebase and restores the git state to the moment the git rebase command was run.

grbc

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Continues an interactive rebase after conflicts on a commit have been resolved.


Ok, I know that was a lot of aliases, so sorry for rambling on about them. I would encourage others to start aliases more often to speed up their development. Let me know if any of those were helpful or if you've got some favourite aliases you'd like to share.


If you found this article consider following me on Twitter, Github, or LinkedIn.

. . . . . . . . . .
Terabox Video Player