"The path to becoming a Master of the mystic art of Git".
A rolling-release presentation on some of the less known internal aspects and commands of Git, some advice for a better use and common workflows.
5. COMMON COMMANDS
ā¢ Git status
ā¢ Status of the repository
ā¢ Git add <file,ā¦>
ā¢ Add file(s) to staging area
ā¢ Under the hood:ākeep an eye on itā, prepare to recursively create tree and blob objects
ā¢ Git commit [-m ā<commitmessage>ā]
ā¢ Commit the changes added to the staging area
ā¢ Under the hood:create the commit object,associate tree,blob and commit objects
ā¢ Git pull
ā¢ Pull the latest updates from the server
ā¢ Git push
ā¢ Push the commit(s) to the remote server
10. GIT:WHAT
ā¢ āGit, the stupid content
trackerā
ā¢ API for DVCS
ā¢ Created by LinusTorvalds in
2005 for Linux Kernel source
code
ā¢ Before: patches & packages,
then proprietary BitKeeper
LinusTorvalds
12. GIT
Linus about the name āgitā (āunpleasant personā in British English slang):
āIām an egotistical bastard,and I name all my projects after myself.FirstāLinuxā,nowāgitā.ā (source)
From the original README (source)
13. GOD MADE UNIVERSE IN 6 DAYS,
LINUS MADE GIT IN 4,
GIT SELF-HOSTED IN 1
ā¢ Development start: ~April 3,2005
ā¢ Announcement:April 6, 2005
ā¢ Self-hosting:April 7, 2005
Original message from the mailing list
14. GOALS
ā¢ Speed
ā¢ Simple design
ā¢ Strong support for non-linear development (thousands of
parallel branches)
ā¢ Fully distributed
ā¢ Able to handle large projects like the Linux kernel efficiently
(speed and data size)
15. COMMIT OFTEN
PERFECT LATER
PUBLISH ONCE
ā¢ Commit early, commit often
ā¢ Each commit represents one idea or one whole change (easier
to read or revert)
ā¢ Each branch represents one feature (or topic) (easier to read or
merge)
ā¢ Your local working directory,index and local repo are scratch
pads.
17. WHY DEEPER?
ā¢ Git originally designed and created for internal-aware pro
developers
ā¢ Specific atomic commands,*NIX style
ā¢ File-system alike level
ā¢ Untold mantra:āWhen youāll know what it does and how,
youāll figure out how to use itā
19. GIT GEOGRAPHY
ā¢ Local repo: .git folder and its content
ā¢ Working directory/area: brothers (and descendents)
of .git folder
ā¢ Stage/Staging area/Cache/Index: (synonyms,index
originally) index of changes to commit
ā¢ Stashing area/Stash: clipboard-ish area where to
temporarily save the changes not to be committed,
facility external toVCS
20. COMMANDS:
PORCELAIN VS.PLUMBING
PLUMBING(s)
Advanced, specific tools/commands for a single purpose and advanced problem
solving (Barely documented: are you a Pro, arenātyou?)
PORCELAIN(s)
Different grouping and use of plumbing commands (Very extended documentation)
Git:API forDVCS
Avoid duplication of API with different parameters
Split underneath logics in simpler commands (UNIX-style)
21. OBJECTS
ā¢ Main element in GIT:
ā¢ Stored in key-value database: Key =>Value
ā¢ Named after the content using SHA-1 hash:SHA-1(Value) => Value
ā¢ IMMUTABLE: mutation would break hash, so references would be broken too!
ā¢ Deduplication for free: same object referenced, not storedagain
ā¢ Main object types:
ā¢ Blob
ā¢ Tree
ā¢ Commit
ā¢ AnnotatedTag
22. OBJECTS
ā¢ Blob
ā¢ Full content of a file without filename and metadata
ā¢ Named after SHA-1 of content and attributes
ā¢ Tree
ā¢ Representation of a directory structure / file(s)
ā¢ Recursive structure: contains blob and tree references
ā¢ Commit
ā¢ Snapshot of the repository at given time
ā¢ Tree object reference (recursive)
ā¢ Author's data:name,email,timestamp,timezone
ā¢ Committer's data:name,email,timestamp,timezone
ā¢ Commit Message
ā¢ Parent commit reference (special commit: Merge commit has two/more than one parents)
23. .GIT FOLDER
ā¢ Created with āgit initā
command
ā¢ The .git folder IS the local
repository
Structure of .git folder of an empty repository
24. .GIT FOLDER
ā¢ After the commit of a
simple text file
Structure of .git folder of the repository at the current state
25. HEAD
ā¢ It points to the current state
of the working area
ā¢ Relative pointing to a refs,
e.g.:
ā¢ āref:refs/heads/masterā
ā¢ In detached state if it points
to an hash
ā¢ only good for read-only navigation of the
repo, destructive otherwise Structure of .git folder of the repository at the current state
26. CONFIG
ā¢ Local configurations
ā¢ Core configuration values
ā¢ Authorās data:
ā¢ Name, email,ā¦
ā¢ Opposite to
global .gitconfig file in home
Structure of .git folder of the repository at the current state
28. HOOKS
ā¢ (Sample of) scripts
automatically launched in
response to a specific
events
ā¢ Validation,styling rules,
commit checks,ā¦
ā¢ Without the .sample
Structure of .git folder of the repository at the current state
29. OBJECTS
ā¢ Key-value database
ā¢ Contains blobs, trees and
commits
ā¢ 256 dirs max per level with
first 2 hex chars of object
hash (avoid filesystem
limits)
ā¢ pack folder: highly compressed,
delta-optimized archives of
objects, created at push/pull Structure of .git folder of the repository at the current state
30. REFS
ā¢ Heads == Branches
ā¢ Tags (lightweight)
ā¢ Refspec: specification of
references
ā¢ Heads point to a specific
commit
ā¢ Head of that branch
Structure of .git folder of the repository at the current state
32. LOG
ā¢ Git log
ā¢ Log of all commits in the common default format
ā¢ More or less infinite combination of parameters for different
showing mode of the log (e.g.colors,graphic,relative time format:
n days ago)
ā¢ Pretty format documentation
ā¢ One of the longest man page!
ā¢ If lost, use A DOG:git log --all --decorate --oneline --graph
33. FETCH VS.PULL
FETCH
ā¢ Fetch refs (branches, tags) from
remote repository (one or more)
ā¢ Doesnāt download commit
ā¢ Useful for sync with new remote(s)
and/or branch(es)
PULL
ā¢ Downloads commits
ā¢ For the current branch from
remote counterpart
ā¢ Doesnāt alter the state of the
working directory or local repository ā¢ Automatically updates the
state of the working directory
ā¢ git fetch && git merge FETCH_HEAD
34. BRANCHES
ā¢ Extremely cheap and fast
ā¢ A branch is a file that contains the reference to a commit.Thatās it.
ā¢ Itās not an object, so (and thatās why) itās mutable!
ā¢ The entire branch is recreated recursively using parent reference
of each commit, starting from the pointed one
ā¢ Fast-forward merge if the branch starts from the end of original/
destination branch (e.g. after a rebase)
35. BRANCHING
ā¢ Git checkout <branch>
ā¢ Move to an existent branch
ā¢ Git checkout -b <branch>
ā¢ Create a branch if not existent and move to it
ā¢ Git branch <branch>
ā¢ Create a branch if not existent without moving to it
ā¢ Git merge <branch>
ā¢ Merge the specified branch on the current one
ā¢ Git rebase <branch>
ā¢ Rebase the current branch on the specified branch
36. STASHING
ā¢ Git stash [save]
ā¢ Save the changes in the working area into the stashing area
ā¢ Git stash list
ā¢ List all the stashes
ā¢ Git stash show
ā¢ Show the details of a stash
ā¢ Git stash apply
ā¢ Apply the specified stash on the current working directory
ā¢ [other options and combinationā¦]
37. TAGS
ā¢ Simple name pointing to a
commit
ā¢ Arbitrary fixed reference to a commit
ā¢ 2 types (completely unrelated to each other)
Lightweight Annotated
ā¢ Structurally similar to branch ā¢ Structurally is an object
ā¢ Simple name pointing to a
commit, with a message
38. TAGGING
ā¢ Git tag
ā¢ List all the tags
ā¢ Git tag <tag name>
ā¢ Create a lightweight tag on the current commit
ā¢ Git tag -a <tag name> -m <message>
ā¢ Create an annotated tag on the current commit, with the specified message
ā¢ Git show <tag name>
ā¢ Show details about the specified tag
ā¢ Git push [remote] --tags
ā¢ Push the tags on remote repository (Not pushed by default push command!)
39. BLAME
ā¢ Useful for firing people
ā¢ Shows the changes made on an object (list of
commits) with relative authors
ā¢ Syntax:
ā¢ git blame [lots of options] <file>
41. REFLOG
ā¢ Reference logs :)
ā¢ git reflog [options]
ā¢ āRecord when the tips of branches and other references were updated
in the local repository.ā(source)
ā¢ āIt saves yourā¦ life!ā (anyone whoāve usedit)
ā¢ Commits are not deleted until the garbage collector comes in action!
ā¢ Go back in time, locate the commit and restore
42. CHERRY-PICK
ā¢ Apply the changes introduced by an existing commit
(somewhere in the repo)
ā¢ It creates a new commit
ā¢ Best results if the commit is atomic and self-
explanatory
ā¢ git cherry-pick <commit>
43. REBASE
ā¢ Reapply commits on top of another base tip
ā¢ Interactive rebase with -i option (options in text editor)
ā¢ Main uses:
ā¢ History rewriting,actions (edit,squash,delete,ā¦) on the same base:git rebase
<commit>
ā¢ Branch update, git rebase <branch>:
ā¢ B branched from A at commit C1,A updated after the divergence (commits C2,C3)
ā¢ Rebase B on top of new tip of A (C3) reapplying all the commits of B on top of C3
ā¢ Best results if the commit is atomic and self-explanatory
51. BISECT
ā¢ Use binary search to find the commit that introduced a bug
ā¢ Automatic mode usage:
ā¢ Select known good commit: git bisect good <commitās sha-1>
ā¢ Select known bad commit: git bisect bad <commitās sha-1>
ā¢ Run the test in commits betweenāgoodāandābadā:git bisect run <bash
script>
ā¢ Bisect will stop at the first commit that introduced the bug (test fails)
ā¢ More info
53. SUBMODULE
ā¢ Include a git repository inside another one
ā¢ Both stories remain separated
ā¢ Push/pull, branches to/from original separate repository
ā¢ .gitmodule
ā¢ Reference to submodule(s)ās repository
ā¢ Reference to submodule(s)ās current commit (HEAD)
ā¢ Reference to submodule(s)ās destination directory (added empty to parent
repository)
54. SUBMODULE COMMANDS
ā¢ Git submodule add <repository> [path]
ā¢ Add an existing repository as submodule in path or subrepo name if any
ā¢ Git submodule foreachā<bash>ā
ā¢ Execute the command specified for each submodule added in the parent repository
ā¢ Git submodule update [path]
ā¢ Update the submodule to the state specified into .gitmodule file (potentially discard
new changes)
ā¢ (Canonical add, commit to update the .gitmodule file with newest
submodule commits)
55. SUBTREE
ā¢ Include a git repository inside another one
ā¢ Stories are not separated: the subtreeās story is included in parent
repository (can be squashed)
ā¢ Modification to subtreeās code goes on parent projectās
repository
ā¢ Can pull updates from original repository
ā¢ Can push updates to original repository
56. SUBTREE COMMANDS
ā¢ Git subtree add --prefix=[path] <repository> <branch> --squash
ā¢ Add an existing repository as subtree in path squashing all the commits in one
ā¢ Git subtree pull --prefix [path] <repository> <branch> --squash
ā¢ Update the subtree with new commits on remote repository
ā¢ Contribute back to remote repository:
ā¢ Add the project as another remote: git remote add <name> <repo>
ā¢ git subtree push āprefix=<prefix> <remote> <branch>
ā¢ More info about subtree: Source,Source
59. My 2 centsā¦
ā¢ Commit ASAP, usingVERY crystal clear message
ā¢ Rebase your local branch without fear until it goes on remote
ā¢ Keep the history straight! Avoid branch updating with merge!
ā¢ Keep environment configuration - e.g. production - on specific
branch (to be rebased on the branch that will go online)
ā¢ Use git aliases, but remember the original command
ā¢ The current status and branch must be always visible without
issuing āgit statusā (e.g. use things like bash-git-prompt or similar)
ā¢ Fetch is better than pull for crowded branches
63. GITHUB
ā¢ Biggest community exclusively for git ecosystem
ā¢ 316 programming languages, ~6M active users, ~332K activeorganizations,
~19.5M active repositories
ā¢ Main home for any open source project (hosted for free withTravisCI)
ā¢ Most advanced web interface and support for most diffused and evolved
development metodologies at any level (Agile,Testing, CI/CD,ā¦)
ā¢ Highly reliable, but not self-hostable
ā¢ The annual state of the Octoverse
65. GITLAB
ā¢ Fastest development project and community exclusively for git ecosystem: rolling releases and CI
ā¢ More than 100K active organizations self-hosting (2/3 market), ~80% of mobile developers
repositories
ā¢ Self-hostable (Vagrant, Docker, native), free public and private repositories with unlimitedmembers
ā¢ Awesome repository management (user permissions,branch protection,Webhooks,ā¦)
ā¢ Most advanced web interface and support for most diffused and evolved development
metodologies at any level (Agile,Testing, CI/CD,ā¦)
ā¢ Most experimental and cutting-edge solutions for Agile and DevOps metodologies development
(Auto DevOps,ā¦)
ā¢ Full stack of additional features (pages,CI/CD,Cycle,IssueTracker and Board,Review,ā¦)
70. REFERENCES / RESOURCES
Conferences
āGit From the Bits Upā -Tim Berglund
āAdvanced GIT for Developersā - Lorna Jane Mitchell
āGet Started with Gitā - DavidBaumgold
āAdvanced Gitā - DavidBaumgold
āA journey intoā GIT internals with Python - AndreySyschikov
Books
Pro Git
āGit Best Practices Guideā - Eric Pidoux
āMastering Gitā - Jakub NarÄbski
āLearn enough Git to be dangerousā - MichaelHartl
71. LICENSE
ā¢ The whole presentation and the entire content (except where alternatively
and explicitly specified) is property of the author, Nicola Costantino, and itās
released under the term of the āCreative CommonsAttribution -
NonCommercial - NoDerivatives 4.0 International Licenseā
ā¢ All third party media contents are property of their respective owners and
are hereby only used for teaching purposes