
Git Simple Cheatsheet
Git Simple Cheatsheet
This is a collection of some of the daily driving and most used git commands for easier reference and navigation.
Configuration
-
Set author name
1git config --global user.name "Your Name" -
Set author email
1git config --global user.email "email@example.com"
📜 Replace the quoted values with your own.
Initialization
-
Start a new repo in an empty directory
1git init -
Clone an existing repository
1git clone "repo-user/repo-name" -
Clone including submodules
1git clone --recurse-submodules user/name📜 Add
-j8to speed up large clones. -
Populate submodules in an already-cloned repo
1git submodule update --init --recursive
Staging and Committing
-
Check status
1git status -
Stage specific paths
1git add "file|folder" -
Stage everything except ignored files
1git add --allor
1git add . -
Commit staged changes
1git commit -m "message" -
Commit tracked changes without re-adding
1git commit -am "message"📜
git add .also picks up new (unignored) files;git add -udoes not.
Cleaning
-
Drop tracked files that are now ignored (clears stage)
1git rm -r --cached .Then commit; to remove ignored files locally:
git clean -d -x -f. -
Remove ignored/untracked files locally only
1git clean -d -x -f
Reset and Restore
-
Unstage changes (Stage → Work)
1git reset "file|folder"📜
git reset .unstages everything;git reset --hardalso discards working copy changes. -
Restore one file
1git restore file/folder -
Restore everything
1git restore .
History and Inspection
-
Full log
1git log -
File/directory history with patches
1git log -p path/to/target -
Commit overview with per-file stats
1git log --stat -
One-line graph of current branch
1git log --oneline --graph -
One-line graph of everything
1git log --oneline --decorate --all --graph -
Search commit messages (case-insensitive)
1git log -i --grep search_string -
Last N commits by author
1git log -n number --author=author -
Commits between dates (yyyy-mm-dd)
1git log --before="2017-01-29" --after="2017-01-17"
Branches
-
List branches (add
-rfor remote,-afor all)1git branch -
Create a branch
1git branch branch-name -
Rename current branch
1git branch -M main -
Switch branches (or checkout a commit)
1git checkout branch-name -
Delete a branch
1git branch -D branch-name
Merging
-
Merge another branch into current
1git merge another-branch📜 For unrelated histories:
git merge main --allow-unrelated-histories. -
Prefer current branch changes on conflict
1git merge -s ours another-branch -
Prefer incoming branch changes on conflict
1git merge -X theirs another-branch
Remotes and Sync
-
List remotes
1git remote -v -
Add a remote
1git remote add origin url -
Remove a remote
1git remote remove origin -
Push (and set upstream once)
1git push -u remote branch📜 After upstream is set, use
git push. -
Pull latest
1git pull
Submodules
-
Add a submodule
1git submodule add "https://github.com/user/repo-name" "path/"Track them in
.gitmodules; to update,cdinto the submodule,git pull, then stage in the parent.
Large Files (Git LFS)
-
Install Git LFS (once per user)
1git lfs installEnsure files are tracked in
.gitattributes; for migrations seegit-lfs-migrateand the docs below. -
Docs: Git Large File Storage
Authentication (GitHub)
You can simply use github-cli to authenticate with GitHub. Install it, and run,
|
|
Signing Commits (GPG)
To sign commits, and send signed tags, you need to have GPG installed and configured. Follow this guide,
To sign all commits by default,
|
|
Troubleshooting
- Handy reference when things go sideways: Oh Shit, Git!?!
Acknowledgements
- Copyright: https://github.com/tldr-pages/tldr