Git Flashcards
What am I going to learn?
Everything I need to know to use Git like a pro
Work effectively with others
Fundamental concepts
Creating snapshots of code
browsing project history
collaborating using Github
rewriting history
What is Git and why is it popular?
Hint: track history, work together

most popular version control system in the world
More than 90% of software projects globally use Git
Almost every job description for software developer mentions Git
records changes made to code over time in a repository
can look at project history to see who made which changes when
can revert project back to earlier state

What is a centralized version control system?
All team members connect to central server to get latest code + share code
Problems?
Single point of failure
if server goes down, cannot collaborate
must wait for server to go back up to work together and save snapshots of code

What is a de-centralized version control system?
every team member has a copy of the project with it’s history on their machine
we can save snapshots of the project locally, on our machine
if server is offline, can synchronize directly with others
What must be in your toolbox if you want to get hired as a software developer?
Know the in’s and out’s of Git
How it works
How to track project history
How to work together effectively
How can we use Git?
Command line
fastest way, many people use
this course will focus on command line
Code Editors and IDEs
built in support for Git features w/ plugins for special features (GitLens, VS code, etc.)
Graphical user interfaces
tools for windows, mac, windows, Linux
GitKraken (GUI for Git) integrates with GitKraken boards (error tracking), timelines (project roadmaps)
have limitations, not always available
What are the different levels we can config Git?
System - all users
Global - all repos of the current user
Local - current repo
We can have different settings for different projects or repositories

What settings do we need to specify?
Name, Email, Default Editor
Line Ending (auto.crlf)

Why do we have to handle “end of line” settings properly?

Windows and Mac have different end of line characters
If we don’t setup line ending properly, will end up with issues down the road
Solution?
Configure variable corecrlf (carriage return line feed)
Windows Users (Git)
remove carriage return line characters (check into repo)
add carriage return line characters (check out repo)
Mac Users
don’t add carriage return characters
remove carraige return line characters if present before (check into repo)
Input - only modify end of lines, when storing into repository

How do we create a new repository?
Commands
$mkdir RepoName
$cd RepoName
$git init
Under the Hood
(implementation detail)
creates a .git file w/in directory (hidden)
.git file stores info about our project
project history, branches, hooks, objects, info, references
purely implementation details
don’t corrupt this file (will lose project history)

What is the git workflow?

Working Directory (Project Directory)
local to my machine
Index (Staging area)
.add command
intermediate step
review code before commit
Git Repository
subdirectory (hidden) in working directory

What does each commit contain?
Hint: complete snapshot
each commit contains…
a unique identifier
generated by git
information about change
Message
Date/Time
Author
complete snapshot of project (not only changes)
can revert to earlier version quickly without computing changes

What is the echo command?
A standard Unix / Linux command for writing content to a file
echo hello > file1.txt

What does $git status show us?
see status of working directory or staging area

How do we add files to staging area?
git add * .txt
all files with .txt ending
git add file1.txt file2.txt
multiple files separated by space
git add .
adds all files in current directory
beware some files we don’t want to add (.gitignore)

If we stage a file, then make a change locally, what must we do?
$git add fileName.ext
add to staging area again
Why?
Git stores a snapshot of file when $git add . to staging area

How do we store our snapshot permanently in our git repository?
$git commit -m “meaningful snapshot description”
bug constraints explained

What are best practice guidlines for commits?

whole point of commits is to record checkpoints
if a mistake is made, can go back to last checkpoint
each commit represents a single unit of work
DO LIST
commit often (5-10 times per day, depending on work)
commit at each checkpoint
As I reach a state I want to record, THEN make a commit
each commit should represent a logically chained step
give each commit message a meaningful comment (messages show in history)
Use present tense in commit message
DO NOT DO
make a commit everytime a file is updated
wait three days, then make a commit
build a feature end to end before making a commit
Do not wait until BIG commit to make a commit
commit two logical steps together
Ex. don’t commit bug fix / typo fix together (separate these)
How do we skip staging?
99% of time, stage code before commiting
Beware (only due if sure, don’t need to review)
-a all
-m message
$git commit -am
How do we remove a file from working directory?
Hint: remove from working directory,
.add + commit to remove from staging directory
$rm file2.txt
linux cmmd removes from working directory
still in staging directory (ls)
Soln?
Must stage changes using add command
$git add file2.txt
stages deletion
$git commit -m

How do we remove a file from working directory and staging files?
$git rm file2.txt
How do we move or rename files?
Hint: $git mv newFileName oldFileName
$ mv file1.txt main.js
rename file1.txt to main.js
Two step process:
- Modify working directory
- Stages two changes (add / delete)
$git mv
does this for us vs. standard unix command
changes are applied to both working directory and staging directory
$git commit -m

How do we ignore files?

certain files we want to ignore:
log files: individual for each developer, don’t want to include
node_modules: increase size of code, etc.
Solution?
Don’t want to add to staging area, don’t want git to track these
.gitignore (in root of project)
Beware?
If you accidentally add a file to your repository
THEN, add to gitignore, it won’t work
How do we remove a file from the staging area that we don’t want git to track?
must remove from staging area (index)
$git rm
removes from working directory and staging area
$git rm –cached -r bin/
removes only from staging area (index)

What’s a shortcut for status?
Hint: git status -s

$git status -s
short version
less wordy
easier on the eyes
Symbols?
M - modified
A - added
left column
staging area
right column
working directory
Ex. red M -> modified a file in working directory, not yet in staging
green M -> some changes in staging area

How can we see the exact lines of code we’ve stagged?
Hint: $git diff –staged
Best practice - always review changes before commit
Terminal:
$git diff –staged
$git diff
compares what is in working directory vs. staged directory
visual dev tools (VSCode):
$git config –global diff.tool vscode
$git config –global difftool.vscode.cmd “code –wait –diff $LOCAL $REMOTE”

How do we setup VScode for viewing differences between files?
Hint: $git difftool –staged

$git difftool –staged
ex.
old copy in working directory
vs
old copy in staging area

How do we look at history?
Hint: $git log

$git log –oneline
newest commit on top
$git log –onlineline –reverse
initial commit on top

How do we see the content of a specific commit?
Hint: $git show ~HEAD1
$git show commit 7c6df80
$git show HEAD~#steps
$git show HEAD~1:.gitignore

How do we see all the files and directories in a commit (not only changes)?
Hint: $git ls-tree HEAD~1

directory in filesystem represented as a tree

each directory can have children
children can be files / sub directories
How do we remove a file from the staging area?
Hint: $git restore –staged file1.js
removes all changes from staging directory
only changes left in the working directory
Restore implementation?
grabs last copy from Git repository
puts that back into staging area (last commit)
if not in Git repository?
Removes from staging
reverts back to untracked file

How can we discard changes made in working directory ?
undue local changes
$ git restore .
will take previous version from staging area and restore that version in working directory
$ git clean -fd
removes untracked files from working directory

How do we restore a file to a previous version?
$git restore –source=HEAD~1 fileName
removes file from working directory and staging area
restore file to previous version vs undue commit

How do we create snapshots using VScode?

Open VScode
go to git extension

What is GitKraken?
A GUI tool for Git
can see all commits - author, date/time

What are we going to learn in the history section?

What does $git –oneline –stat do for us?

$git –oneline -stat
shows us all the files changed in each commit
$git –oneline –patch

How do we filter history of commits when looking at Git repository?

In real world, hundreds or thousands of commits
Don’t want to look through them all
filter by author, commit message, date, etc.
GIT COMMANDS:
$git log –oneline -3
returns last three commits
$git log –oneline –author=”arsalon”
returns commits made by author=arsalon
$git log –oneline –after=”2020-08-17”
$git log –oneline –after=”yesterday”
$git log –oneline –after=”one week ago”
returns commits made after specified date
git log –oneline –grep=”GUI”
case sensitive, returns commits with message containing “GUI” term

How can we filter history by all commits that have modified a specific function?
Hint: $git log –oneline -S”functionName”
Ex.
$ git log –oneline -S”getJwt” –patch
brings up all commits that modify getJwt ( ) and shows changes

How do we filter history by a range (within a range)?
Hint: $ git log –oneline commitIdentifier1…commitIdentifier2

Example
$ git log –oneline 56a3436..600400c
gives all commits after 56a3436 up to 600400c (included)

How do we customize the log output?
Hint: $git log –pretty=format:”%an commited %h on %cd”

$ git log –pretty=format:”%an commited %h on %cd”
%an - author name
%h - abbreviated commit identifier
%cd - commit date

What are git aliases?
Hint: $git config –global alias.unstage “restore –staged .”

$ git config –global alias.lg“log –pretty=format:’%Cgreen%an%Creset commited %Cred% %h%Creset% %Cblue% on %cd’“
Can abstract git commands into shorter alias

How do we see a specific commit?
Hint $git show HEAD~2

How can we see the list of file names that have been changed across commits?
Hint: $git diff HEAD~1 HEAD –name-only
$git diff HEAD~2 HEAD –name-status
shows status of files changed (modified, added, etc.)

How do we restore our working directory to a particular snapshot from a commit?
(ie. restore all files to a previous snapshot version)

$git checkout 6c325d5
working directory will look exactly like an earlier point in time

What is a detached head?

$git checkout master
restores head to master branch
git When we checkout a particular commit
head will point to that commit
Head isn’t pointing to a branch (master)
it’s pointing to a particular commit
DON’T MAKE CHANGES
ONLY LOOK AROUND

What is bisect?

GIT COMMANDS:
$git bisect start
$git bisect good ###
$git bisect bad ###
$git bisect reset
an incredibly powerful tool for finding bugs
can quickly find the commit that introduced the issue
How?
Give Git the bad commit code (one with bug)
give Git a good commit code (one without bug)
like binary search (log n )
ref/bisect/bad -> bad commit labeled
(Head) -> in middlepoint between two*
refs/bisect/good -> good commit
*working directory restored to middlepoint snapshot (head)
*run tests to see if issue is still there
*if issue is not there, tell bisect commit is good, continue

How do we view an authors contribution to a git repository?
Hint: $git shortlog

$git shortlog -n -s -e –before=”” –after ““
-n number of commits (filter author by)
-s suppress commit messages
-e show email address
-before/after to see contributors for certain date rangs

How do we view history of a file?
Hint: $git log –oneline –stat fileName.ext

$cd
$ls
$git log –oneline –stat index.js

How do we restore a deleted file?
$git checkout previousCommit pathName.ext
$git commit -m “restore pathName.ext”
How?
Look at parent of file, get ID
checkout only deleted file from commit
Situation?
remove file fixing a bug or implementing a new feature
want to restore the file

How do we find the author of a particular line of code?
$git blame filename.ext -L 1, 11

$git blame filename.ext -L 1,11
-L lines
1, 11 line 1 through 11
Part of troubleshooting issues, want to know who wrote a line of code
$git blame fileName.ext
in front of each line of code, additional information
commit ID
author name
date/time inserted
Ex.
git blame -e -L 1,11 authService.js

What are tags?
$git tag -a v1.1 -m “message”
$git tag -d v1.1

allow us to bookmark commits like a certain version
lightweight vs annotated tage
(light weight tag)
$git tag v1.1 commitID
a reference or pointer to particular commit
(annotated tag)
$git tag -a v1.1 -m “my version 1.1”
$git tag -n
creates annotated tag (object) with properties
can associate a message with tag
$git tag -d tagID
deletes tag

What is gitlens?

VSCode plugin
Pros
can view commits
can view diff
can compare commits
can see files changed
can search by messages, etc.
Cons
cannot search by multiple criterias
cannot use bisect to find bugs
Mosh Advice
GUI tools are great for diff
seeing how two files are different

How do we view history in GitKraken?
Can right click, add tags, checkout, view diffs
What is branching?
Hint: totally change how you develop software

One of the most powerful Git tools
the tools learned in this section
can totally change how I develop software
use branches
to work on a feature in isolation
like a separated isolated workspace

How do we use branches to develop features in isolation?
without messing with main line of work (master)
work on various work items (features)
after testing and verifying feature works
bring code back to master (merging)
What is a branch in git?
just a pointer to a commit
tiny file with a 40 char bit ID
Head tracks which branch currently working
Ex.
master branch is just pointer to last commit in main line of work
git moves this pointer forward automatically

How do we fix a bug?
$git branch bugfix
create a branch to fix bug
$git branch -m oldName newName
rename it to specific bug
Ex. changes only visible in bugfix branch
make changes
$git add .
$git commit -m “fix bug”
$git branch -d branchName
if we switch back to master branch (update head pointer)
won’t see changes
in future, will have to merge branches
delete branch

How can we assess how a branch is divering from master?
ie. comparing branches (master w/ sub branch)
$git diff bugfix/signup-form
shows differences between current branch and listed branch
when branches are merged can see expected changes
$git diff –name-status bugfix/signup-form
shows on the file that have been changed in branch
$git log master ..branchName
$git log master ..bugfix/signup-form

What is stashing?

Hint:
$git stash push -am “message”
$git switch master
$git stash show 1
$git stash apply 1
$git stash drop 1
$git stash clear
COMMANDS:
$git stash push -am “message”
$git switch master
$git stash show 1
$git stash apply 1
$git stash drop 1
$git stash clear
When we modify a file in working directory of a branch
and we want to switch to a new branch but have not commited changes
we don’t want to commit because we are not finished
we can stash changes (store in a safe place)
NOTE
new, untracked files are not included in stash (default)
$git stash push -am
to push all files (untracked) into stash

What is merging?
bringing changes from one branch to another
Two methods:
Fast-forward merge
moving the master pointer to the updated branch
if two branches have not diverged
there is a direct linear path from target to source
Three way merge
if branches have diverged
additional commit that combines versions is created
What is a fast-forward merge?

bring master pointer forward
if two branches have not diverged
there is a direct linear path from target to source
brings pointer of master forward
Ex. Create a bugfix branch
code is identical to master
make updates/changes to bugfix directory
copy all files from bugfix to master?
if thousands of files??
no additional changes in master
files in master are first version of bugfix
simply, rename bugfix to master

What is a three way merge?
additional commits on master (after branch)
changes in master that don’t exist in bugfix branch
soln?
git creates a new commit that combines changes from both branches
git looks at before commit and after snapshots (two)
How do we create and switch to a branch simultaneously?

$ git switch -C bugfix
How do we force a merge-commit?
Hint:
$git merge –no-ff bugfix/login-form

$git merge –no-ff bugfix/login-form
allows us a true history of a project
allows us to revert back to previous version

What’s the benefit of not using a fast-forward merge?

new merge commit combines all changes in branch
can revert to an old commit
if using a merge commit, a single commit to revert
vs.
if fast-forward merge, have to undue more commits (two)

How do we disable fast-forward merging in the current repository?
$git config ff no
Some companies may have a policy of no fast forward merging
How do we run a three way merge commit?

$git merge branchName
looks at tips (last commit in each branch)
and common ancestor
creates a merge

How do we see which branches have already been merged with master?
Hint: $git branch –merged (no–merged)

$git branch –merged
shows branches merged
$git branch -d branchName
deletes branch
$git branch –no-merge
shows unmerged branches
Best Practice:
When done with a branch, delete it
otherise pointer will sit / create confusion in future

What is a merge conflict?

Merge Conflicts:
Same line of code has been changed in different ways in different branches
One branch changes a file, another deletes a file
Two branches add same file twice, content is different in both files
Soln?
goog

How do we resolve a merge conflict during a merge commit?
Hint: Don’t add new lines of code during a merge commit

VSCode:
open file
click buttons (accept current, accept incoming, accept both, compare)
add file to staging area
commit file to repository (modified)
Manually:
remove markers for head, divider
don’t add new lines of code during merge commit (evil commit)
Third Party Merge Tools
also available for handling merge conflicts

What is an evil commit?
adding new lines of code during a merge commit
in real world, sometimes not avoidable
AS MUCH AS POSSIBLE, try not to introduce new lines of code

What are merge tools?
Hint: if using VScode or GitKraken, don’t need these tools

can visually see differences
can toggle between multiple files or conflicts
save changes
add modified file to staging
commit the file
How do we abort a merge?
$git merge –abort
takes you back to the state before you start the merge
back to a clean state before starting a merge

How do we undue a merge?

something happens, application breaks, merge unsuccessful
undue the merge
Two options:
remove commit (re-writing history)
okay, if only local, not shared with team
revert commit
create a new commit, cancels all changes in bad commit
How can we remove a commit?
Hint: git reset –hard HEAD~1

WARNING
only do this if on local repository (haven’t pushed to a team repository)
reset the head
Resetting:
Hard
points head to new snapshot, sets working directory, staging directory to snapshot
all working environments are identical
Mixed
points head to new snapshot, staging directory to snapshot, working directory unchanged
Soft
head pointer points to new snapshot, working/staging directory not updated

How do we revert a merge?
Hint: git revert -m 1 HEAD

Use if shared history with collaborators.
Creates a new commit reverting merge commit
doesn’t change history
Each merge has two parents:
one on master branch
one on secondary branch
tell git how to revert:
to last commit on master branch
merge commit is on master branch
merge commit should be on master branch also
$git revert -m 1 HEAD
-m 1 = first parent
HEAD = target commit (last commit)

What is squash merging?
Hint: git merge –squash branchName

$ git merge –squash branchName
$git add.
$git commit -m ““
creates a new commit in stagging area that combines changes from branch into staged files
apply this commit to master
Doesn’t have two parents
Not a merge commit
no reference to the sub branch
just a regular commit added on top of master
not a merge commit
Benefits?
left with a simple, clean history
When to Use?
do not want them to pollute history of master branch
small, short lived branches
bug fixes
use if branch commits are not high quality
features can implement in a few hours / day
make sure to remove target branch!
$git merge –squash branchName
$git branch -D bugfix/photo-upload

What is rebasing?

can change base of our branch
can base it on latest commit on master
Why?
Creates a direct linear path (fast forward merge)
Cautious?
Rewrites history
only use for local commits
do not use for collaboration

What do we do if there is a conflict when rebasing?

launch merge tool
resolve conflict
$git rebase –continue

What is cherry picking?

$git cherry-pick commitID
new commit cherry picked from sub-branch
picking a particular commit from another branch into master
not the entire branch, just a particular commit

How do we take a file from another branch and bring it into the current branch?
git restore –source=feature/send-email –toc.txt
restores file in working directory
from latest version in branch specified

How do we work with branches in VScode?

can view, create, etc branches
can resolve merge conflicts, etc.
can revert commits, etc.

What is git graph?
VS code plugin

How can we use GitKraken for branches?

Makes it incredibly easy to manage branches, resolve conflicts
What will mosh teach us about collaboration?
Super important tools, techniques we must master
Master these tools to collaborate with others
Will use every single day when working with a team
Collab workflows
Push, pull, fetch remote repos
GitHub collab tools - milestones, issues, etc.
contributing to open source projects

What is the centralized workflow?

workflow used in most private teams
everyone has their local repository
central repository used to synchronize work
if central repository goes down, can work offline in remote repository
can synchronize directly with members (often complex)
Where is central respository?
private server in private network
cloud - github, bitbucket

What is integration-manager workflow?

used in open source projects
only maintainers have access to push code into central repository
Process?
fork repository
clone on our local machine
make commits
push to fork
maintainer can pull and review
if good, will push to main repository

How do we add collaborators in Github?
Settings -> manage access -> invite collaborators

How do we clone a github repository onto our machine?

git clone http://etc

What is origin/master?
A remote tracking branch
tells us where master branch is in origin
local repository vs. remote repository (history can change independently)
origin/master tells us where master branch is in origin repository

Is our local repositry connected to our remote repository?
$git fetch
No,
if a change is made in remote repository, local repository is unaware
$git fetch
downloads new commit to our local repository from remote repository
git will download commit / move origin/master forward
to move changes from remote tracking back to local repositry,
merge local branch with origin/master branch
if there is a conflict, resolve like normal

How do we bring the changes in our remote repository to our local repository?

$git fetch
grabs the current snapshot in origin/main
$git merge origin/main
merges local repository with snapshot from origin/main

How do we bring changes from our remote repository into our local repository?
Hint: pull combines two commmands
fetch + merge
$git fetch origin
$git merge origin/main
$git pull
combines fetch and merge commands
What is pulling?

combining fetch and merge commands
$git pull
creates a three way merge commit
pollutes history
$git pull -rebase
creates a rebase action
creates a linear history

What is three way merge pull?


What is a rebase pull?

$git pull –rebase
Creates simple, linear history

What is the push command?

if we have local changes in our repository
push will send them to remote repository
Can be rejected, why?
someone else puts work in the remote repository
First, pull and do a merge (or rebase)
if conflicts, solve them
Then, do a push
How do we store github credentials for push requests?
keychain
git config –global credential.helper osxkeychain

How can we create / store tags in github?

very useful, allows us to download entire source file up to that point

How do we delete a tag from github?
$ git push origin –delete v1.0
deletes tag from repository
$ git tag -d v1.0
deletes tag from local repository

What are releases?

a gitHub feature
can create release to package software, binary files (compiled version of app), release notes

How do we share branches?
Hint: git push -u origin feature/change-password
if collaborating on a branch
must explicitly push branch to github
git push -u origin branchName
-u upstream
creates a branch in github
can use it like main branch
push commits, etc.
git push -d origin feature/change-password
deletes from origin

How do we work with our remote branches?
Hint: git branch -r

first, we fetch a new branch from origin
then, we create a new branch locally that points to origin/main branch

How can we collaborate on remote branches?

git pull
gets the latest commits on branch from collaborators
git switch main
return to main branch from sub branch
git merge feature/change-password
merge main with feature branch (if finished on feature)
git push
send branch changes merged into main to origin/main
git push -d origin feature/change-password
delete branch from origin repository (remote repo)
git branch -d feature/change-password
delete branch off local repository
Collaborator:
create a local branch
point origin/branch to local branch repository
make commits, push
when finished, merge with main and push

How do we remove remote branches that have been deleted from our local repository?
Hint: git remote prune origin
last step after a feature branch was merged and deleted from origin

What is a pull request?

before merging a branch to master
opening a discussion with team mates
Why?
as part of implementing a feature / fixing a bug…
we often need feedback from team
before merging a branch to master
Who closes?
Depends on your team
person who opened it, closes it
person who opened it, doesn’t close it
close + delete on github
then…
$git remote prune origin
$git branch -d feature/login

How can we handle conflicts in a pull request?

Ex. Masterbranch moves forward, feature branch is behind and conflicted
soln
start pull request
use command line
use github user interface
command line?
same as usual
- pull in latest code
- start merge process
- resolve conflicts
- add new commit
- push to github
Github?
use the UI
click on resolve conflicts
click submit
click merge
done!

What is issue tracking?

Goes hand and hand with pull requests
can track…
bug fixes
new features
enhancements
ideas on the team
Why?
all details for working on an issue encapsulated in a single place
much better than using email
can assign to team members
can have a conversation about the issue on the github issue page
can link to a pull request
can assign to a milestone
can come in and pickup a new issue / assign to self

What can we do in our issue tracking?
create custom lables:
refactoring, etc.
github provides a standard / custom labels

What are milestones?

track progress of various issues
add issues to milestone
see progress in milestone
assign issues to a milestone
Ex. V2.0 of Software release
as issues are completed
milestone progress is updated

What are we going to learn in rewriting history?
great but dangerous features
re-write history
rewrite, drop, combine, split commits, etc.

Why do we need history?
Extact meaningful information from history
See what was changed
why and when it was changed
Why do we need to rewrite history?

if we have bad history,
cannot extract meaningful information
re-write to create a meaningful story
Bad history:
commit messages are not meaningful
commits are too large
commits contain unrelated changes
commits are too small or scattered all over history

How can we make clean, meaningful history?
Hint: re-write bad history
too small commits
squash related commits together
too large commits
split large commits into small commits with each being a logical chain
poor messages on commits
reword commits
accidental commits
drop commits
commits not representing logical steps
can modify contents of commits - add file, etc.

What’s the golden rule of re-writing history?
Hint: don’t re-write public history
if pushed commits to public repository (origin)
don’t re-write!
why?
if you replace a commit someone has checked out, going to get issues
will make history look noisy
only re-write private history!
It’s a good practice
clean up private history before sharing with the world
What’s a good practice?
clean up history in private
then push to world
history should tell a story
What’s wrong with this history?

poor commit message
ommited the word “on”
render restaurants ON the map
Small commits
Fix a typo - this shouldn’t be a commit
change color of restaurant icons - should be combined with prev commit
WIP - this is just noise in history, drop or change message
Update terms of service and Google SDK - combining two logic steps together, split into two commits
. - mysterious commit, drop or change message
How do we undue a commit?

Hint: git reset –hard HEAD~1 (removes history)
Locally only?
git reset –hard HEAD~1
removes from history
-soft removes commit only, working/local untouched, working can commit
-mixed removes changes in staging area
-hard discard local changes

How can we revert last three commits?
Hint: git revert –no-commit HEAD~3..
creates a new commit that undoes last three commits
use if history is public already
$ git revert –no-commit HEAD~3
$ git revert –continue
$ git revert –abort (if bad)

How do we recover lost commits?

Hint:
$git reflog
$git reset –hard HEAD@{1}
$ git reflog
$git reset –hard HEAD@{1}
git stores commits in project
after a while, garbage collects them

How do we modify a commit?
$git commit –amend -m

commits on git are immutable
even though on surface, ammeding a commit
git is actually creating a new commit

How can we remove a commit?
$git reset –mixed HEAD~1
working directory is dirty
mixed
points head to commit before last
puts that snapshot in staging area
working directory is still dirty
Soln?
git reset –mixed HEAD~1
git clean -fd
removes untracked files from working directory

How can we ammend an earlier commit?
$ git rebase -i commitID
$ git commit –amend

interactive rebasing
re-writes history
have to re-create other commits along the chain
How?
$ git rebase -i commitID
open in editor, change specific commit to “edit”
make modifications
$ git commit –amend
Process?
creates a separate branch
all changes made from commit will be played on top of commit
changes are carried on across subsequent commits
branch will be discarded

How do we drop a commit?
start rebase with parent of commit (want to drop)
Problem?
if this commit introduced a new file, next commit modifies it
will be a conflict
changes from deleted commit, added to subsequent commit

How do we reword commit messages?
Hint: git rebase -i commit~1

change message to reword
git will pull up commit, allow us to change message
all subsequent commits are re-created
re-writes history
only do with private repository!

How do we reorder commits?
Hint: git rebase -i commit

git rebase -i commit
move up in VScode

How do we squash commits?

Hint: git rebase -i
combine commits

How can we split a commit?

git rebase -i commitParent
opens VS code, click edit, close
brings edit snapshot to working dir / staging dir
git reset –soft HEAD~1
moves head to one step before target commit
changes in staging area, can unstage some, make separate commits
git reset –mixed HEAD^

How can we re-write history in gitkracken?
