You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository serves as a cheat sheet for common Git commands and best practices. Whether you're a beginner looking to get started with Git or an experienced developer wanting to brush up on your Git skills, you'll find useful information here.
Discard all changes in your working directory and revert the files to the last committed state.
-
Merging
Command
Description
Example(s)
git merge <branch-name>
Merge a branch
git merge feature-branch
git merge <source-branch> <target-branch>
Merge a branch into target branch
git merge feature-V1 feature-V0
git merge --squash <branch-name>
Merge a branch with all commits squashed into single commit
git merge --squash feature-V0
git rebase <branch-name>
Merge a branch without creating new merge commit, keeps work history clean
git rebase feature-branch
Reviewing
Command
Description
Example(s)
git status
Displays the current status of your working directory and staged changes
-
git log
Displays a detailed commit history of the current branch
-
git log --oneline
Displays a simplified commit history of the current branch in a compact format
-
git diff
Shows the differences between your working directory and the most recent commit
-
git diff <commit1> <commit2>
Compares two commits and displays the differences between them
git diff abc123 xyz987
Stashing
Command
Description
Example(s)
git stash
Store modified and staged changes
-
git stash save "comment"
Stash the modified changes with description
git stash save "Work in Progress - Feature 1"
git stash list
List all stashes
-
git stash apply
Applies the most recent stash entry to your working directory, without deleting the stash entry
-
git stash pop
Applies the most recent stash entry to your working directory and deletes it
-
git stash show
Show the difference summary of recent stash entry
-
git stash drop
Delete the recent stash entry without applying it
-
git stash clear
Delete all stash entries
-
Git commands related to remote repositories
Command
Description
Example(s)
git push
Push changes to remote repository
-
git pull
Fetch the latest changes from the remote repository and merge them with the local one
-
git push origin <branch-name>
Push a branch to the remote repository
git push origin feature-branch
git push origin --delete <branch-name>
Delete a remote branch
git push origin --delete feature-branch
git fetch origin
Fetch the changes from remote repository without merging
-
git fetch origin <branch-name>
Fetch changes from a remote branch without merging
git fetch origin main
git remote
View all remote connections
-
git remote remove <connection-name>
Remove a connection
-
git remote rename <old-name> <new-name>
Rename a connection
-
git pull --rebase origin
Rebase local changes on top of new changes from remote repository
-
Reverting and Resetting Commands
Command
Description
Example(s)
git revert <commit>
Creates a new commit that undoes the changes introduced by the specified commit
git revert abc123
git revert <commit1> <commit2> ... <commit n>
Reverts a range of commits, creating new commits that undo the changes introduced by each of them.
git revert abc123 xyz987
git reset --soft <commit>
Moves the branch pointer to the specified commit, preserving the changes in your working directory and staging area.
git reset --soft abc123
git reset --hard <commit>
Moves the branch pointer to the specified commit and discards all changes, both in the working directory and staging area (Deletes all commits ahead of it).
git reset --hard abc123
git revert <commit>
Creates a new commit that undoes the changes introduced by the specified commit
git revert abc123
git rm <file>
Remove file from Working Directory and staging area
Amends the most recent commit with the new changes
-
git show <commit> --stat
Displayed a histogram showing inserts, deletions and modifications per file for a specific commit along with general commit information
-
git bisect
A powerful Git command used to find the commit that introduced a bug or regression in your codebase. It performs a binary search through the commit history to identify the specific commit where the issue was introduced.
git bisect start , git bisect good , git bisect bad
git commit --amend
Amends the most recent commit with the new changes