Git alias tricks

Productivity RStudio

Using a git alias to commit and stage all files and other fancy tricks.

Seb Dalgarno https://github.com/sebdalgarno
04-14-2021

TL;DR type git config --global alias.coa "!git add -A && git commit -m" in the command line. From now on, use git coa 'message here' to commit and stage all files!

Overview

For years I have staged, committed and pushed changes with the RStudio Git interface.

It works! But I’ve realized that it’s much more efficient (and not hard!) to do from the command line. Check out this sweet resource by Jenny Bryan for more info on git with r.

Git from the command line

This solution on stackoverflow suggests staging, committing and pushing all files by running the following in the command line:

git add -A && git commit -m "rebuild site"
git push

Git alias

Real productivity gains are made by using git aliases, which remove the need to type (and remember) full git commands. Let’s start with a simple example. To set up the alias git p for git push, we run in the command line:

git config --global alias.p "push"

Going back to our original use case, we can set up the alias git coa:

git config --global alias.coa "!git add -A && git commit -m"

From now on, we can stage, commit and push all changes with two simple commands:

git coa 'rebuild site'
git p

Here are some more useful aliases:

Create and checkout branch

git config --global alias.bc "checkout -b"
git bc some_new_branch

Delete branch locally and remotely (from here)

git config --global alias.bd '!sh -c "git branch -D $1 && git push origin :$1" -'

git bd some_new_branch

Keyboard shortcut to Terminal

To completely remove the need to use the mouse, I’ve set up a customized keyboard shortcut1 Cmd-3 to move the cursor to the RStudio Terminal. Check out this great tutorial on customizing keyboard shortcuts in RStudio. The advantage of using the RStudio terminal and not the MacOS Terminal app (or iterm2)2 is that it is automatically drilled down into the directory of your project.

🎉🎉🎉


  1. I also use Cmd-1 to move cursor to script and Cmd-2 to move cursor to console.↩︎

  2. These tips are for Mac users. I’m not sure how to do the above in Windows.↩︎

Citation

For attribution, please cite this work as

Dalgarno (2021, April 14). Fishy Data Blog: Git alias tricks. Retrieved from https://fishydata.netlify.app/posts/2021-04-14-git-alias/

BibTeX citation

@misc{dalgarno2021git,
  author = {Dalgarno, Seb},
  title = {Fishy Data Blog: Git alias tricks},
  url = {https://fishydata.netlify.app/posts/2021-04-14-git-alias/},
  year = {2021}
}