Git cheat sheet: configuration, GPG signing, branches and undo
Common Git commands: configuration, GPG signing, history, reset/revert, branches, tags, merge tools and GPG cache on Windows.
Git cheat sheet: initial configuration, GPG signing, history, undoing commits, branches, tags, diff/merge tools and GPG cache handling on Windows.
Initial configuration
git --version
git config --global user.name "app_user"
git config --global user.email "user@example.com"
git config --global branch.autosetuprebase always
git config --global color.ui true
git config --global color.status auto
git config --global color.branch auto
git config --global core.editor vim
git config --global merge.tool vimdiff
git config --list
Plain-text credential storage (avoid on shared machines):
git config --global credential.helper store
On Windows, to force credential storage through DPAPI and check it:
git config --global credential.credentialStore dpapi
git config --global --get credential.credentialStore
To find where a duplicated credential setting comes from:
git config --list | Select-String "user\.|credential\."
git config --global --list | Select-String "credential\.azrepos.*username"
git config --local --list | Select-String "credential\.azrepos.*username"
Signing commits and tags with GPG
gpg --list-secret-keys --keyid-format LONG
git config --global gpg.program "c:/Program Files (x86)/GnuPG/bin/gpg.exe"
git config --global user.signingkey "<KEY_ID>"
git config --global commit.gpgsign true
git config --global tag.gpgsign true
# show where each setting comes from
git config --list --show-origin
Manual signing:
git commit -S -m "message"
git tag -s v0.0.1 -m "signed"
History (log)
git log --oneline --graph --decorate --all --color
git log --pretty=oneline
git log --stat
git log --pretty=format:"%h %s" --graph
git log --merges --graph --oneline --decorate
git show <commit-hash>
git log -S "searched_text" # find the commit that adds/removes a string
More readable output and an alias:
git log --graph --pretty=format:'%C(yellow)%h%C(reset) - %C(cyan)%an%C(reset) %Cgreen(%cr)%C(reset) %s %C(auto)%d%C(reset)' --all
git config --global alias.tree "log --graph --decorate --all --pretty=format:'%C(yellow)%h%C(reset) %Cblue%an%C(reset) %Cgreen%ad%C(reset) %s %C(auto)%d%C(reset)' --date=format:'%Y-%m-%d %H:%M:%S'"
Remote repository
git remote -v
git remote add origin https://git.example.com/org/repo.git
git remote set-url origin https://git.example.com/org/repo.git
git push -u origin master
git push --set-upstream origin master
Syncing with the remote and undoing changes
Warning: git reset --hard discards local changes and unpushed commits.
# save your work just in case
git commit -a -m "Saving my work, just in case"
git branch my-saved-work
# align the local branch with the remote
git fetch origin
git reset --hard origin/master
git clean -n -f -d # dry run
git clean -f -d # actually remove untracked files
git fetch only downloads new data without touching working files; git pull downloads and integrates it (a merge conflict can occur). Run git pull on a clean working copy (use git stash if needed).
Undoing commits:
# drop unpublished commits (destroys local modifications)
git reset --hard <commit>
# undo already published commits with new commits
git revert a867b4af 25eee4ca 0766c053
git revert HEAD~2..HEAD
git revert -m 1 <merge_commit_sha> # revert a merge commit
Restoring a file:
git checkout 'master@{7 days ago}' -- path/to/file.txt
git checkout <commit> path/to/file
Moving the latest commits to a new branch:
git branch newbranch
git reset --keep HEAD~3 # move master back by 3 commits
git checkout newbranch
Branches
git branch # local
git branch -r # remote
git branch -a # all
git remote update origin --prune
git checkout -b my-branch-name
git checkout my-branch-name
git checkout --track origin/my-branch-name
git push -u origin HEAD
git merge my-branch-name
git branch -d my-branch-name # deletes only if merged
git branch -D my-branch-name # deletes regardless
git push origin --delete my-branch-name
git rebase master
Tags
git tag -a v1.4 -m "my version 1.4"
git tag
git push origin <tag_name>
git push --tags
git push --follow-tags
git config --global push.followTags true
git describe --tags --exact-match
git tag --points-at HEAD
Line endings
LF will be replaced by CRLF warning on Windows:
git config --global core.autocrlf false
Submodules
git config --global push.recurseSubmodules on-demand
git submodule foreach 'git status'
git pull --recurse-submodules && git submodule update --init --recursive
Diff and merge tools
# vimdiff
git config --global diff.tool vimdiff
git config --global merge.tool vimdiff
git config --global mergetool.prompt false
# Meld
git config --global diff.tool meld
git config --global difftool.meld.cmd "'C:/Program Files (x86)/Meld/Meld.exe' \"$LOCAL\" \"$REMOTE\""
git config --global merge.tool meld
git config --global mergetool.meld.cmd "'C:/Program Files (x86)/Meld/Meld.exe' \"$LOCAL\" \"$BASE\" \"$REMOTE\" --output=\"$MERGED\""
git config --global mergetool.prompt false
# Visual Studio Code
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff \"$LOCAL\" \"$REMOTE\""
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait \"$MERGED\""
git config --global mergetool.prompt false
GPG cache on Windows (PowerShell)
Check the GPG agent configuration:
Get-Content "$(gpgconf --list-dirs homedir)\gpg-agent.conf"
The default-cache-ttl, max-cache-ttl, default-cache-ttl-ssh and max-cache-ttl-ssh settings control how long the passphrase stays cached.
Test a signature without creating a commit on the branch:
git commit-tree -S -m "GPG cache test" 'HEAD^{tree}'
- If a hash is returned directly, signing succeeded without a passphrase prompt.
- If GPG asks for the passphrase, it was no longer cached.
- Nothing changes in the branch or in the Git history.
Force GPG to ask for the passphrase again:
gpgconf --kill gpg-agent
The agent restarts automatically on next use.
gpgconf --list-options gpg-agent | Select-String "cache-ttl"
Notes
gpgconf --kill gpg-agentclears the cache of all keys managed by the agent.- GPG cannot report the remaining time left in the cache.