Setup Git Reference & Command Cheat Sheet
Git Setup & Command Reference Guide
Setting Up SSH for Git
To securely connect to GitHub using SSH, follow these steps to generate an SSH key and configure your GitHub account.
Step 1: Generate an SSH Key
- Open Terminal or Command Prompt:
- On Windows, you can use PowerShell or Command Prompt.
- Run the SSH Key Generation Command:
- Use the following command to create a new SSH key:
1
ssh-keygen -t ed25519 -C "your_email@example.com"
- Use the following command to create a new SSH key:
- Replace “your_email@example.com” with your actual email address.
- Follow the Prompts:
- You will be asked where to save the key. Press Enter to accept the default location.
- Optionally, set a passphrase for added security.
Step 2: Add the SSH Key to the SSH Agent
- Start the SSH Agent:
- Run the following command to ensure the SSH agent is running:
1
eval "$(ssh-agent -s)"
- Run the following command to ensure the SSH agent is running:
- Add Your SSH Key:
- Use the command below to add your SSH key to the agent:
1
ssh-add ~/.ssh/id_ed25519
- Use the command below to add your SSH key to the agent:
- Adjust the path if you saved your key in a different location.
Step 3: Add the SSH Key to Your GitHub Account
- Copy the Public Key:
- Use the following command to copy your public key to the clipboard:
1
cat ~/.ssh/id_ed25519.pub
- Use the following command to copy your public key to the clipboard:
- Alternatively, open the file in a text editor and copy its contents.
- Log into GitHub:
- Go to your GitHub account settings.
- Add a New SSH Key:
- Navigate to SSH and GPG keys.
- Click on New SSH key, paste your public key, and save it.
Step 4: Test Your SSH Connection
- Run the following command to test the connection:
1
ssh -T git@github.com - You should see a message confirming a successful connection.
By following these steps, you will have set up SSH for Git, allowing secure communication with GitHub.
Setting Up Your Git Username and Email
To properly configure your Git environment, you need to set your username and email address. This information is crucial as it is associated with every commit you make.
Global Configuration
To set your username and email globally (for all repositories on your system), use the following commands in your terminal:
- Set your username:
1
git config --global user.name "Your Name"
- Set your email:
1
git config --global user.email "you@example.com"
Local Configuration
If you want to set a different username or email for a specific repository, navigate to that repository’s directory and run:
- Set your username:
1
git config user.name "Your Name" - Set your email:
1
git config user.email "you@example.com"
Verifying Your Configuration
To check if your username and email have been set correctly, use the following command:
1
git config --list
This will display all your Git configuration settings, including the username and email.
Important Notes
- The global settings apply to all repositories unless overridden by local settings.
- Changes made will only affect future commits; past commits will retain the original information.
Git Command Cheat Sheet
1. Getting Started & Initial Branch Setup
- Start a new repository (default initial branch):
1
git init
- Start a new repository with initial branch
main:1 2 3
git init -b main # OR git init --initial-branch=main
- Set
mainas the global default initial branch:1
git config --global init.defaultBranch main - Rename an existing branch (e.g.,
mastertomain):1
git branch -m master main - Clone an existing repository:
1
git clone <url>
2. Configure Git
- Set a config option (local repository):
1
git config user.name 'Your Name' - Set option globally across system:
1
git config --global user.name 'Your Name'
- Set global default branch to
main:1
git config --global init.defaultBranch main - Add a command alias:
1
git config alias.st status
- See all configuration options:
1
man git-config
3. Prepare to Commit (Staging)
- Add untracked file or unstaged changes:
1
git add <file>
- Add all untracked files and unstaged changes:
1
git add . - Choose which parts of a file to stage interactively:
1
git add -p - Move or rename a file:
1
git mv <old> <new> - Delete a file:
1
git rm <file> - Stop tracking a file without deleting it locally:
1
git rm --cached <file>
- Unstage a single file:
1 2 3
git reset <file> # OR git restore --staged <file>
- Unstage everything:
1 2 3
git reset # OR git restore --staged .
- Check workspace status:
1
git status
4. Make Commits
- Make a commit (opens editor for message):
1
git commit
- Make a commit with an inline message:
1
git commit -m 'message'
- Commit all modified and deleted (unstaged) files:
1
git commit -am 'message'
- Amend last commit (message or add staged files):
1
git commit --amend
5. Branch Management & Switching
- Switch branches:
1 2 3
git switch <name> # OR git checkout <name> - Create and switch to a new branch:
1 2 3
git switch -c <name> # OR git checkout -b <name>
- List local branches:
1
git branch
- List branches sorted by most recent commit date:
1
git branch --sort=-committerdate
- Rename current branch (e.g.
mastertomain):1
git branch -m master main - Delete a branch safely:
1
git branch -d <name> - Force delete an unmerged branch:
1
git branch -D <name>
6. Diff Staged / Unstaged Changes & Commits
- Diff all staged and unstaged changes:
1
git diff HEAD
- Diff only staged changes:
1
git diff --staged - Diff only unstaged changes:
1
git diff
- Show diff between a commit and its parent:
1
git show <commit>
- Diff two specific commits:
1
git diff <commit1> <commit2>
- Diff changes in a single file since a specific commit:
1
git diff <commit> <file>
- Show diff summary statistics:
1 2
git diff <commit> --stat git show <commit> --stat
7. Referencing Commits
When <commit> is required, you can use:
- Branch name:
main - Tag name:
v0.1 - Commit hash/ID:
3e887ab - Remote branch:
origin/main - Current commit:
HEAD - Relative commits:
HEAD^^^orHEAD~3(3 commits prior)
8. Discard Changes, Edit History & Reflog
- Discard unstaged changes to a file:
1 2 3
git restore <file> # OR git checkout <file> - Discard all staged and unstaged changes to a file:
1 2 3
git restore --staged --worktree <file> # OR git checkout HEAD <file>
- Discard all staged and unstaged changes in repository:
1 2 3
git reset --hard # OR git restore --staged --worktree .
- Remove untracked files:
1
git clean
- Stash all staged and unstaged changes:
1
git stash
- Undo last commit preserving working tree changes (Soft Un-commit):
1
git reset HEAD^
- Amend current commit message or add staged changes:
1
git commit --amend - Interactive Rebase (Squashing / Editing recent commits):
1 2
git rebase -i HEAD~5 # In editor, change "pick" to "fixup" or "squash" to combine commits
- Emergency Recovery with Reflog (Undo failed rebase or lost commits):
1 2
git reflog <branchname> git reset --hard <commit-id>
9. Advanced Branch Combination Strategies
- Combine via rebase (linear history):
1 2
git switch feature git rebase main
- Combine via standard merge:
1 2
git switch main git merge feature
- Combine via squash merge (condense branch into single commit):
1 2 3
git switch main git merge --squash feature git commit - Bring branch up to date (fast-forward merge):
1 2
git switch main git merge feature
- Copy a specific commit to current branch:
1
git cherry-pick <commit>
10. Syncing & Remote Operations
- Push main branch to remote origin:
1
git push origin main
- Push current branch to its tracking branch:
1
git push
- Push a new local branch and set upstream tracking:
1
git push -u origin <name> - Safely force push (prevents overwriting remote work):
1
git push --force-with-lease - Push tags to remote:
1
git push --tags - Fetch remote changes without updating local branches:
1
git fetch origin main
- Fetch and rebase current branch on top of remote:
1
git pull --rebase - Fetch and merge into current branch:
1 2 3
git pull origin main # OR git pull
11. Code Archaeology & History Inspection
- View commit history:
1 2 3
git log main git log --graph main git log --oneline
- Show commits that modified a specific file:
1
git log <file>
- Show file history across renames:
1
git log --follow <file> - Find commits that added or removed specific text:
1
git log -G 'search text'
- Show line-by-line author attribution:
1
git blame <file>
12. Restore Files & Remotes
- Restore a file version from a specific commit:
1 2 3
git checkout <commit> <file> # OR git restore <file> --source <commit>
- Add a remote repository:
1
git remote add <name> <url>
13. Important Configuration Files
- Local repository configuration:
.git/config - Global user configuration:
~/.gitconfig - Ignored patterns list:
.gitignore