DEVELOPERS
<SOCIETY/>
GIT
CHEAT
SHEET
More Such Notes Uploded on WhatsApp
Tap here to Join our
WhatsApp Channel
SETUP & INIT STAGE & SNAPSHOT
Configuring user information, Working with snapshots
initializing and cloning and the Git staging area.
repositories.
$ git init $ git status
Initialize an existing directory as Show modified files in working directory,
a Git repository. staged for your next commit.
git init Git Status
Normal Directory Git Repository
Working Staged Unstaged
Directory Snapshot Snapshot
$ git clone [url]
Retrieve an entire repository
from a hosted location via URL. $ git add [file]
Add a file as it looks now to your
next commit (stage).
Remote
Repository at [URL]
git clone [URL] git clone [URL] git add [file]
</> </>
(Stage)
Working Staging
Directory Area
Local Copy of the Local Copy of the
Repository Repository
01 Git Cheatsheet
$ git reset [file] $ git commit -m “[descriptive message]”
Unstage a file while retaining the changes Commit your staged content as
in working directory. a new commit snapshot.
(Stage)
git add [file]
Working Staging Git Repository
git reset [file] Directory Area (Commit History)
Working (Unstage) Staging
Directory Area
git add
$ git diff git commit
Diff of what is changed but not staged
= BRANCH & MERGE
Working git diff Staging Isolating work in branches, changing
Directory Area
context, and integrating changes.
$ git branch
git diff --staged List your branches. A * will appear next
to the currently active branch.
Diff of what is staged but
not yet commited.
Branch 1
HEAD *Main Branch
=
Staging Commit
git diff --staged
Area History Branch 2
02 Git Cheatsheet
$ git branch [branch-name] $ git merge [branch]
Create a new branch at the current Merge the specified branchʼs history
commit. into the current one.
[branch-name] New Feature
git branch
Main Branch git merge
[branch-name]
Main Branch
Branch 2 Branch 2
$ git checkout $ git log
Switch to another branch and check it Add a file as it looks now to your
out into your working directory. next commit (stage).
Branch_1
Git Logs
*Main Branch
Git Repository
commit 21a67
Author : xyz
Date: Mon May 16 16:03:16 2022
commit 21a67 Commit Message
git checkout Branch_1
commit fb555
git log
Author : ABC
*Branch_1 commit fb555 Date: Tue May 17 09:05:45 2022
Main Branch Commit Message
commit 3ecd3
commit 3ecd3
Author : XYZ
Date: Sun May 22 19:45:34 2022
Commit Message
03 Git Cheatsheet
INSPECT & COMPARE
Configuring user information,
initializing and cloning repositories.
$ git log branchB..branchA $ git diff branchB...branchA
Show the commits on branchA that Show the diff of what is in branchA
are not on branchB. that is not in branchB.
Git Repository branchA
Git Logs
commit 181a9 hA
Author : XYZ a nc
.. br
B.
Date: Mon May 16 16:03:16 2022
h
commit 181a9 git log
Commit Message
a nc branchB
branchB
commit c7eaf
br
ff
...
t di
branchA Author : ABC gi
commit c7eaf Date: Tue May 17 09:05:45 2022
Commit Message
branchB branchA
$ git log --follow [file] $ git show [SHA]
Show the commits that changed file, Show any object in Git in
even across renames. human-readable format.
commit 715c3
commit 715c3
Author : XYZ
Date: Mon May 16 16:03:16 2022
git show 715c3 Commit Message
File1
File1 Changes
File2
File2 Changes
...
04 Git Cheatsheet
TRACKING PATH CHANGES $ git log --stat -M
Versioning file removes and Show all commit logs with indication
path changes. of any paths that moved.
$ git rm [file]
Delete the file from the project and
stage the removal for commit. IGNORING PATTERNS
Preventing unintentional staging
git rm [file] or commiting of files.
+
(Stage the removal)
logs/
*.notes
Git Repository pattern*/
Save a file with desired patterns as
.gitignore with either direct string matches
or wildcard globs.
$ git mv [existing-path] [new-path]
Change an existing file path and
stage the move. .gitignore
logs/
*.notes
git mv [file] pattern*/
+
(Stage the removal)
Git Repository $ git config --global core.excludesfile [file]
System wide ignore pattern for all
local repositories
05 Git Cheatsheet
SHARE & UPDATE $ git merge [alias]/[branch]
Retrieving updates from another Merge a remote branch into your current
repository and updating local repos. branch to bring it up to date.
$ git remote add [alias] [url] Main
Remote Repository
Add a git URL as an alias.
git merge origin/main
origin/main
git remote add new merge
[alias] [URL]
commit
Local Repository
Remote Git Local Git
Repository at [URL] Repository
after merge
Main
$ git fetch [alias] $ git push [alias] [branch]
Fetch down all the branches from Transmit local branch commits to
that Git remote. the remote repository branch.
new merge commit
Main
Remote Repository remote/main
after merge
git fetch origin/main
Remote Repository
origin/main
merge
git push
Local Repository
Local Repository
Main
local/main
06 Git Cheatsheet
$ git pull $ git reset --hard [commit]
Fetch and merge any commits Clear staging area, rewrite
from the tracking remote branch. working tree from specified
commit.
remote origin/main git reset --hard HEAD~1
git pull
head head
Local Repository
new merge
commit
local/main Rewriting Staging Index
after merge & Working Directory
REWRITE HISTORY TEMPORARY COMMITS
Rewriting branches, updating Temporarily store modified, tracked
commits and clearing files in order to change branches.
history.
$ git rebase [branch] $ git stash
Apply any commits of the
Save modified and staged changes.
current branch ahead of
specified one.
git rebase
Feature
git stash
main Working Directory Staged Changes
(Unstaged
Changes)
07 Git Cheatsheet
$ git stash list $ git stash pop
Write working from the top
List stack-order of stashed file changes. of the stash stack.
git stash pop
git stash list
git stash Apply
stash@(0): WIP on master: fd3aab8 done stash@{3}
Working
git stash Directory
stash@(1): WIP on master: fd3aab8 done stash@{3
}
stash@{2
git stash
stash@(2): WIP on master: fd3aab8 done }
stash@{1
Working Stash
} List
Directory
stash@{0
}
$ git stash drop
Discard the changes from the
top of the stash stack.
git stash drop
stash@{3}
(Discard)
stash@{3}
stash@{2}
stash@{1}
stash@{0}
Stash List
08 Git Cheatsheet
DEVELOPERS
<SOCIETY/>
Tap here
to Join our
WhatsApp Channel
5000+ Free Content
Members E-Books, Courses,
Notes & Job Updates
More Such Notes Uploded on WhatsApp