Skip to content

Instantly share code, notes, and snippets.

@neonexus
Last active November 13, 2018 04:46
Show Gist options
  • Save neonexus/5139285 to your computer and use it in GitHub Desktop.
Save neonexus/5139285 to your computer and use it in GitHub Desktop.
Git shortcuts

Git Shortcuts

What do they do?

The following blob is a set of basic setup configuration for Git, along with some shortcuts that I personally use.

Here we assume that you are working on your own, local branch (basically, not the master).

The shortcuts and what they do

git refresh [branch on origin]

This will update the branch you provide (will default to "master" if nothing is provided) from the tracking repo (aka the Git server, or Github), then update the current working branch.

Here's a list of the actual commands it runs:

  • git checkout [branch | master]
  • git remote update
  • git pull origin [branch | master]
  • git checkout {{the branch you were on}}
  • git rebase [branch | master]

git deploy [branch on origin]

After doing a git refresh (found above), will merge the current branch into the branch provided (defaults to master if none provided), push to the tracking server (aka Github), and switch back to the original working branch.

This is what it's really doing:

  • git checkout [branch | master]
  • git remote update
  • git pull origin [branch | master]
  • git checkout {{the branch you were on}}
  • git rebase [branch | master]
  • git checkout [branch | master]
  • git merge {{the branch you were on}}
  • git push origin [branch | master]
  • git checkout {{the branch you were on}}

git new {{new branch}} [branch on origin]

This will create a new branch, based on the branch from the origin provided (defaults to master if left blank).

This is the commands it's running:

  • git checkout [branch|master]
  • git remote update
  • git pull origin [branch|master]
  • git branch {{new branch}}
  • git checkout {{new branch}}

git restore

A quick shorcut for git reset --hard.

git goback

A simple shortcut for git reset --hard HEAD~1.

git commits

This will spit out a fairly nice, formatted view of the commit history of the current branch.

git nuke

This will perform a "nuke and pave" on the current branch. Essentially, it clears away the current local branch, un-pushed commits and all, and resets it to the origin HEAD for said branch.

Commands it runs:

  • git checkout master (have to be on some branch)
  • git branch -D {BRANCH_COMMAND_RAN_ON}
  • git checkout {BRANCH_COMMAND_RAN_ON}

Additional things the blob adds

  • Sets nano as the primary editor (in-place of the usual vi).

    remove the [core] block if you don't want this

  • Sets opendiff (requires Xcode) as the git mergetool.

    remove the [merge] block if you don't want this

  • Makes it so the current working branch is what gets pushed if not defined (git push)

    remove the [push] block if you don't want this

How do I install this magic?

Copy the blob (found below), and paste it into the ~/.gitconfig file.

The blob

[alias]
	refresh = "!f() { current_branch=$(git symbolic-ref --short -q HEAD) || current_branch='(unnamed branch)'; current_branch=${current_branch##refs/heads/}; if [ -z $1 ] ; then stream='master' ; else export stream=$1 ; fi ; git checkout $stream ; git remote update ; git pull origin $stream ; git checkout $current_branch ; git rebase $stream ; } ; f"
	deploy = "!f () { current_branch=$(git symbolic-ref --short -q HEAD) || current_branch='(unnamed branch)'; current_branch=${current_branch##refs/heads/}; if [ -z $1 ] ; then stream='master' ; else export stream=$1 ; fi ; git refresh $stream ; git checkout $stream ; git merge $current_branch ; git push origin $stream ; git checkout $current_branch ; } ; f"
	restore = reset --hard
	goback = reset --hard HEAD~1
	commits = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
	new = "!f () { if [ -z $1 ] ; then echo 'git new requires a branch name!' ; else if [ -z $2 ] ; then stream='master' ; else stream=$2 ; fi ; git checkout $stream ; git remote update ; git pull origin $stream ; git branch $1 ; git checkout $1 ; fi ; } ; f"
	nuke = "!f () { if [ -z $1 ] || [ $1 != 'YES' ] ; then echo 'You must be SURE you want to do this! (git nuke YES)' ; else current_branch=$(git symbolic-ref --short -q HEAD) || current_branch='(unnamed branch)'; current_branch=${current_branch##refs/heads/}; master='master' ; git checkout $master; git branch -D $current_branch; git checkout $current_branch  ; fi ; } ; f"
[core]
	editor = nano
[merge]
	tool = opendiff
[push]
	default = current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment