Last active
September 22, 2025 15:37
-
-
Save aasmundeldhuset/b698801c1423a4f7188d4ceda03cf1f2 to your computer and use it in GitHub Desktop.
Git and Bash aliases for Cognite DevCon 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Store this in your ~/.bashrc file. Note that a .bashrc file is really just a regular shellscript, | |
| # but it must be executed _inside_ the current shell instead of as a subprocess in order for its | |
| # definitions to apply to the current shell. Alternatively, you can apply this file from within | |
| # your ~/.bashrc (or interactively in a shell) with `source /path/to/file.bashrc` | |
| # or `. /path/to/file.bashrc`. | |
| # Basic aliases that simply expand to the given command (which, in the case of git, | |
| # may involve git aliases). Note that you may append any parameters/flags when invoking the alias, | |
| # which will be appended to the underlying command (and if that involves a git alias, | |
| # the underlying git command will also see the extra parameters). | |
| alias gs="git st" | |
| alias gsh="git show" | |
| alias gshn="git show --name-status" | |
| alias gd="git d" | |
| alias gdc="git dc" | |
| alias gdH="git dH" | |
| alias gc="git commit" | |
| alias gcm="git commit -m" | |
| alias gca="git commit --amend" | |
| alias gcam="git commit --amend -m" | |
| alias gg="git go" | |
| alias gr="git r" | |
| # Bash syntax may be used. This edits your .bashrc file and then applies it to this shell. | |
| # Note that: | |
| # - && runs the right command only if the left command succeeds (returns zero) | |
| # - || runs the right command only if the left command fails (returns nonzero) | |
| # - ; runs the right command either way | |
| alias bashrc="vim ~/.bashrc && . ~/.bashrc" | |
| # Got a directory with a collection of utility scripts? Add it to `PATH` in order to be able | |
| # to run `foo.sh` instead of `~/scripts/foo.sh`. | |
| export PATH="$PATH:$HOME/scripts" | |
| # Create a directory and cd into it. | |
| function mkcd() { | |
| mkdir "$1" | |
| cd "$1" | |
| } | |
| # Edit a (potentially new) file and make it executable. | |
| function vimex() { | |
| vim "$1" | |
| chmod u+x "$1" | |
| } | |
| # Go up the given number of directory levels. If you have run this several times, | |
| # you can cycle back through the previous directories with `popd`. | |
| function up() { | |
| DOTS="" | |
| for x in $(seq "$1"); do | |
| DOTS="../$DOTS" | |
| done | |
| pushd "$DOTS" | |
| } | |
| # Textual diff: `<(cmd)` runs `cmd` and gets replaced with its output. | |
| # `tdiff a b` will diff the two parameters (remember quotes if they contain spaces). | |
| function tdiff() { | |
| diff -- <(echo -n "$1") <(echo -n "$2") | |
| } | |
| # Eternal history file. | |
| export HISTFILESIZE= | |
| export HISTSIZE= | |
| export HISTTIMEFORMAT="[%F %T] " | |
| export HISTFILE=~/.bash_eternal_history | |
| # This function will be run before showing a prompt. It figures out what the current branch is, | |
| # and stores it in a variable that will be referenced by the prompt definition (which also contains | |
| # a lot of color coding and different components like the current user, hostname, directory, and timestamp). | |
| function prompt_command { | |
| history -a | |
| if ! PROMPT_GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)"; then | |
| PROMPT_GIT_BRANCH="" | |
| fi | |
| export PROMPT_GIT_BRANCH | |
| } | |
| PROMPT_COMMAND=prompt_command | |
| PS1='\n'\ | |
| '${PROMPT_GIT_BRANCH:+(}\[\033[01;33m\]$PROMPT_GIT_BRANCH\[\033[00m\]${PROMPT_GIT_BRANCH:+) }'\ | |
| '${debian_chroot:+($debian_chroot)}'\ | |
| '\[\033[01;32m\]\u@\h\[\033[00m\]:'\ | |
| '\[\033[01;34m\]\w\[\033[00m\] '\ | |
| '[\[\033[01;36m\]\t\[\033[00m\]]'\ | |
| '\n\$ ' | |
| # Bonus: I started by measuring people's Bash familiarity by asking who knew the difference | |
| # between single and double quotes, but didn't get as far as to explain it! "Briefly": | |
| # - Unquoted strings end at the first space (unless you escape the space with a backslash), and: | |
| # - Dollar signs are expanded (replaced): | |
| # - $VAR expands to the value of the environment variable VAR | |
| # - ${VAR/a/b} does the same, except it replaces a with b | |
| # - $(cmd) runs cmd and gets replaced by its output - this is the same as `cmd` | |
| # - $n inside a script or alias is parameter number n (1 is the first one; 0 is the whole command) | |
| # - $? is the exit code of the previous process | |
| # - ~ expands to your home directory | |
| # - Anything containing a * expands to all matching files in this directory | |
| # - (cmd) runs cmd as a subprocess | |
| # - ! expands to some previous command, depending on the subsequent character | |
| # - Any other special character, like < or > or |, will end the unquoted string and be interpreted | |
| # as per the Bash syntax (e.g. | performs piping). | |
| # - They may not contain single quotes or double quotes, but you can escape them with backslash. | |
| # - Double-quoted strings: | |
| # - May contain spaces | |
| # - May contain actual newlines (press Enter, and you're still inside the string) | |
| # - Most characters including ~ * ! < > | are literal | |
| # - Dollar signs are still expanded | |
| # - Backslashes can be used to escape dollar signs and other backslashes. Note that other escape | |
| # characters are not defined: \n remains those two characters instead of becoming newline | |
| # (use echo -e if you need this transformation). | |
| # - Single-quoted strings are verbatim: | |
| # - Every character except single quote is itself (you can also use actual newlines); note that | |
| # backslashes can't be used for escaping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Store this in your ~/.gitconfig file (you could also put repo-specific aliases | |
| # in .git/config inside the repo directory), or store it in a separate file and | |
| # reference it from ~/.gitconfig like this: | |
| # | |
| # [include] | |
| # path = /path/to/this.file | |
| # This section of the git config file defines the aliases. | |
| [alias] | |
| # Abbreviations for some common commands. | |
| st = status | |
| co = checkout | |
| cob = checkout -b | |
| coB = checkout -B | |
| ca = commit --amend --no-edit | |
| caa = commit --amend --no-edit -a | |
| cam = commit --amend -m | |
| cm = commit -m | |
| # Alex' mood-based synonyms for `git blame`. | |
| disagree = blame | |
| praise = blame | |
| wow = blame | |
| # Your commit graph, presented as beautiful ASCII art. | |
| # Add specific branches to show the paths backward from those branches, | |
| # or `--all` to show the all the branches. | |
| go = log --graph --oneline --decorate | |
| gt = log --graph --pretty=format:'%Cred%h%Creset %C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit | |
| # Add only selected changes ("p" stands for "patch") - you will get an interactive editor. | |
| ap = add -p | |
| # When used with a branch/hash and a filename, copy only the selected parts | |
| # of that verison of the file. | |
| cop = checkout -p | |
| # Diff the workdir against the stage (thus showing unstaged changes). | |
| d = diff | |
| # Diff the stage against the active commit (thus showing staged changes). | |
| dc = diff --cached | |
| # Diff the workdir against the active commit (thus showing both staged and unstaged changes). | |
| # `HEAD` means the active commit. | |
| dH = diff HEAD | |
| # Only show the names of the changed files. | |
| dn = diff HEAD --name-only | |
| # The same diff types as the first three above (@ is a synonym for HEAD), | |
| # but showing only the actual characters that changed rather than full lines. | |
| dw = diff --word-diff=color --word-diff-regex=. | |
| dcw = diff --cached --word-diff=color --word-diff-regex=. | |
| dHw = diff @ --word-diff=color --word-diff-regex=. | |
| # An alias can be defined as a Bash expression instead. By defining a Bash function and calling it, | |
| # we can refrence the arguments of the alias. `git dp n` shows the diff of the last _n_ commits. | |
| dp = "!f() { git diff HEAD~$1...HEAD; }; f" | |
| # List your branches, in the order of when you most recently commited to them. | |
| r = "!f() { git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset)| %(color:bold blue)%(objectname:short) %(color:bold red)%(committerdate:relative) %(color:dim white)%(authorname)%0a %(color:black) %(color:reset)| %(contents:subject)%0a |' --color=always | grep -v '^ x-' | column -ts '|' | less --tabs=4 -RFX; }; f" | |
| # Get the current branch name (and nothing else, so that this can be passed to other aliases). | |
| bn = rev-parse --abbrev-ref HEAD | |
| # Get the hash of the current or given commit. | |
| id = "!f() { git rev-parse ${1:-HEAD}; }; f" | |
| # Did you accidentally commit to `main`? Creating another branch is easy, | |
| # but moving `main` back to where it was is a bit annoying. | |
| rb = "!f() { git branch -f $1 origin/$1; }; f" | |
| # Unless you have set up the tracking info of your branch correctly or configured | |
| # the way `push` and `pull` should work by default, those commands might require you to specify | |
| # which branch to operate on, even if it's the current branch. These aliases save some typing. | |
| # Note that when using a Bash function to define your alias, you can reference other aliases. | |
| fo = fetch origin | |
| po = "!f() { git pull origin `git bn`; }; f" | |
| pH = "!f() { git push origin `git bn`; }; f" | |
| pfH = "!f() { git push --force-with-lease origin `git bn`; }; f" | |
| # The merge base of two branches is the most recent mutual ancestor commit. | |
| # This finds the merge base of the given branch and the current branch | |
| # (or some other branch if the second argument is given). | |
| mb = "!f() { git merge-base $1 ${2:-HEAD}; }; f" | |
| # Show the commits on the given branch (default: current branch) that are not on main | |
| # (or whichever branch you supply as the second argument). "u" stands for "unmerged". | |
| u = "!f() { BRANCH=${1:-`git rev-parse --abbrev-ref HEAD`}; git log --graph --oneline --decorate $(git merge-base origin/${2:-main} $BRANCH)...$BRANCH; }; f" | |
| # `git graft x y z` cuts the topmost y commits (default: 1) from branch x off and paste them | |
| # (and the branch name x) on top of the branch z (default: current branch). | |
| # (In everyday English, "graft" means to cut a branch off of a tree and attach it | |
| # to another tree: "pode" in Norwegian.) | |
| graft = "!f() { TARGET=$(git rev-parse --abbrev-ref ${3:-HEAD}); git rebase -i $1~${2:-1} $1 --onto $TARGET && git branch --set-upstream-to $TARGET; }; f" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment