Git GitHubCheatSheet
1. Local Version Control: Git
Git handles local tracking, history tree management, and state transitions.
[ Working Directory ]
│
│ git add <file>
▼
[ Staging Area ]
│
│ git commit -m "message"
▼
[ Local Repository ]
Repository Initialization & Configuration
Establish a new tracking environment or configure identity across local operations.
- Initialize a new local repository
git init
- Configure global user identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
- Check operational status (Inspects working directory vs. staging area states)
git status
The Three-Tree Architecture: Saving Versions
Git manages code assets through three internal states: Working Directory (untracked/modified), Staging Area (index of upcoming commit), and Local Repository (immutable history).
- Stage modifications (Move from Working Directory to Staging Area)
- Specific file:
git add path/to/file.ext
- All changes, tracking deletes/additions:
git add .
- Commit staged changes (Snapshot Staging Area into Local Repository history)
git commit -m "feat: implement structured state serialization"
- Amend the immediate last commit (Combines staged changes with previous commit; rewrites local history)
git commit --amend --no-edit
Branching & State Isolation
Branches represent independent lines of development, acting as pointers to specific commits.
- Create a new branch
git branch <branch-name>
- Switch execution context to an existing branch
Bash
git checkout <branch-name>
# Modern alternative: git switch <branch-name>
- Atomic creation and context switch
git checkout -b <branch-name>
# Modern alternative: git switch -c <branch-name>
- List all local branches (-a includes tracking remote branches)
git branch -a
- Delete a localized branch (Safe mode: fails if unmerged)
git branch -d <branch-name>
- Force delete a branch (Bypasses merge verification)
git branch -D <branch-name>
Merging & Integration Operations
Integrating history lines from different branches.
- Merge target branch into current active branch
git merge <target-branch-name>
- Abort an active merge conflict state (Restores state prior to the merge execution)
git merge --abort
Undo, State Reversion, & Inspection
Mechanisms to traverse or alter history trees and uncommitted states.
- View commit history log
git log --oneline --graph --decorate
- Discard unstaged modifications in working directory
git checkout -- <file-path>
# Modern alternative: git restore <file-path>
- Unstage a file while preserving modifications
git reset HEAD <file-path>
# Modern alternative: git restore --staged <file-path>
- Soft Reset (Rolls back commits; moves HEAD to target commit; preserves staging area and working directory contents)
git reset --soft <commit-hash>
- Hard Reset (CRITICAL: Destructive operation. Obliterates all commits, staging assets, and working directory modifications past the target commit hash)
git reset --hard <commit-hash>
- Stash temporary state (Saves modified/staged state to internal stack, returning working directory to clean HEAD state)
git stash
- Apply and remove top stashed item
git stash pop
2. Remote Coordination: GitHub
GitHub acts as the centralized host for shared remote repository structures and collaborative flow mechanics.
Remote Association & Cloning
- Clone an existing remote repository locally
git clone https://github.com/username/repository-name.git - Inspect associated remote endpoints
git remote -v - Link a local repository to a remote architecture
git remote add origin https://github.com/username/repository-name.git - Rename remote tracking pointer
git remote rename <old-name> <new-name>
Data Synchronization: Push & Pull
Synchronizing states between local database structures and remote target locations.
- Push local branch upstream (Initial baseline establishment)
git push -u origin <branch-name>
- Push subsequent updates to a configured upstream branch
git push
- Force push state (Overwrites remote history with local branch state; use with extreme caution)
git push --force
- Fetch remote definitions and merge into local branch (Combines git fetch and git merge)
git pull
- Download remote tracking updates without merging into local source files
git fetch origin
The Fork & Pull Request Collaboration Pipeline
Workflow protocol for contributing to upstream architectures where direct write permissions are restricted.
1. Fork the Target Repository Execute via GitHub Web UI: Navigate to the upstream target repository page and click the "Fork" button to instantiate a copy under your personal profile domain.
2. Clone Personal Fork Locally
git clone https://github.com/your-username/forked-repo-name.git
cd forked-repo-name
3. Establish Upstream Tracking Link (Enables synchronization with the primary source repository)
git remote add upstream https://github.com/original-author/forked-repo-name.git
4. Isolate Execution State (Create feature-specific branch)
git checkout -b feat/optimization-patch
5. Commit Changes Locally
git add .
git commit -m "perf: optimize runtime memory footprint"
6. Push Local Changes to Personal Fork (Origin)
git push -u origin feat/optimization-patch
7. Instantiate Pull Request Execute via GitHub Web UI: Return to your fork's GitHub dashboard page. Select the "Compare & pull request" trigger banner, define target baseline branch conditions, detail structural adjustments, and submit for evaluation.
Synchronizing a Forked Repository with Upstream Master
Prevent divergence from the active source tree by pulling structural shifts from the primary repository.
- Fetch and merge upstream baseline updates
git fetch upstream
git checkout main
git merge upstream/main
git push origin main