Git & GitHub Commands Reference
1. Setup and Initialization
git --help - Displays all Git help commands and documentation.
git init - Initializes a new local Git repository in the current working directory, creating a
hidden `.git` folder.
git clone <remote_repo_url> - Copies an entire remote repository to a new local repository.
Example: git clone https://github.com/user/repo.git
git config --global user.name "Your Name" - Sets the global Git username for commit
authorship.
git config --global user.email "[email protected]" - Sets the global Git email for
commit authorship.
dir / ls - Lists contents of the current directory.
2. Basic Workflow (Add, Commit, Push)
git status - Shows the status of the working directory and staging area.
git add <file> / git add . - Adds specified files or all changes to the staging area.
git add -A - Stages all changes (new, modified, deleted).
git commit -m "Commit message" - Commits staged changes with a descriptive message.
git push origin <branch> - Pushes committed changes to the remote branch.
git push -u origin <branch> - Pushes changes and sets tracking.
git remote add origin <remote_repo_url> - Adds a remote repository connection.
git remote -v - Lists all remote repositories.
3. Viewing History and Changes
git log - Displays commit history.
git log -n 2 / --max-count=2 - Shows only the last 2 commits.
git log --author="Author Name" - Filters commit history by author.
git log --before="YYYY-MM-DD" - Shows commits before a specific date.
git log --after="YYYY-MM-DD" - Shows commits after a specific date.
git log --grep="commit reason" - Filters commits by message content.
git log --oneline - Shows a concise one-line summary.
git log --reverse - Shows commit history from oldest to newest.
git log <commit_id1>..<commit_id2> - Shows commits in a range.
git log --follow -p <file> - Shows history for a specific file with changes.
git log --stat - Shows summary of changes per commit.
git show <commit_id> - Displays details of a specific commit.
git show HEAD - Shows details of the most recent commit.
git show HEAD~2 - Shows details of the commit 2 steps back.
git diff - Shows unstaged changes.
git diff --staged - Shows staged changes vs last commit.
git diff <commit_id1> <commit_id2> - Compares changes between commits.
git diff HEAD~2..HEAD - Shows changes from 2nd last commit to current.
git diff <commit_id> -- <file> - Shows changes for a file.
git blame <file> - Shows author of each line.
git shortlog -sn - Lists authors and commit counts.
4. Branching
git branch - Lists all local branches.
git branch -a - Lists all branches (local and remote).
git branch <branch_name> - Creates a new local branch.
git checkout <branch_name> - Switches to the specified branch.
git checkout -b <branch_name> - Creates and switches to a new branch.
git switch <branch_name> - Switches to the specified branch.
git branch -d <branch_name> - Deletes a local branch.
git diff <branch1> <branch2> - Compares differences between branches.
git branch -r - Lists remote branches.
git push origin --delete <branch_name> - Deletes a remote branch.
5. Merging and Conflicts
git merge <branch_name> - Merges specified branch into current.
git merge origin/master - Merges remote master into local branch.
git pull origin <branch> - Fetches and merges remote changes.
git fetch - Fetches changes without merging.
git fetch origin - Fetches updates from origin.
git pull --rebase - Pulls and rebases local commits.
git rebase <branch_name> - Rebases current branch onto specified.
git cherry-pick <commit_id> - Applies a commit from another branch.
git stash - Temporarily saves changes to stash.
git stash pop - Applies and removes last stash.
git stash list - Lists all stashes.
git stash apply - Applies last stash without removing.
git reset --soft <commit_id> - Resets to commit, keeps changes staged.
git reset --mixed <commit_id> - Resets to commit, keeps changes in working directory.
git reset --hard <commit_id> - Resets and discards all changes.
git push --force - Force-pushes local changes.
git revert <commit_id> - Creates new commit to reverse changes.
git revert HEAD~3 - Reverts last 3 commits.
6. Tagging and Releases
git tag <tag_name> - Creates a lightweight tag.
git tag -a <tag_name> -m "Tag message" - Creates an annotated tag.
git push origin <tag_name> - Pushes a tag to remote.
git push origin --tags - Pushes all tags to remote.
7. Ignoring Files and Aliases
.gitignore - Adds patterns for files/folders to ignore.
git config --global alias.<alias> <command> - Creates a Git alias.
8. Remote and Forking Workflow
git remote add upstream <original_repo_url> - Adds upstream remote.
git pull upstream master - Pulls updates from original repo.
git push origin <branch> - Pushes local branch to remote for PR.
9. Viewing Merged Branches
git branch --merged - Lists branches merged into current.
git branch --no-merged - Lists branches not merged.
10. Tree and Project Structure
tree / git ls-tree -r <commit_id> - Displays directory tree structure.
11. Undoing and Resetting
git clean -fd - Removes untracked files and directories.
git checkout -- <file> - Discards changes in a file.