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