Maybe you are a beginner with Git like me, and you found it weird to memorize all those commands (Especially if you don't like to memorize a lot of things like me), and know what every command does, So here is a list of The essential Git commands every developer must know.
In this article you will see:
- Creating snapshots
- Browsing history
- Branching & merging
- Collaboration using Git & GitHub
- Rewriting history
Creating Snapshots
Initializing a repository
git init
Staging files
Stages a single file
git add file1.js
Stages multiple files
git add file1.js file2.js
Stages with a pattern
git add \*.js
Stages the current directory and all its content
git add .
Viewing the status
Full status
git status
Short status
git status -s
Committing the staged files
Commits with a one-line message
git commit -m "Message"
Opens the default editor to type a long message
git commit
Skipping the staging area
git commit -am "Message"
Removing files
Removes from working directory and staging area
git rm file1.js
Removes from staging area only
git rm --cached file1.js
Renaming or moving files
git mv file1.js file1.txt
Viewing the staged/unstaged changes
Shows unstaged changes
git diff
Shows staged changes
git diff --staged
Same as the above
git diff --cached
Viewing the history
Full history
git log
Summary
git log --oneline
Lists the commits from the oldest to the newest
git log --reverse
Viewing a commit
Shows the given commit
git show 921a2ff
Shows the last commit
git show HEAD
Two steps before the last commit
git show HEAD~2
Shows the version of file.js stored in the last commit
git show HEAD:file.js
Unstaging files (undoing git add)
Copies the last version of file.js from repo to index
git restore --staged file.js
Discarding local changes
Copies file.js from index to working directory
git restore file.js
Restores multiple files in working directory
git restore file1.js file2.js
Discards all local changes (except untracked files)
git restore .
Removes all untracked files
git clean -fd
Restoring an earlier version of a file
git restore --source=HEAD~2 file.js
Browsing History
Viewing the history
Shows the list of modified files
git log --stat
Shows the actual changes (patches)
git log --patch
Filtering the history
Shows the last 3 entries
git log -3
git log --author="Mosh"
git log --before="2020-08-17"
git log --after="one week ago"
Commits with "GUI" in their message
git log --grep="GUI"
Commits with “GUI” in their patches
git log -S"GUI"
Range of commits
git log hash1..hash2
Commits that touched file.txt
git log file.txt
Formatting the log output
git log --pretty=format:"%an committed %H"
Creating an alias
git config --global alias.lg "log --oneline"
Viewing a commit
git show HEAD~2
Shows the version of file stored in this commit
git show HEAD~2:file1.txt
Comparing commits
Shows the changes between two commits
git diff HEAD~2 HEAD
Changes to file.txt only
git diff HEAD~2 HEAD file.txt
Checking out a commit
Checks out the given commit
git checkout dad47ed
Checks out the master branch
git checkout master
Finding a bad commit
git bisect start
Marks the current commit as a bad commit
git bisect bad
Marks the given commit as a good commit
git bisect good ca49180
Terminates the bisect session
git bisect reset
Finding contributors
git shortlog
Viewing the history of a file
Shows the commits that touched file.txt
git log file.txt
Shows statistics (the number of changes) for file.txt
git log --stat file.txt
Shows the patches (changes) applied to file.txt
git log --patch file.txt
Finding the author of lines
Shows the author of each line in file.txt
git blame file.txt
Tagging
Tags the last commit as v1.0
git tag v1.0
Tags an earlier commit
git tag v1.0 5e7a828
Lists all the tags
git tag
Deletes the given tag
git tag -d v1.0
Branching & Merging
Managing branches
Creates a new branch called bugfix
git branch bugfix
Switches to the bugfix branch
git checkout bugfix
Same as the above
git switch bugfix
Creates and switches
git switch -C bugfix
Deletes the bugfix branch
git branch -d bugfix
Comparing branches
Lists the commits in the bugfix branch not in master
git log master..bugfix
Shows the summary of changes
git diff master..bugfix
Stashing
Creates a new stash
git stash push -m "New tax rules"
Lists all the stashes
git stash list
Shows the given stash
git stash show stash@{1}
Shortcut for stash@{1}
git stash show 1
Applies the given stash to the working dir
git stash apply 1
Deletes the given stash
git stash drop 1
Deletes all the stashes
git stash clear
Merging
Merges the bugfix branch into the current branch
git merge bugfix
Creates a merge commit even if FF is possible
git merge --no-ff bugfix
Aborts the merge
git merge --squash bugfix
Viewing the merged branches
Shows the merged branches
git branch --merged
Shows the unmerged branches
git branch --no-merged
Rebasing
Changes the base of the current branch
git rebase master
Cherry picking
Applies the given commit on the current branch
git cherry-pick dad47ed
Collaboration
Cloning a repository
git clone url
Syncing with remotes
Fetches master from origin
git fetch origin master
Fetches all objects from origin
git fetch origin
Shortcut for "git fetch origin"
git fetch
Fetch + merge
git pull
Pushes master to origin
git push origin master
Shortcut for "git push origin master"
git push
Sharing tags
Pushes tag v1.0 to origin
git push origin v1.0
git push origin —delete v1.0
Sharing branches
Shows remote tracking branches
git branch -r
Shows local & remote tracking branches
git branch -vv
Pushes bugfix to origin
git push -u origin bugfix
Removes bugfix from origin
git push -d origin bugfix
Managing remotes
Shows remote repos
git remote
Adds a new remote called upstream
git remote add upstream url
Remotes upstream
git remote rm upstream
Rewriting History
Undoing commits
Removes the last commit, keeps changed staged
git reset --soft HEAD^
Unstages the changes as well
git reset --mixed HEAD^
Discards local changes
git reset --hard HEAD^
Reverting commits
Reverts the given commit
git revert 72856ea
Reverts the last three commits
git revert HEAD~3..
git revert --no-commit HEAD~3..
Recovering lost commits
Shows the history of HEAD
git reflog
Shows the history of bugfix pointer
git reflog show bugfix
Amending the last commit
git commit --amend
Interactive rebasing
git rebase -i HEAD~5
Btw, This article is taken from the Git Cheat Sheet
From The Mosh Course You can download the original file HERE as a PDF file.