general

Change user and email of commit

git

log of subdirectory

git log -- path/to/subdir

ignore file mode (chmod) changes

git config core.fileMode false

show changed files only

git diff --name-only HEAD~1

files

revert single file

git checkout HEAD -- my-file.txt

Note: -- basically means treat every argument after this point as a file name.

modifying history

undo commit

keeping file changes

git reset --soft HEAD~1

discard file changes

git reset --hard HEAD~1

change git user name and email for all commits

changes every user(!) to the specified name and email

git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='Firstname Lastname'; GIT_AUTHOR_EMAIL='address@provider.com'; \
GIT_COMMITTER_NAME='Firstname Lastname'; GIT_COMMITTER_EMAIL='address@provider.com';" HEAD

archive branches

to archive branches, use tags. see also stackoverflow.com.

assign tag and delete branch

git tag archive/<branchname> <branchname>
git branch -D <branchname>

restore from tag

list all tags from archive

git tag | grep 'archive'

restore the branch from tag

git checkout -b <branchname> archive/<branchname>

archive many branches

#!/usr/bin/env bash

set -eux

while read -r branch
do
  tag=$(echo "$branch" | sed -e 's/ARCHIVE/archive/')
  git tag $tag $branch
  git branch -D $branch
done < <(git branch | grep "ARCHIVE")

fixup & autosquash

merge

merge git repository as subdirectory

As described on stackoverflow.com, to merge project projA into projB:

~/projB
~/path/to/projA

1) create clone of projA (note: procedure rewrites history of projA)

git clone ~/path/to/projA ~/new/path/to/projA_clone

2) in ~/new/path/to/projA_clone

PREFIX=subdir/path/in/projB  #adjust this

git filter-branch --index-filter '
    git ls-files -s |
    sed "s,\t,&'"$PREFIX"'/," |
    GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
    mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE
' HEAD

where PREFIX is adjusted to relative path within ~/projB.

3) In ~/projB, simply:

git pull path/to/A

merge git repository as subdirectory

(perserves history) stackoverflow.com

global repository modifications

"push down" repository contents into a subfolder