DEV Community

Cover image for Basic Git cheat sheet (yet another)
Gourav Singh Rawat
Gourav Singh Rawat

Posted on • Edited on

Basic Git cheat sheet (yet another)

Yet another Git cheat sheet.

This sheet only contains the basics of git like committing, viewing, cloning, removing, etc
Well to guys who gonna comment down, look at this duuuude...posting another cheat sheet, just move on bro.
Anyways I hope you'll like this cheat sheet <3

Creating Snapshots

Initializing a Repository
Initializing - git init

Staging Changes/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 .

Unstaging Changes/Files
Copies the last version from repo - git restore --staged file.js

Viewing Status
Full status - git status
Short status - git status -s

Viewing the staged/unstaged changes
Shows unstaged changes - git diff
Shows staged changes - git diff --staged

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 (first old name, new name)

Browsing History

Viewing history
Full history - git log
Full history from oldest to newest - git log --reverse
Shows the list of last modified files - git log --stat
Shows the actual changes - git log --patch

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

Filtering history
Show last 3 changes - git log -3
Filter by author - git log --author="Gourav"
Filter by date - git log --before="2022-01-20"
Filter by files modified (commit in which there are changes in file.txt) - git log file.txt
Show Commits with “Fixed” in their message - git log --grep=“Fixed”

Branching & Merging

Managing branches
Creates a new branch called alpha - git branch alpha
Switching branch to alpha - git switch alpha
Deleting branch alpha - git branch -d alpha

Merging
Merges the alpha branch into the current branch - git merge alpha
Aborting merge - git merge --abort

Viewing the merged branches
Shows the merged branches - git branch --merged
Shows the unmerged branches - git branch --no-merged

If you think there's something wrong or I missed related to these types, please comment below. Thanks.
Hope you found this helpful <3

Top comments (1)

Collapse
 
henryong92 profile image
Dumb Down Demistifying Dev

Noice