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.
git config --global alias.loagd "log --oneline --all --graph --decorate"List, locate, and inspect aliases:
# List all global configgit config --global --list
# Find the origin of a config keygit config --show-origin alias.loagd
# Show the value of a config keygit config --get alias.loagdPersonal 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:
- https://help.github.com/articles/creating-an-access-token-for-command-line-use/
- https://gitlab.com/profile/personal_access_tokens
git clone https://github.com/user-or-organisation/myrepo.gitUsername: <my-username>Password: <my-personal-access-token>Sign Off Your Commits
git commit --signoff --message 'This is my commit message'git commit -s -m "This is my commit message"Cloning
Clone a specific branch
git clone -b <branch> <remote_repo>Clone including submodules
git clone --recurse-submodules -j8 <remote_repo>Branches
List branches
git branch # local branchesgit branch -a # all branches (local + remote)Create a branch
git branch <branch-name>git branch <branch-name> <sha1-of-commit> # from a specific commitgit checkout -b <branch-name> # create and switchSwitch branches
git checkout <branch-name>Push a branch to remote
git push origin <branch-name>Rename a branch
# Rename the current branchgit branch -m <new-name>
# Rename a different branchgit branch -m <old-name> <new-name>
# Update the remotegit push origin :<old-name> <new-name>git push origin -u <new-name>Delete a branch
# Delete locallygit branch -d <branch-name>
# Delete remotelygit push origin --delete <branch-name># or shortergit push origin :<branch-name>Prune stale remote-tracking branches
Deletes stale remote-tracking branches that have already been removed from the remote.
# Preview what will be removedgit remote prune origin --dry-run
# Actually prunegit remote prune originMove uncommitted changes to a new branch
git checkout -b <new-branch>git statusRename the default branch
git branch -m old_branch new_branchgit push origin new_branch# Then change "Default Branch" in GitHub/GitLab settingsgit push origin :old_branchCommits
Amend the latest commit message
git commit --amend -m "New message"git push --force origin <branch-name>Remove an unpushed commit (keep changes staged)
git reset --soft HEAD~Remove a pushed commit
Find the commit you want to revert to:
git logForce push that commit as the new HEAD:
git push origin +<commit-hash>:mainOr reset and force push:
git reset --hard <commit-hash>git push origin <branch-name> -fIf others work on the same branch, they need to sync:
git fetchgit reset --hard origin/<branch-name>Switch to a specific commit
git checkout <commit-hash>
# Optionally create a branch from itgit checkout -b <new-branch> <commit-hash>Tags
Rename a tag
git tag <new> <old>git tag -d <old>git push origin :refs/tags/<old>git push --tagsDelete a tag
git push origin :refs/tags/<tag-name>git tag --delete <tag-name>Submodules
Delete a submodule
Remove the submodule entry from .git/config:
git submodule deinit -f path/to/submoduleRemove the submodule directory from .git/modules:
rm -rf .git/modules/path/to/submoduleRemove the entry in .gitmodules and the submodule directory:
git rm -f path/to/submoduleSyncing & Remotes
View remote URLs
git remote -vSet up upstream remote (for forks)
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.gitSync fork with upstream
git fetch upstreamgit checkout maingit merge upstream/maingit push origin mainFetch everything (branches, tags, prune)
git fetch --all --tags --prunePull with rebase
Rebase your local changes on top of the latest remote changes:
git pull --rebaseUndoing Changes
Revert uncommitted changes
# Revert modified filesgit reset --hard
# Remove untracked files and directoriesgit clean -fdReset local branch to match remote
git fetch origingit reset --hard origin/mainSave your current state before resetting (just in case):
git commit -a -m "Saving my work, just in case"git branch my-saved-workReset both local and remote
git reset --hard <commit-hash> && git push --force origin <branch-name>Merge Strategies
Explicit Merge: Creates a new merge commit. (--no-ff)

Workflow:
# Stage all changesgit add .# Commit the changesgit commit -m "Some messages"# Push to remotegit push origin development# Switch to the target branchgit checkout main# Pull latest changesgit pull origin main# Merge the changesgit merge --no-ff development# Push the merged changesgit push origin main# Switch back to the development branchgit checkout developmentFast Forward Merge: Forward rapidly, without creating a new commit: (--ff)

Rebase: Establish a new base level:

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

Fork & Pull Request Workflow
- Fork the repository and clone your fork
- Set up the upstream remote
- Create a topic branch for your changes
- Commit, push, and open a Pull Request
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.gitgit fetch upstreamgit checkout maingit merge upstream/maingit checkout -b ISSUE1# ... make changes ...git commit -am "Fix for ISSUE1"git push origin ISSUE1# Open a PR from ISSUE1 → upstream/mainInspection
View commit history
git loggit log --oneline --all --graph --decorateShow current commit hash
git log --pretty=format:'%h' -n 1List the N largest files
git ls-tree -r -t -l --full-name HEAD | sort -n -k 4 | tail -n 10Quick 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 |