Skip to content

Git

Git cheat sheet: configuration, aliases, tokens, signed-off commits, cloning and branches.

5 min read

References

Configuration

Alias

To create shortcut for long commands. Below defines loagd instead of log --oneline --all --graph --decorate. Config file location can be --global, --system, --local, --file, --blob.

Terminal window
git config --global alias.loagd "log --oneline --all --graph --decorate"

List, locate, and inspect aliases:

Terminal window
# List all global config
git config --global --list
# Find the origin of a config key
git config --show-origin alias.loagd
# Show the value of a config key
git config --get alias.loagd

Personal Access Token (PAT)

You can use personal access tokens to authenticate against Git over HTTP. They are the only accepted password when you have Two-Factor Authentication (2FA) enabled.

Create a PAT first:

Terminal window
git clone https://github.com/user-or-organisation/myrepo.git
Username: <my-username>
Password: <my-personal-access-token>

Sign Off Your Commits

Terminal window
git commit --signoff --message 'This is my commit message'
git commit -s -m "This is my commit message"

Cloning

Clone a specific branch

Terminal window
git clone -b <branch> <remote_repo>

Clone including submodules

Terminal window
git clone --recurse-submodules -j8 <remote_repo>

Branches

List branches

Terminal window
git branch # local branches
git branch -a # all branches (local + remote)

Create a branch

Terminal window
git branch <branch-name>
git branch <branch-name> <sha1-of-commit> # from a specific commit
git checkout -b <branch-name> # create and switch

Switch branches

Terminal window
git checkout <branch-name>

Push a branch to remote

Terminal window
git push origin <branch-name>

Rename a branch

Terminal window
# Rename the current branch
git branch -m <new-name>
# Rename a different branch
git branch -m <old-name> <new-name>
# Update the remote
git push origin :<old-name> <new-name>
git push origin -u <new-name>

Delete a branch

Terminal window
# Delete locally
git branch -d <branch-name>
# Delete remotely
git push origin --delete <branch-name>
# or shorter
git push origin :<branch-name>

Prune stale remote-tracking branches

Deletes stale remote-tracking branches that have already been removed from the remote.

Terminal window
# Preview what will be removed
git remote prune origin --dry-run
# Actually prune
git remote prune origin

Move uncommitted changes to a new branch

Terminal window
git checkout -b <new-branch>
git status

Rename the default branch

Terminal window
git branch -m old_branch new_branch
git push origin new_branch
# Then change "Default Branch" in GitHub/GitLab settings
git push origin :old_branch

Commits

Amend the latest commit message

Terminal window
git commit --amend -m "New message"
git push --force origin <branch-name>

Remove an unpushed commit (keep changes staged)

Terminal window
git reset --soft HEAD~

Remove a pushed commit

Find the commit you want to revert to:

Terminal window
git log

Force push that commit as the new HEAD:

Terminal window
git push origin +<commit-hash>:main

Or reset and force push:

Terminal window
git reset --hard <commit-hash>
git push origin <branch-name> -f

If others work on the same branch, they need to sync:

Terminal window
git fetch
git reset --hard origin/<branch-name>

Switch to a specific commit

Terminal window
git checkout <commit-hash>
# Optionally create a branch from it
git checkout -b <new-branch> <commit-hash>

Tags

Rename a tag

Terminal window
git tag <new> <old>
git tag -d <old>
git push origin :refs/tags/<old>
git push --tags

Delete a tag

Terminal window
git push origin :refs/tags/<tag-name>
git tag --delete <tag-name>

Submodules

Delete a submodule

Remove the submodule entry from .git/config:

Terminal window
git submodule deinit -f path/to/submodule

Remove the submodule directory from .git/modules:

Terminal window
rm -rf .git/modules/path/to/submodule

Remove the entry in .gitmodules and the submodule directory:

Terminal window
git rm -f path/to/submodule

Syncing & Remotes

View remote URLs

Terminal window
git remote -v

Set up upstream remote (for forks)

Terminal window
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

Sync fork with upstream

Terminal window
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Fetch everything (branches, tags, prune)

Terminal window
git fetch --all --tags --prune

Pull with rebase

Rebase your local changes on top of the latest remote changes:

Terminal window
git pull --rebase

Undoing Changes

Revert uncommitted changes

Terminal window
# Revert modified files
git reset --hard
# Remove untracked files and directories
git clean -fd

Reset local branch to match remote

Terminal window
git fetch origin
git reset --hard origin/main

Save your current state before resetting (just in case):

Terminal window
git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Reset both local and remote

Terminal window
git reset --hard <commit-hash> && git push --force origin <branch-name>

Merge Strategies

Explicit Merge: Creates a new merge commit. (--no-ff)

Explicit Merge

Workflow:

Terminal window
# Stage all changes
git add .
# Commit the changes
git commit -m "Some messages"
# Push to remote
git push origin development
# Switch to the target branch
git checkout main
# Pull latest changes
git pull origin main
# Merge the changes
git merge --no-ff development
# Push the merged changes
git push origin main
# Switch back to the development branch
git checkout development

Fast Forward Merge: Forward rapidly, without creating a new commit: (--ff)

Fast Forward Merge

Rebase: Establish a new base level:

Rebase

Squash on Merge: Crush or squeeze (something) with force so that it becomes flat: (--squash)

Squash on Merge

Fork & Pull Request Workflow

  1. Fork the repository and clone your fork
  2. Set up the upstream remote
  3. Create a topic branch for your changes
  4. Commit, push, and open a Pull Request
Terminal window
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
git fetch upstream
git checkout main
git merge upstream/main
git checkout -b ISSUE1
# ... make changes ...
git commit -am "Fix for ISSUE1"
git push origin ISSUE1
# Open a PR from ISSUE1 → upstream/main

Inspection

View commit history

Terminal window
git log
git log --oneline --all --graph --decorate

Show current commit hash

Terminal window
git log --pretty=format:'%h' -n 1

List the N largest files

Terminal window
git ls-tree -r -t -l --full-name HEAD | sort -n -k 4 | tail -n 10

Quick Reference

Command Description
git init Initialize a new repository
git add Stage files
git commit Commit staged files
git branch List branches
git branch <name> Create a new branch
git checkout <branch> Switch to a branch
git merge <branch> Merge a branch into current
git diff Show unstaged changes
git diff --cached Show staged changes
git log Show commit history

Related articles

1 min read

ngrok

Exposing a local server to the internet with ngrok tunnels for testing and demos.

  • Developer Tools
1 min read

NetBeans

Building NetBeans from source, cleaning the Recent Projects list and choosing the JDK it runs on.

  • Developer Tools
1 min read

vim

Vim and Neovim setup: vimtex with Zathura, Neo-tree, packer.nvim and vim-plug.

  • Developer Tools

Type to search the whole site.