Contents

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

    1
    
    git config --global user.name "Your Name"
    
  • Set author email

    1
    
    git config --global user.email "email@example.com"
    

📜 Replace the quoted values with your own.

Initialization

  • Start a new repo in an empty directory

    1
    
    git init
    
  • Clone an existing repository

    1
    
    git clone "repo-user/repo-name"
    
  • Clone including submodules

    1
    
    git clone --recurse-submodules user/name
    

    📜 Add -j8 to speed up large clones.

  • Populate submodules in an already-cloned repo

    1
    
    git submodule update --init --recursive
    

Staging and Committing

  • Check status

    1
    
    git status
    
  • Stage specific paths

    1
    
    git add "file|folder"
    
  • Stage everything except ignored files

    1
    
    git add --all
    

    or

    1
    
    git add .
    
  • Commit staged changes

    1
    
    git commit -m "message"
    
  • Commit tracked changes without re-adding

    1
    
    git commit -am "message"
    

    📜 git add . also picks up new (unignored) files; git add -u does not.

Cleaning

  • Drop tracked files that are now ignored (clears stage)

    1
    
    git rm -r --cached .
    

    Then commit; to remove ignored files locally: git clean -d -x -f.

  • Remove ignored/untracked files locally only

    1
    
    git clean -d -x -f
    

Reset and Restore

  • Unstage changes (Stage → Work)

    1
    
    git reset "file|folder"
    

    📜 git reset . unstages everything; git reset --hard also discards working copy changes.

  • Restore one file

    1
    
    git restore file/folder
    
  • Restore everything

    1
    
    git restore .
    

History and Inspection

  • Full log

    1
    
    git log
    
  • File/directory history with patches

    1
    
    git log -p path/to/target
    
  • Commit overview with per-file stats

    1
    
    git log --stat
    
  • One-line graph of current branch

    1
    
    git log --oneline --graph
    
  • One-line graph of everything

    1
    
    git log --oneline --decorate --all --graph
    
  • Search commit messages (case-insensitive)

    1
    
    git log -i --grep search_string
    
  • Last N commits by author

    1
    
    git log -n number --author=author
    
  • Commits between dates (yyyy-mm-dd)

    1
    
    git log --before="2017-01-29" --after="2017-01-17"
    

Branches

  • List branches (add -r for remote, -a for all)

    1
    
    git branch
    
  • Create a branch

    1
    
    git branch branch-name
    
  • Rename current branch

    1
    
    git branch -M main
    
  • Switch branches (or checkout a commit)

    1
    
    git checkout branch-name
    
  • Delete a branch

    1
    
    git branch -D branch-name
    

Merging

  • Merge another branch into current

    1
    
    git merge another-branch
    

    📜 For unrelated histories: git merge main --allow-unrelated-histories.

  • Prefer current branch changes on conflict

    1
    
    git merge -s ours another-branch
    
  • Prefer incoming branch changes on conflict

    1
    
    git merge -X theirs another-branch
    

Remotes and Sync

  • List remotes

    1
    
    git remote -v
    
  • Add a remote

    1
    
    git remote add origin url
    
  • Remove a remote

    1
    
    git remote remove origin
    
  • Push (and set upstream once)

    1
    
    git push -u remote branch
    

    📜 After upstream is set, use git push.

  • Pull latest

    1
    
    git pull
    

Submodules

  • Add a submodule

    1
    
    git submodule add "https://github.com/user/repo-name" "path/"
    

    Track them in .gitmodules; to update, cd into the submodule, git pull, then stage in the parent.

Large Files (Git LFS)

  • Install Git LFS (once per user)

    1
    
    git lfs install
    

    Ensure files are tracked in .gitattributes; for migrations see git-lfs-migrate and the docs below.

  • Docs: Git Large File Storage

Authentication (GitHub)

You can simply use github-cli to authenticate with GitHub. Install it, and run,

1
gh auth login

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,

1
git config --global commit.gpgSign true

Troubleshooting

Acknowledgements