Notes to Self

Alex Sokolsky's Notes on Computers and Programming

git

Topics

And more:

Configs

System level (applied to every user on the system and all their repositories)

to view, git config --list --system (may need sudo)
to set, git config --system color.ui true
to edit system config file, git config --edit --system

Global level (values specific personally to you, the user. )

to view, git config --list --global
to set, git config --global user.name xyz
to edit global config file, git config --edit --global

Repository level (specific to that single repository)

to view, git config --list --local
to set, git config --local core.ignorecase true (--local optional)
to edit repository config file, git config --edit --local (--local optional)

To view all settings:

Run git config --list, showing system, global, and (if inside a repository) local configs
Run git config --list --show-origin, also shows the origin file of each config item

To read one particular config:

Run git config user.name to get user.name, for example.
You may also specify options --system, --global, --local to read that value at a particular level.

Global Config

Followed Customizing Git Configuration.

git config --global user.name "Alex Sokolsky"
git config --global user.email "asokolsky@gmail.com"
git config --global core.editor /usr/bin/emacs
git config --global core.excludesfile ~/.gitignore_global
git config --global core.pager ''

where ~/.gitignore_global:

*~
.*.swp
.DS_Store

Handling an Upstream Repo

alex@latitude7490:~/Projects/psmqtt/ > git remote -v
origin  https://github.com/asokolsky/psmqtt.git (fetch)
origin  https://github.com/asokolsky/psmqtt.git (push)
upstream        https://github.com/eschava/psmqtt.git (fetch)
upstream        https://github.com/eschava/psmqtt.git (push)

alex@latitude7490:~/Projects/psmqtt/ > git status
On branch typing
Your branch is up to date with 'origin/typing'.

nothing to commit, working tree clean

Just pull from upstream:

alex@latitude7490:~/Projects/psmqtt/ > git pull upstream master

And then push

alex@latitude7490:~/Projects/psmqtt/ > git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/asokolsky/psmqtt.git
   a98ddea..52094f3  master -> master

Repo-specific Config

Set repo-specific (non-global) config settings:

git config user.name "Alex Sokolsky"
git config user.email "asokolsky@gmail.com"
git config core.sshCommand "ssh -i ~/.ssh/private_key"

Make sure your private identity is available:

ssh-add ~/.ssh/private_key

Then

git remote add origin git@github.com:asokolsky/repo.git
git push --set-upstream origin master

Importing a file from a different repo

Instead of using git submodules, sometimes this may make more sense:

#!/bin/bash
#
# This will update DST with the latest version of SRC from REPO.
DST=discard_me.cpp
SRC=trash1.cpp
REPO_HOST="git@github.com:asokolsky"
REPO=oddeven
#
# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail
# Turn on traces, useful while debugging but commented out by default
#set -o xtrace
# Check syntax with `shellcheck ./goto`

# this repo root
DSTROOT=$(git rev-parse --show-toplevel)
# this one already defined in MacOS
: "${TMPDIR:=/tmp}"

pushd "${TMPDIR}" > /dev/null
echo "Retrieving ${REPO}/${SRC}"
git clone --quiet --no-checkout --depth 1 "${REPO_HOST}/${REPO}.git"
cd $REPO
echo "Overwriting ${DSTROOT}/${DST}"
git show HEAD:$SRC > "${DSTROOT}/${DST}"
popd > /dev/null
rm -rf "${TMPDIR}/${REPO}"