Skip to content

Instantly share code, notes, and snippets.

@StefDev
Last active February 25, 2025 14:10
Show Gist options
  • Save StefDev/6d9c674034d259cbc00c to your computer and use it in GitHub Desktop.
Save StefDev/6d9c674034d259cbc00c to your computer and use it in GitHub Desktop.
Useful tips for version control systems

Learning Resources

Cheat Sheets

Releases

Tutorials

gitignore - Specifies intentionally untracked files to ignore

  • Patterns to ignore in all situations: $XDG_CONFIG_HOME/git/ignore / $HOME/.config/git/ignore
    • specified/overwritten by core.excludesFile (gitconfig)
  • Atlassian: .gitignore

Merging vs. Rebasing

Squashing

Merge conflicts

Hooks

  1. $ cd .git/hooks
  2. $ mv pre-commit.sample pre-commit
  3. $ vim pre-commit
  4. $ chmod +x pre-commit

Tools

diff3

TUIs

Configuration

$ git config --global user.name "[name]"
$ git congig --global user.email "[email address]"

$ git config --global gpg.format ssh
$ git config --global user.signingkey <KEY>

$ git config --global init.defaultBranch main

$ git config --global log.date iso8601

$ git config --global core.editor helix

$ git config --global core.autocrlf input

$ git config --global diff.tool meld
$ git config --global difftool.prompt false

$ git config set merge.tool vscode

$ git config --global merge.tool vimdiff
$ git config --global merge.conflictStyle zdiff3

$ git config --global mergetool.prompt false

$ git config --global commit.verbose true

$ git config --unset user.name

Git aliases

$ git config --global alias.ci commit
$ git config --global alias.ciam "commit --all --message"
$ git config --global alias.st status

Specify how to reconcile divergent branches

$ git config pull.rebase false  # merge (the default strategy)
$ git config pull.rebase true   # rebase
$ git config pull.ff only       # fast-forward only

Add --global to set default preference for all repositories.
Pass --rebase, --no-rebase, or --ff-only on the command line to override configured default per invocation.

Edit configuration

Open editor to modify config file; either --system, --global, or repository (default)

$ git config --global --edit
$ git config --global core.fileMode false

Git prompt

GIT_PS1_SHOWDIRTYSTATE=1: * unstaged changes, + staged changes
GIT_PS1_SHOWUNTRACKEDFILES=1: % untracked files
GIT_PS1_SHOWUPSTREAM="auto": < behind upstream, > ahead upstream, <> diverged, = no difference

~/.ssh/config

Host *.github.com
  User git
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa

GPG

$ git config --global gpg.program gpg2
$ git stash push
$ git switch main
$ git stash list
$ git stash pop

Examples

git log - The good parts (HN thread)

$ git [log](http://www.git-scm.com/docs/git-log) -3 # Limit the number of commits to output.

Revert changes to file(s)

$ git checkout -- <file(s)>

Committed to wrong branch

$ git switch right-branch
$ git cherry-pick <commit-hash>
$ git switch -
$ git reset --hard HEAD~1

Compare development branch with origin:

$ git fetch origin
$ git log origin/branchname..branchname

Rename branch master to main (update local clone)

$ git branch --move master main
$ git fetch origin
$ git branch --set-upstream-to=origin/main main
$ git add <files>
$ git commit --amend --no-edit  # add <files> to previous (unpushed) commit without editing commit message
$ git diff -b # --ignore-space-change (ignore changes in amount of whitespace)
$ git diff --cached # Shows file differences between staging and the last file version (synonym: --staged)
$ git diff preview master # Changes between the tips of the preview and the master branches.
$ git checkout - # quickly jump back to your last git branch
$ git remote -v # view current remotes

GitHub Help

Articles

Reference

~/.hgrc (mercurial.ini in Windows User Home Dir)

What's new?

  • Release 4.2
    • Pager has moved from a bundled extension and into core. hg help pager
    • Color is now a core feature on by default. hg help color

Prepare Mercurial / Section ui

[ui]
username = Mr. Johnson <[email protected]>
verbose = True
paginate = never

GpgExtension

[extensions]
hgext.gpg =

[gpg]
# if cmd is not provided it defaults to gpg
cmd=/path/to/gpg-command-to-use
# key is optional and can be provided on the command line
key=KEYID

Configure compression for Mercurial

[ui]
ssh = ssh -C

The Mercurial system uses a file called .hgignore in the root directory of a repository to control its behavior when it searches for files that it is not currently tracking.

An untracked file X can be explicitly added with hg add X, even if X would be excluded by a pattern in .hgignore.

syntax: glob

Examples

$ hg log -p -l 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment