Computer Notes/GIT/Git Cheat Sheet

From Cramsession
Jump to navigationJump to search
✍️ Verified Author: MflavellClick to view professional profile & credentials

Starting a Project

Command Action
git init Initialize a new local Git repository
git clone <url> Download a repository from a remote server

Staging & Committing

Command Action
git status Show modified, untracked, and staged files
git add <file> Stage a specific file
git add . Stage all modified and new files
git add -f <file> Force-stage a file ignored by .gitignore
git commit -m "message" Save staged snapshot with a description
git commit -am "message" Stage tracked changes and commit in one step

Branching & Merging

Command Action
git branch List all local branches
git branch <name> Create a new branch
git switch <name> Switch to an existing branch
git switch -c <name> Create and switch to a new branch
git merge <branch> Merge target branch into active branch
git branch -d <name> Delete a merged branch

Syncing & Remotes

Command Action
git remote add origin <url> Link local repository to a remote URL
git fetch Download remote changes without merging
git pull Fetch and merge changes from remote repository
git push -u origin <branch> Upload commits and set default remote branch
git push Upload commits to linked remote branch

Viewing History & Files

Command Action
git log --oneline View condensed commit history
git diff Show changes made to unstaged files
git diff --staged Show changes staged for the next commit
git ls-files List all files currently tracked by Git

Undoing Changes

Command Action
git restore <file> Discard uncommitted changes in a file
git restore --staged <file> Unstage a file without losing changes
git reset HEAD~1 Undo last commit, keeping changes locally
git reset --hard HEAD~1 Permanently discard last commit and all work
git commit --amend Add staged changes to or edit the last commit