Navigation :
Git Cheat Sheet
Git Cheat Sheet
Basic Commands
git init
: Initializes a new Git repository in the current directory.git clone <url>
: Clones an existing repository from a remote location (e.g., GitHub).git add <file>
: Stages changes in a file to be included in the next commit.git add .
: Stages all changes in the current directory.
git commit -m "<message>"
: Creates a new commit with the specified message.git status
: Shows the current state of the working directory and staging area.git log
: Displays the commit history.git log --oneline
: Displays commit history in a concise format.
git diff
: Shows the differences between the working directory and the last commit.git diff --cached
: Shows the differences between the staged changes and the last commit.
Branching and Merging
git branch
: Lists all local branches.git branch <branch_name>
: Creates a new branch.git checkout <branch_name>
: Switches to the specified branch.git checkout -b <branch_name>
: Creates a new branch and switches to it.git merge <branch_name>
: Merges the specified branch into the current branch.git branch -d <branch_name>
: Deletes the specified branch.
Remote Repositories
git remote add origin <url>
: Adds a remote repository named “origin” to the current repository.git push
: Pushes the current branch to the remote repository.git pull
: Fetches and merges changes from the remote repository.
Other Useful Commands
git reset --soft HEAD^
: Unstage all changes for the last commit.git reset --hard HEAD^
: Discard all changes since the last commit.git revert <commit_hash>
: Creates a new commit that reverts the specified commit.git stash
: Temporarily stores uncommitted changes.git stash pop
: Restores the most recently stashed changes.
Configuration
git config --global user.name "Your Name"
: Globally configure your name.git config --global user.email "your_email@example.com"
: Globally configure your email.