Git CheatSheet
SETUP and user configuration
git config --global user.name “[firstname lastname]”
git config --global user.email “[valid-email]”
git config --global color.ui auto
Working with Branches
Command Function
git branch Lists all branches in the repository.
git branch Creates a new branch.
<branch-name>
git checkout Switches to the specified branch.
<branch>
git merge <branch> Merges the specified branch into the current branch.
Staging and Commit Commands
Command Function
git add <file> Adds changes in a file to the staging
area.
git add . Stages all changes in the repository.
git commit -m "<message>" Commits staged changes with a
message.
git commit --amend Amends the most recent commit.
Working with Remote Repositories
Command Function
git remote -v Displays remote repositories.
git remote add <name> Adds a new remote repository.
<url>
git push <remote> <branch> Pushes changes to a remote repository.
git pull <remote> <branch> Fetches and merges changes from a
remoterepository.
git fetch <remote> Fetches changes from a remote repository without
merging.
Undoing Changes
Command Function
git reset <file> Unstages a file.
git reset --hard Resets the working directory to the last commit.
git checkout -- <file> Discards changes in the working directory.
git revert <commit> Reverts a specific commit.
Git Stash Commands
Command Function
git stash Stashes changes in the working directory.
git stash list Lists all the stashed changes.
git stash apply Applies the latest stashed changes.
git stash drop Drops a specific stash.
Aspect git stash git commit
Purpose Temporarily saves your uncommitted Saves your changes permanently
changes (e.g., for later use) without in the commit history with a specific
affecting your commit history. message.
Use Case When you need to temporarily set When you're ready to finalize and
aside changes to switch branches, pull document the changes as part of
updates, or do other work. the repository's history.
Visibility Stashed changes are private to you; Commits are public and become
they don't appear in the repository part of the repository's history.
history.
Data Storage Stores changes in a "stash" list that can Stores changes in the repository's
be reapplied or discarded later. history, which is synchronized with
remote repositories.
Command git stash, git stash apply, git git commit -m "<message>",
Examples stash list, git stash drop git commit --amend