Skip to content

Instantly share code, notes, and snippets.

@susimsek
Last active February 18, 2025 10:36
Show Gist options
  • Save susimsek/805d4e7763651adb8ee05490b7b406b1 to your computer and use it in GitHub Desktop.
Save susimsek/805d4e7763651adb8ee05490b7b406b1 to your computer and use it in GitHub Desktop.
Git Command Cheatsheet

Git Command Cheatsheet


Overview

This worksheet provides a well-structured list of essential Git commands along with their descriptions and practical usage examples specifically for GitHub workflows.


Git Commands Table

Command Description GitHub Usage Example
git init Initializes a new Git repository. git init inside a new project directory to start version control.
git clone <repo_url> Clones an existing repository. git clone https://github.com/user/repo.git to get a copy of a remote repo.
git status Shows the working directory status. Run git status to check which files are modified before committing.
git add <file> Adds the specified file to the staging area. git add README.md to stage the README file.
git commit -m "Message" Commits the staged changes. git commit -m "Added new feature" to save changes.
git push origin <branch> Pushes local changes to the remote repository. git push origin main to upload changes to GitHub.
git pull origin <branch> Pulls and merges changes from the remote repository. git pull origin main to update your local repo with the latest changes.
git branch Lists available branches. git branch to see all branches in the repository.
git checkout <branch> Switches to the specified branch. git checkout feature-branch to move to another branch.
git merge <branch> Merges the specified branch into the current branch. git merge develop to merge develop into your current branch.
git rebase <branch> Reapplies commits on top of another base branch. git rebase main to integrate changes from main interactively.
git stash Temporarily saves changes in the working directory. git stash to store uncommitted changes without committing.
git stash pop Restores the stashed changes. git stash pop to retrieve saved changes.
git log Displays the commit history. git log --oneline to see a compact commit history.
git reset --hard <commit_id> Resets the repository to a specific commit and discards changes. git reset --hard HEAD~1 to undo the last commit.
git revert <commit_id> Reverts a specific commit by creating a new commit. git revert abc123 to undo a specific commit without deleting history.
git fetch Retrieves changes from a remote repository but does not merge. git fetch origin to see the latest changes before merging.
git remote -v Lists remote repositories. git remote -v to verify remote connections to GitHub.
git tag <tag_name> Creates a new tag. git tag v1.0 to create a version tag.
git diff Shows changes between commits, branches, or the working directory. git diff HEAD to see differences between last commit and working directory.
git blame <file> Shows who modified each line in a file. git blame main.js to see who last modified each line.
git cherry-pick <commit_id> Applies a specific commit to the current branch. git cherry-pick abc123 to apply changes from another commit.
git rm <file> Removes a file from tracking in Git. git rm old_file.txt to delete a file from the repository.
git config --global user.name "Your Name" Sets the global username. git config --global user.name "John Doe" to configure GitHub user settings.
git config --global user.email "[email protected]" Sets the global email address. git config --global user.email "[email protected]" for commit identity.
git checkout -b <new_branch> Creates and switches to a new branch. git checkout -b feature-branch to start working on a new feature.
git restore <file> Restores a file to its last committed state. git restore index.js to undo uncommitted changes in a file.
git reset <file> Unstages a staged file. git reset HEAD README.md to unstage a file before committing.

Summary

This Git Worksheet provides a well-organized reference for common Git commands along with GitHub-related usage examples. Whether you are working on a personal project or collaborating in a team, these commands will help you efficiently manage your repositories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment