Skip to main content

Git Cheatsheet

A quick reference guide to the most commonly used Git commands.


Repository Setup

CommandDescription
git initInitialize a new Git repository locally.
git clone <repo-url>Clone a remote repository to your local machine.

Working with Changes

CommandDescription
git statusCheck the status of changes in the working directory.
git add <file>Stage a specific file for commit.
git add .Stage all changes.
git commit -m "message"Commit staged changes with a descriptive message.
git logView commit history.
git diffShow changes between working directory and last commit.

Working with Remote Repositories

CommandDescription
git remote add origin <repo-url>Connect a local repository to a remote one.
git push origin mainPush changes to the main branch of the remote repository.
git pull origin mainFetch and merge changes from the remote repository.
git fetchRetrieve changes from the remote repository without merging.

Branching and Merging

CommandDescription
git branchList all branches.
git branch <branch-name>Create a new branch.
git checkout <branch-name>Switch to an existing branch.
git checkout -b <branch-name>Create and switch to a new branch.
git merge <branch-name>Merge changes from another branch into the current branch.
git rebase <branch-name>Reapply commits on top of another base branch.

Undoing Changes

CommandDescription
git reset --soft <commit-hash>Reset to a previous commit but keep changes staged.
git reset --hard <commit-hash>Reset to a previous commit and discard all changes.
git revert <commit-hash>Undo a commit by creating a new commit that reverses it.
git checkout -- <file>Discard local changes to a specific file.

Other Useful Commands

CommandDescription
git stashTemporarily save changes without committing.
git stash popReapply stashed changes.
git tag <tag-name>Create a tag for a specific commit.
git show <commit-hash>Display detailed information about a commit.

For more advanced Git usage, refer to the official documentation: Git Documentation.