Written by: Geoffrey Callaghan

comprensive git cheatsheet

Comprensive Git Cheatsheet

Sure! Here is a comprehensive Git cheatsheet that covers essential commands and concepts:

Git Basics

Configuration

  • Set user name: git config --global user.name "Your Name"
  • Set user email: git config --global user.email "your.email@example.com"
  • List configuration: git config --list

Initialization

  • Initialize a new repository: git init
  • Clone a repository: git clone [url]

Working with Commits

Staging and Committing

  • Check status: git status
  • Add file(s) to staging area: git add [file] or git add . (all files)
  • Commit changes: git commit -m "commit message"
  • Amend last commit: git commit --amend

Viewing Changes

  • Show changes: git diff
  • Show staged changes: git diff --staged
  • View commit history: git log
  • View commit history (one line): git log --oneline
  • Show specific commit: git show [commit]

Branching and Merging

Branch Management

  • List branches: git branch
  • Create new branch: git branch [branch-name]
  • Switch to branch: git checkout [branch-name]
  • Create and switch to new branch: git checkout -b [branch-name]
  • Delete branch: git branch -d [branch-name]
  • Rename branch: git branch -m [new-name]

Merging

  • Merge branch into current branch: git merge [branch-name]
  • Abort merge: git merge --abort

Rebase

  • Rebase current branch onto another: git rebase [branch-name]
  • Continue rebase after conflict resolution: git rebase --continue
  • Abort rebase: git rebase --abort

Remote Repositories

Working with Remotes

  • Add remote repository: git remote add [name] [url]
  • Remove remote: git remote remove [name]
  • Show remote details: git remote -v

Pushing and Pulling

  • Push to remote repository: git push [remote] [branch]
  • Push all branches: git push --all [remote]
  • Pull from remote repository: git pull [remote] [branch]
  • Fetch changes from remote: git fetch [remote]

Stashing

Stash Management

  • Save changes to stash: git stash
  • List stashes: git stash list
  • Apply stash: git stash apply [stash]
  • Apply and remove stash: git stash pop
  • Drop stash: git stash drop [stash]

Undoing Changes

Resetting

  • Unstage a file: git reset [file]
  • Reset to last commit: git reset --hard
  • Reset to specific commit: git reset --hard [commit]
  • Soft reset (keep changes): git reset --soft [commit]
  • Mixed reset (unstage changes): git reset --mixed [commit]

Reverting

  • Revert a commit: git revert [commit]

Viewing and Comparing

Logs and Diffs

  • Show commit history: git log
  • Show changes for a commit: git show [commit]
  • Show changes between commits: git diff [commit1] [commit2]
  • Show changes in a file: git diff [commit] [file]
  • Show file status: git status

Tags

Tagging

  • Create a tag: git tag [tag-name]
  • Create annotated tag: git tag -a [tag-name] -m "message"
  • List tags: git tag
  • Push tag to remote: git push [remote] [tag-name]

Advanced Commands

Cherry-Pick

  • Cherry-pick a commit: git cherry-pick [commit]

Submodules

  • Add submodule: git submodule add [url] [path]
  • Update submodules: git submodule update --init --recursive

Cleaning

  • Remove untracked files: git clean -f
  • Remove untracked directories: git clean -fd

Collaboration

Forking and Pull Requests

  • Fork a repository: Fork it on GitHub/GitLab/Bitbucket UI.
  • Create pull request: Use GitHub/GitLab/Bitbucket UI.

Configuration and Optimization

Aliases

  • Create alias: git config --global alias.co checkout
  • List aliases: git config --get-regexp alias

Example Workflow

  1. Initialize and configure Git:

    git init
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
  2. Create a branch and switch to it:

    git checkout -b new-feature
  3. Stage and commit changes:

    git add .
    git commit -m "Add new feature"
  4. Push changes to remote:

    git push origin new-feature
  5. Merge branch into main:

    git checkout main
    git merge new-feature
  6. Push changes to remote main:

    git push origin main

This cheatsheet covers the essential Git commands and workflows. Keep it handy as a quick reference guide while working with Git.