How to add a file to the last commit? / How to edit last commit’s message?
# stages the desired file
git add new-file
# changes the last commit's message and includes staged files in that commit
git commit --amend
How to update deleted remote branches?

git remote prune origin

Change remote URL
# view existing remote URL
git remote -v
# change existing remote URL
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

Reference: Github

Ignore local changes to tracked files (we have that with local modifications to config files)

git update-index --skip-worktree <file> (This only works if you don’t pull again from master)

Reference: StackOverflow

Undo last commit
# undo last commit and leave changes unstaged
git reset HEAD~
# undo last commit and leave changes staged
git reset --soft HEAD~
# undo last commit and undo changes (WARNING: This is permanent)
git reset --hard HEAD~

Reference: How to undo the last commits in Git?

Revert file permission changes in local git repository
git diff -p \ 
    | grep -E '^(diff|old mode|new mode)' \ 
    | sed -e 's/^old/NEW/;s/^new/old/;s/^NEW/new/' \ 
| git apply

Reference: Gist

View history of changes in a specific directory with diffs

git log -p DIR

View changes between branches (optionally in a specific directory)

git diff master..yourbranch path/to/folder

Change commit author at one specific commit
  1. Go to the commit immediately before the commit you want to edit: git rebase -i <earliercommit>
  2. In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify.
  3. Then do one of the following:
    • git commit --amend --author="Author Name <email@address.com>"
    • git commit --amend --reset-author --no-edit
  4. Finally end the rebase: git rebase --continue

Reference: Gist

How to set git user and email per directory

1. Install ondir (on a Mac: brew install ondir, other OSs need to install it from the linked page).
2. Put these functions into your shell profile (e.g. .profile, .zshrc):

# ondir configuration
cd() {
builtin cd "$@" && eval "`ondir "$OLDPWD" "$PWD"`"
}
pushd() {
builtin pushd "$@" && eval "`ondir "$OLDPWD" "$PWD"`"
}
popd() {
builtin popd "$@" && eval "`ondir "$OLDPWD" "$PWD"`"
}
eval "`ondir /`"

3. Configure ondir by putting these lines into ~/.ondirrc (adapt it to whatever you need):

enter ~/dev/work
export GIT_AUTHOR_NAME='Name at work'
export GIT_AUTHOR_EMAIL='email@work.com'
export GIT_COMMITTER_NAME='Name at work'
export GIT_COMMITTER_EMAIL='email@work.com'
echo 'Switched to git user/email settings for "work".'

leave ~/dev/work
unset GIT_AUTHOR_NAME
unset GIT_AUTHOR_EMAIL
unset GIT_COMMITTER_NAME
unset GIT_COMMITTER_EMAIL
echo 'Switched back to global git user/email settings.'

4. Now when you cd to ~/dev/work, git config user.name will still show the global git user name, but for commits, the environment variable will be used (check with echo $GIT_AUTHOR_NAME).
Don’t forget to source the changed shell profile to your current shell session.
Reference: How to set git user and email per directory

Tags: cheatsheet git