Git is a distributed version control system, created by Linus Torvalds, and used worldwide by many software developers for a large number of well known software projects.
In this presentation, I explain the fundamentals of the Git software and some of its internal workings and related terminology. The target audience is individuals who have used Git before in a limited capacity and want expand their knowledge of the software to improve their daily use of it. The presentation could also be useful for those with knowledge of Git and a desire to improve their understanding of some of the more technically elements.
4. @DivineOmega
History of Git
● Developed in 2005
– Started on 3 April
– 1.0 release on 21 December 2005
● Created
– For the Linux Kernel
– By Linus Torvalds
● Maintained by Junio Hamano
7. @DivineOmega
How people use Git
● Many different ways
● Can just be used locally
– Just on your PC
● Or between teams
– Using a remote repository
● Many web services/interfaces
– GitHub, GitLab, BitBucket, etc.
9. @DivineOmega
The Git Places
● Working directory
● 'Stage' or 'Staging Area'
● Local Git repository
– '.git' directory
● Optionally, remote Git repository
– Known as 'remotes'
10. @DivineOmega
Working directory
● Where you work
● Where you make your changes
● Use 'git checkout' to revert changes to a file in
the working directory (back to the last commit)
● Sometimes called the 'workspace'
11. @DivineOmega
Staging area
● Any files about to be committed ('staged')
● Use 'git add' to move files to the staging area
('stage' them)
● Use 'git reset' to remove files from the staging area
● Stored in index file in your .git directory
● Sometimes called the 'index'
12. @DivineOmega
Local Git Repository
● All committed files, revision history, etc.
– Commits (objects identified by SHA1 hashes – commit IDs)
– Pointers to certain commits (known as references)
●
Branches
●
Tags
– 'Head' – where you currently are, a pointer to another reference
● Use 'git commit' to store any staged files in a new commit
● Use 'git revert' to undo a commit by its ID (creates a new commit)
●
Most of what is stored in your .git directory (.git/objects/ & .git/refs/)
13. @DivineOmega
Remote Git Repository
● Any repository not on your computer
● Known as a 'remote'
● Consists of a name and a URL
● 'git remote add', 'git remote remove', 'git remote'
● 'git push' to send changes to the remote
● 'git pull' to fetch & merge changes from the remote
15. @DivineOmega
Quick Explanation of Branches
● Just a named pointer to a specific commit
● Use 'git branch branch_name' to create one
pointing at your current commit
● File containing the SHA-1 hash of the commit it
points to
– .git/refs/heads/master
– .git/refs/heads/branch_name
16. @DivineOmega
Quick Explanation of Branches
http://goo.gl/wpesH6
● git branch testing
– Create new 'testing' branch at current commit
17. @DivineOmega
Quick Explanation of Branches
● Used to separate developments from the
'master' branch
– New features
– Phases of development
● Typically merged back into master when feature
is complete
20. @DivineOmega
Local Repository Example
●
Assumption: A local Git repository exists and has multiple branches.
● git checkout greetings
– checkout 'greetings' branch of local repository
● create/change hello.txt in your favourite editor
● git add hello.txt
– stage files (added to the staging area / git index)
● git commit -m “Altered greeting”
– commit to local repository (.git directory)
22. @DivineOmega
Remote Repository Example
●
Assumption: A remote Git repository exists and has been cloned locally.
● git add hello.txt
– stage files (to staging area / git index)
● git commit -m “New greets”
– commit to local repo. (.git directory)
● git push
– Updates the remote repository with local changes
– Technically:
● Tells the remote about your local branches/commits
● Asks the remote to update its branch reference (commit ID) to match the local reference
● It will oblige providing the change is a 'fast forward' (i.e. no merge required, just moving reference)
24. @DivineOmega
Create your new branch
● git branch super_new_feature
– Create a new branch for the new feature
● git checkout super_new_feature
– Switch to the new branch
25. @DivineOmega
Add your new fix/feature
● Add the new feature
– In our example, we'll say this feature only required a change to 'thing.php'
● git add thing.php
– Stage the file(s)
● git commit -m 'Added great new feature'
– Commit the file(s)
● Continue to change, add and commit until the feature is complete,
tested and ready to be merged into the main (master) branch
26. @DivineOmega
Merge the branch
● git checkout master
– Switch back to the main branch
● git merge super_new_feature
– Merge the changes from super_new_feature into the
current branch (master)
● git branch -d super_new_feature
– Delete the branch, to keep your local repository tidy
27. @DivineOmega
Notes on Merging and Conflicts
● Branches can be used however you wish.
● Master is nothing special, it's just the default.
● Automatic merges sometimes fail.
– CONFLICT (content): Merge conflict in thing.php
– Automatic merge failed; fix conflicts and then commit the result.
● When they do:
– Go into the files specified and fix the conflicts.
– Stages the files ('git add')
– Commit the fixes ('git commit') with an appropriate message
30. @DivineOmega
Quick reference to Git commands
●
git status – Shows the current branch, modified working directory files, and staged files
●
git add – Stages files (gets them ready for committing)
● git commit – Moves all staged files into your local repository
●
git push – Sends all local repository changes to the remote, and updates the remote reference
●
git pull – Fetch and merge from a remote repository (or local branch)
● git revert – Undoes a commit based on its ID (creates a new commit)
●
git checkout – Reverts file(s) in the working directory back to the last commit (also be used to
switch branch)
– Be careful! This command can cause permanent loss of uncommitted changes!
● git reset – Removes files from the staging area (and more, be careful!)
– Danger Will Robinson! Use of the '--hard' parameter can cause permanent loss of both uncommitted and
committed changes!