Git command line Cheat Sheet#
Warning
🚧 Under construction 🚧
This cheat sheet covers essential Git commands for daily workflows using GitLab and GitHub. Works on Linux, macOS, and Windows (bash/Miniforge).
There are other wasy to use git, including:
VSCode editor git integration (works really well!)
Specific software tools like GitHub Desktop (works really well but clunky for gitlab..).
Setup#
# Set your name and email (used in commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Check configuration
git config --list
Explanation: Configure Git identity so commits are labeled correctly.
Starting a Repository#
# Initialize a new Git repository
git init
# Clone an existing repository
git clone https://gitlab.com/username/repo.git
git clone https://github.com/username/repo.git
Explanation: init creates a new repo locally. clone copies an existing remote repo.
Checking Repository Status#
# See changes and untracked files
git status
# Show the last few commits
git log --oneline --graph --decorate
Explanation: status shows what’s staged, modified, or untracked. log shows commit history in a compact visual format.
Basic Workflow#
# Stage changes
git add <file> # Stage a single file
git add . # Stage all changes
# Commit changes
git commit -m "Commit message describing the changes"
# Amend last commit (if you forgot something)
git commit --amend -m "Updated commit message"
Explanation: Staging (add) selects files to commit. commit saves them locally. --amend edits the last commit if needed.
Branching#
# List branches
git branch
# Create a new branch
git checkout -b feature-branch
# Switch branch
git checkout main
# Delete a local branch
git branch -d feature-branch
Explanation: Branches let you work on features separately. checkout -b creates and switches in one step.
Updating & Merging#
# Pull latest changes from remote
git pull origin main
# Merge another branch into current branch
git merge feature-branch
Explanation: pull fetches updates and merges them. merge combines changes from another branch into your current branch.
Working with Remote Repositories#
# Show remote URLs
git remote -v
# Add a remote (if not already present)
git remote add origin https://gitlab.com/username/repo.git
# Push changes
git push origin main # Push main branch
git push -u origin feature-branch # Push new branch and track it
# Delete remote branch
git push origin --delete feature-branch
Explanation: remote commands manage connections to servers. push uploads commits. -u sets upstream tracking for future pushes/pulls.
Undoing Changes#
# Discard changes in working directory
git checkout -- <file>
# Unstage changes
git reset <file>
# Reset to last commit (dangerous, discard commits)
git reset --hard HEAD
Explanation: Undo mistakes in working files (checkout), staged files (reset), or commits (reset --hard).
Stashing#
# Save changes temporarily
git stash
# Apply stashed changes
git stash apply
# List stashes
git stash list
Explanation: Temporarily save changes without committing. Useful to switch branches without losing work.
Tips for GitLab/GitHub#
Merge Requests / Pull Requests: Create feature branches, push them, open MR/PR on web interface.
SSH vs HTTPS: SSH keys avoid typing passwords for every push/pull.
Avoid pushing secrets:
.gitignoreexcludes local config or large files.Interactive staging:
git add -plets you stage specific changes, not whole files.
End of Git Cheat Sheet
Setup (continued: SSH keys for remote)#
# Generate a new SSH key (if you don't have one)
ssh-keygen -t ed25519 -C "you@example.com"
# Default location is usually fine (~/.ssh/id_ed25519)
# Start the SSH agent
eval "$(ssh-agent -s)"
# Add your SSH key to the agent
ssh-add ~/.ssh/id_ed25519
# Display your public key (copy this to GitLab/GitHub)
cat ~/.ssh/id_ed25519.pub
Explanation:
ssh-keygencreates a new key pair.ssh-agentruns in the background to manage keys.ssh-addloads your key into the agent.cat ...pubprints your public key to copy into your GitLab/GitHub account (Settings → SSH Keys).
Tip: After adding the key, test the connection:
# Test GitHub
ssh -T git@github.com
# Test GitLab
ssh -T git@gitlab.com
You should get a success message like:
“Hi username! You’ve successfully authenticated…”