You are on page 1of 9

$ git branch

$ git logs

GIT
CHEAT
SHEET
$ git fetch

$ git status
SETUP & INIT STAGE & SNAPSHOT
Configuring user information, Working with snapshots and the
initializing and cloning repositories. Git staging area.

$ git init $ git status

Initialize an existing directory as a Show modified files in working directory,


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 Commit your staged content as a


changes in working directory. new commit snapshot.

(Stage)
git add [file]
Working Staging Git Repository
Directory Area (Commit History)
git reset [file]
Working Staging
(Unstage) Area
Directory

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 Merge the specified branchʼs history


current 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 A
ch
Author : XYZ an
Date: Mon May 16 16:03:16 2022 ...br
Commit Message c hB
commit 181a9 git log an branchB
branchB br
... commit c7eaf iff
td
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 config --global core.excludesfile [file]


Git Repository

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 from Clear staging area, rewrite working
the tracking remote branch. 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 history. files in order to change branches.

$ git rebase [branch] $ git stash

Apply any commits of the current


Save modified and staged changes.
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 of


List stack-order of stashed file changes.
the stash stack.

git stash pop


git stash list

git stash Apply


stash@(0): WIP on master: fd3aab8 done stash@{3}

Working
git stash stash@{3} Directory
stash@(1): WIP on master: fd3aab8 done
stash@{2}
stash@{1}
git stash
stash@(2): WIP on master: fd3aab8 done stash@{0}

Working Stash List


Directory

$ 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

You might also like