Skip to content

Instantly share code, notes, and snippets.

@QNimbus
Last active December 8, 2024 03:19
Show Gist options
  • Save QNimbus/7c2aec2fbaaf67823223380d9f794be2 to your computer and use it in GitHub Desktop.
Save QNimbus/7c2aec2fbaaf67823223380d9f794be2 to your computer and use it in GitHub Desktop.
Cheat sheets #git #jq #cheatsheet

Git cheat sheet

Useful links

GitHub Git Cheat Sheet

Common commands

General

Create repository:

git init

Add file:

git add <file>

Remove file:

git rm <file>

Move or rename file:

git mv <from> <to>

Commit changes:

git commit

Show changes:

git status

Show log:

git log

Show log with tags:

git log --decorate

Search thru commit messages:

git log --grep="<search>"

Add remote repository:

git remote add origin <url>

Branches

Show branches:

git branch

Create branch:

git branch <branch>

Create and checkout branch:

git checkout -b <branch>

Checkout branch:

git checkout <branch>

Rename branch:

git branch -m <from> <to>

Delete branch:

git branch -d <branch>

Delete remote branch:

git push origin :<branch>

Review branch changes:

git diff <branch>

Merge branch into current:

git merge <branch>

Resolve merge conflicts:

mate <file>
git add <file>
git commit

Discard branch changes:

git checkout -f master

Tags

Show tags:

git tag

Create tag:

git tag -a <tag>

Create tag for specific commit:

git tag -a <tag> <commit>

Show tag data:

git show <tag>

Delete tag:

git tag -d <tag>

Delete remote tag:

git push origin :refs/tags/<tag>

Push

Push to master:

git push origin master

Push with tags:

git push origin master --tags

Pull

Fetch from remote repository:

git fetch origin

Merge remote branch into current:

git merge origin/master

Fetch and merge into current branch:

git pull

Clone

Clone repository:

git clone <url>

Clone with submodules:

git clone --recursive <url>

Submodules

Add submodule to repository:

git submodule add <url>

Update submodule:

git submodule update

Stash

Stash changes:

git stash

Show stashes:

git stash list

Restore stash:

git stash apply

Restore stash and restage files:

git stash apply --index

Restore specific stash:

git stash apply <stash>

Remove stash:

git stash drop <stash>

Restore and remove stash:

git stash pop

Create branch from stash:

git stash branch <branch>

Special

Remove last commit (not pushed):

git reset --hard HEAD~1

Misc

Get the number of commits in the current branch:

git log --pretty=oneline | wc -l

Configuration

Set name:

git config --global user.name "<name>"

Set email:

git config --global user.email "<email>"

Set editor (e.g. TextMate):

git config --global core.editor "mate -w"

Use colors:

git config --global color.ui true

Processing JSON using jq

jq is useful to slice, filter, map and transform structured json data.

Installing jq

On Mac OS

brew install jq

On AWS Linux

Not available as yum install on our current AMI. It should be on the latest AMI though: https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/

Installing from the source proved to be tricky.

Useful arguments

When running jq, the following arguments may become handy:

Argument Description
--version Output the jq version and exit with zero.
--sort-keys Output the fields of each object with the keys in sorted order.

Basic concepts

The syntax for jq is pretty coherent:

Syntax Description
, Filters separated by a comma will produce multiple independent outputs
? Will ignores error if the type is unexpected
[] Array construction
{} Object construction
+ Concatenate or Add
- Difference of sets or Substract
length Size of selected element
| Pipes are used to chain commands in a similar fashion than bash

Dealing with json objects

Description Command
Display all keys jq 'keys'
Adds + 1 to all items jq 'map_values(.+1)'
Delete a key jq 'del(.foo)'
Convert an object to array to_entries &#124; map([.key, .value])

Dealing with fields

Description Command
Concatenate two fields fieldNew=.field1+' '+.field2

Dealing with json arrays

Slicing and Filtering

Description Command
All jq .[]
First jq '.[0]'
Range jq '.[2:4]'
First 3 jq '.[:3]'
Last 2 jq '.[-2:]'
Before Last jq '.[-2]'
Select array of int by value jq 'map(select(. >= 2))'
Select array of objects by value ** jq '.[] | select(.id == "second")'**
Select by type ** jq '.[] | numbers' ** with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars

Mapping and Transforming

Description Command
Add + 1 to all items jq 'map(.+1)'
Delete 2 items jq 'del(.[1, 2])'
Concatenate arrays jq 'add'
Flatten an array jq 'flatten'
Create a range of numbers jq '[range(2;4)]'
Display the type of each item jq 'map(type)'
Sort an array of basic type jq 'sort'
Sort an array of objects jq 'sort_by(.foo)'
Group by a key - opposite to flatten jq 'group_by(.foo)'
Minimun value of an array jq 'min' .See also min, max, min_by(path_exp), max_by(path_exp)
Remove duplicates jq 'unique' or jq 'unique_by(.foo)' or jq 'unique_by(length)'
Reverse an array jq 'reverse'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment