DEV Community

Adil Shahzad
Adil Shahzad

Posted on

Git Cheat sheet for Beginners

This Git cheat sheet saves you time when you just can't remember what a command is or don't want to use git help in the command line. It is hard to memorize all the important Git commands by heart, so this can help you when you get stuck.

Setup

git --version To check the version of git
which git See where Git is located
git help terminal-based documentation
cd Navigate to the directories using terminal
ls list files and directories
ls -A list hidden files and directories
mkdir Creating a new directory using terminal
touch readme.txt creating new file

Git configuration

Configuring user information used across all local repositories

git config --global user.name "First name last name"
Enter fullscreen mode Exit fullscreen mode
git config --global user.email "[valid-email"
Enter fullscreen mode Exit fullscreen mode

Git basics

git init initialize an existing directory as a Git repository
git status show modified files in the working directory, staged for your next commit
git add [filename] Stage all changes in for the next commit.
git commit -m "Describe Message" commit your staged content as a new commit snapshot

Branches

git branch List all branches
git branch newbranch Create a new branch
git checkout newbranch switch to another branch and check it out into your working directory
git checkout -b newbranch Create and switch to new branch at the same time
git branch -m branchname new_branchname Renaming a branch
git branch -d branchname Delete merged branch (only possible if not HEAD)
git branch -D branch_to_delete Delete not merged branch

Merge

git merge branchname Merge the specified branch’s history into the current one
git merge --ff-only branchname Merge to master only if fast forward
git merge --abort Stop merge in case of merge conflicts
git cherry-pick 073791e7 Merge a Specific commit
git checkout branchname » git rebase master Rebase the current branch onto master
git rebase --abort Cancel rebase
git rebase -i HEAD~3 Squash Multiple commits into one

Git log

git log show all commits in the current branch’s history
git log --oneline Show oneline-summary of commits
git log -p show changes
git log --stat --summary Show stats and summary of commit
git log --graph Show history of commits as graph
git log --oneline --graph --all --decorate Show history of commits as graph-summary

Compare

git diff To compare modified files
git diff --staged To compare modified files within the staging area
git diff master..branchname To compare branches
git diff 6eb715d Compare with the previous commit
git diff --ignore-space-change 6eb715d..HEAD to compare without caring about spaces:
git diff --ignore-all-space 6eb715d..HEAD To compare without caring about all spaces

Make sure you replace this commit 6eb715d with your commit ID

Stash

git stash Save modified and staged changes
git stash save "Message" Put in the stash with message
git stash list list stack-order of stashed file changes
git stash show stash@{0} Show stash stats
git stash pop write working from the top of stash stack
git stash branch new_branch Create branch from stash
git stash drop discard the changes from the top of stash stack

Tags

git tag Show all released versions
git tag -l -n1 Show all released versions with comments
git tag v1.0.0 Create release version
git tag -a v1.0.0 -m "Message" Create release version with comment
git checkout v1.0.0 Checkout a specific release version

Reset

git revert 073791 Go back to commit
git reset --soft 073791 Soft reset (move HEAD only; neither staging nor working directory is changed)
git reset --soft HEAD~ Undo the latest commit

Remote

git remote add origin [repo-url] Adding remote origin to local repository
git remote -v Show remote details
git remote rm origin Remove origin
git branch -M main Renaming a branch
git push -u origin master Push changes to the remote repository
git pull Pulling changes from the remote repository
git pull origin branchname Pull specific branch
git clone [repo-url] Cloning a local repository
git push origin --delete branchname Deleting branch from the remote origin

Top comments (0)