Git is a distributed version control system that is used for software development, and allows many software developers to work on a given project without requiring them to share a common network.
This presentation helps you to know what is Git, how it works, and how you can use it.
2. • Def:
– A system that records changes to a file or set of files(any type!) over time so
that you can recall specific versions later
• Used for:
– Revert files back to a previous state,
– Revert the entire project back to a previous state,
– Compare changes over time,
What is Version Control System?
– Compare changes over time,
– See who last modified something that might be causing a problem,
– Who introduced an issue and when,
– If you screw things up or lose files, you can easily recover.
– And more!
• Used by
– Graphic
– Web designer
– Developers
– And more!
2
8. Git History
• Linux kernel in 1992-2002:
– filing & archiving
• Linux kernel in 2002
– BitKeeper DVCS
8
9. Git History
• Linux kernel in 1992-2002:
– filing & archiving
• Linux kernel in 2002
– BitKeeper DVCS
• GIT a new DVCS• GIT a new DVCS
• Goals of New System by linus trovalds in 2005:
– Speed
– Simple design
– Strong support for non-linear development
(thousands of parallel branches)
– Fully distributed
9
10. How it works? (snapshots…)
• Snapshot, not differences (like CVS, Subversion, Perforce, Bazaar)
10
11. How it works?(Integrity…)
• Snapshot, not differences (like CVS, Subversion, Perforce, Bazaar)
• Nearly every operation is local
• Git has integrity
– Everything is check-summed before stored and referred to by that
– it’s impossible to change the contents of any file or directory without
Git knowing about it.Git knowing about it.
– SHA-1 hash:
• 40- hexadecimal character string
• calculated based on the contents of a file or directory structure in Git.
• 24b9da6552252987aa493b52f8696cd6d3b00373
11
12. How it works? (Undo…)
• Snapshot, not differences (like CVS, Subversion, Perforce, Bazaar)
• Nearly every operation is local
• Git has integrity
– Everything is check-summed before stored and referred to by that
– it’s impossible to change the contents of any file or directory without
Git knowing about it.Git knowing about it.
– SHA-1 hash:
• 40- hexadecimal character string
• calculated based on the contents of a file or directory structure in Git.
• 24b9da6552252987aa493b52f8696cd6d3b00373
• Git generally only adds data
– It is very difficult to get the system to do anything that is
not undoable
12
13. How it works? (states…)
• The three states
– Committed: Data is safely stored in your local database
– Modified: Files are changed but have not committed it to
database
– Staged: Marked a modified file in its current version to go– Staged: Marked a modified file in its current version to go
into next commit snapshot
Three States
• modified
• staged
• Commit
• modified
• staged
• Commit
Three main
sections
• Working directory
• Staged area
• Git directory
• Working directory
• Staged area
• Git directory
13
14. How it works? (main sections…)
• working directory:
– single checkout of one version of the project
– Files are pulled out of the compressed database in the Git directory and
placed on disk
• Staging:
– Simple file
– contained in Git directory– contained in Git directory
– stores information about what will go into your next commit
– sometimes referred to as the index
• Git directory
– stores the metadata and object database for your project
– most important part
– It is what is copied when you clone a repository from another
computer
14
16. How it works? (work flow…)
• You modify files in your working directory.
• You stage the files, adding snapshots of them to
your staging area.
• You do a commit, which takes the files as they are
in the staging area and stores that snapshot
permanently to your Git directory.
16
22. ssh Example
• cd ~/.ssh
Check for SSH keys
• $ls
• $mkdir key_backup
Backup and remove existing SSH keys
• $mkdir key_backup
• cp id_rsa* key_backup
• rm id_rsa*
• ssh-keygen -t rsa -C "your_email@youremail.com"
• Enter path and pass phrase
Generate a new SSH key
• Done! Now, you can clone the repository on git server.
Send “id_rsa.pub ” to the SERVERS’s administrator
22
23. • File status life cycle
– Add
– Commit
– Remove
Add/Commit (status)
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
– Remove
– Rename
23
24. • File status life cycle
Add/Commit (status)
commitUntracked unmodifide modifide Staged
Tracking files
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
24
25. • File status life cycle
Add/Commit (status)
commitUntracked unmodifide modifide Staged
Tracking files
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
Add the file
• You can remove the files from tracking/unmodified stage, too
25
26. • File status life cycle
Add/Commit (status)
commitUntracked unmodifide modifide Staged
Tracking files
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
Edit the file
Add the file
26
27. • File status life cycle
Add/Commit (status)
commitUntracked unmodifide modifide Staged
Tracking files
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
Edit the file
Add the file
Staged the file
27
28. • File status life cycle
Add/Commit (status)
commitUntracked unmodifide modifide Staged
Tracking files
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
Commit file
Edit the file
Add the file
Staged the file
28
29. • File status life cycle
Add/Commit (status)
commitUntracked unmodifide modifide Staged
Tracking files
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
Commit file
Edit the file
Add the file
Staged the file
Commit
29
30. • File status life cycle
– getting the file status:
• $git status
Add/Commit (status)
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
– Notice to:
• Untracked files
• Nothing to commit (working directory clean)
• Changed but not updated
• Changes to be committed
30
31. • File status life cycle
– getting the file status:
• $git status
Add/Commit (status)
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
– Notice to:
• Untracked files
• Nothing to commit (working directory clean)
• Changed but not updated
• Changes to be committed
31
32. Add/Commit (commands)
• Add files:
– $git add your_file_or_directory
– Multipurpose add: 1)begin tracking new files, 2)stage files, …
Track new file
• $vim README
$git status
• $ git add README
$ git status
• $vim benchmarks.rb
$ git status
• $ git add benchmarks.rb
$ git status
Staging modified files:
32
34. Add/Commit(Undo)
• Committing (Changing the last commit)
– $git commit -m 'initial commit'
– $git add <forgotten_file>
– $ git commit --amend
• Un-staging a staged file (Notice: status)• Un-staging a staged file (Notice: status)
– $git reset HEAD <filename>
• Un-modifying a modified file (Notice: status)
– $git checkout -- <filename>
• Deleted and overwritten commits are recoverable!
34
35. Dot(!) gitignore
• Ignoring:
– A class of files that you don’t want Git to
automatically add or even show you as being
untrackeduntracked
– .gitignore: listing all patterns that should be
ignored
• Ignoring files:
– $ cat .gitignore
• The rules
35
36. Dot(!) gitignore
• The rules
– The blank lines or lines starting with # are ignored.
– To specify a directory, end your patterns with “/”
– Negate a pattern by “!”
– “*” matches zero or more characters
– [abc] matches any character inside the brackets– [abc] matches any character inside the brackets
– “?” matches a single character
• Example:
– *~ (Ignore the temp files of Emacs)
– build/ # (Ignore all of the files in the build directory )
– doc/*.txt # (Ignore doc/notes.txt, but not doc/server/arch.txt)
– *.a # (Ignore the files which ends with .a)
– !lib.a # (but do track lib.a, even though you're ignoring .a files)
– /TODO # (Only ignore the root TODO file, not subdirectory /TODO)
36
37. diff
• We uses this command to:
– know what is changed, not just which files were changed (It is not same
as “git status” command)
– answer these two questions:
• What have you changed but not yet staged?
• What have you staged that you are about to commit?
– show the exact lines which are added and removed
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
– show the exact lines which are added and removed
• To see what is modified but not it is not staged :
– $git diff (compares working directory with staging area)
• To see what is staged but is not committed:
– $git diff --cached
– $git diff --staged
37
38. log
• To Show commits on project reversely!
– $git log
• Each part of the ‘log’ command’s output contains: Sha1,
Author’s Name and Email, Date of commit, and the Commit Message
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date: Sat Mar 15 16:40:33 2008 -0700
removed unnecessary test code
38
39. log…
• To Show commits on project reversely!
– $git log
• Each part of the ‘log’ command’s output contains: Sha1,
Author’s Name and Email, Date of commit, and the Commit Message
• Options:
– -p : Shows diff– -p : Shows diff
– -<#n>: shows n-th last commits
– --stat: shows statics for files modified in each commit: 1 files
changed, 1 insertions(+), 1 deletions(-)
– --pretty: The output format
• =Oneline
• =Short/full/fuller
• =format :"%h %s"
$ git log --since=2.weeks
$ git log --pretty="%h - %s" --author=gitster --since="2008-
10-01" --before="2008-11-01" --t/ 39
40. Log options description
• -p: Show the patch introduced with each commit.
• --stat: Show statistics for files modified in each commit.
• --shortstat: Display only the changed/insertions/deletions line from the --
stat command.
• --name-only: Show the list of files modified after the commit information.
• --name-status: Show the list of files affected with added/modified/deleted
information as well.information as well.
• --abbrev-commit: Show only the first few characters of the SHA-1
checksum instead of all 40.
• --relative-date: Display the date in a relative format (for example, “2
weeks ago”) instead of using the full date format.
• --graph: Display an ASCII graph of the branch and merge history
beside the log output.
• --pretty: Show commits in an alternate format. Options include oneline,
short, full, fuller, and format (where you specify your own format).
40
41. Log format options
• %H Commit hash
• %h Abbreviated commit hash
• %T Tree hash
• %t Abbreviated tree hash
• %P Parent hashes
• %p Abbreviated parent hashes
• %an Author name• %an Author name
• %ae Author e-mail
• %ad Author date (format respects the –date= option)
• %ar Author date, relative
• %cn Committer name
• %ce Committer email
• %cd Committer date
• %cr Committer date, relative
• %s Subject
41
46. Remotes …
• Pushing to Your Remotes
– $ git push [remote-name] [branch-name]
• Inspecting a Remote
– $git remote show <remote-name>– $git remote show <remote-name>
• Removing and Renaming Remotes (Changes
your remote branch name, locally)
– $git remote rename <prev_name> <new_name>
– $git remote rm <branch_name>
46
47. Tag
• purposes
– tag specific points in history as being important
– mark release points
• Tags Types
– Lightweight
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
– Lightweight
• it’s just a pointer to a specific commit
• Just like a branch that doesn’t change
– Annotated (recommended)
• stored as full objects in the Git database
• They’re checksummed
• contain the tagger name, e-mail, and date
• have a tagging message
• can be signed and verified with GNU Privacy Guard(GPG)
47
48. Tag…
• Verifying a Signed Tag
– need the signer’s public key in your keyring
– If don’t have the signer’s public key: “could not verify
the tag”
• Sharing Tags
– “git push” command doesn’t transfer tags
to remote servers
48
49. Tag…
• List your tags
– $ git tag
• Search a pattern in the tags
– $ git tag -l 'v1.4.2.*‘
• Lightweight tags:
– $git tag <Your_Tag>
– $git show <Your_Tag> (To show no more info)
• Annotated tags:
– git tag -a <Your_Tag> -m ‘<Tag_Message>’
– $git show <Your_Tag> (To show commit too)
49
50. Tag…
• Signed tags:
– $git tag -s <Your_Tag> -m ‘<Tag_Message>’
– $git show <Your_Tag> (To show GPG sign too)
• Verifying a signed tag
– $git tag -v <Your_Tag>– $git tag -v <Your_Tag>
• Tagging Later(Tag a specific commit)
– $git tag -a <Your_Tag> <commit_checksum> (or part of it!)
• Sharing Tags
– $git push <remote-name> <Your_Tag>
– $git push <remote-name> --tags (To push all tags)
50
51. Some tips & tricks
• Bach shell completion:
– Find file GitDir/contrib/completion/git-completion.bash
– For your user, copy it to home directory
– For all user, copy it to /etc/bash_completion.d/– For all user, copy it to /etc/bash_completion.d/
– add it to your .bashrc file
• Git Aliases
– $git config --global alias <your-Alias> <command>
– $git config --global alias.ci commit
51
52. Branch
You can have different versions of your source code!
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
52
53. Branch…
• Every indexed file in stage area is a blob!
• Every commit has a tree of blobs
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
53
54. Branch…
• Every indexed file in stage area is a blob!
• Every commit has a tree of blobs
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
54
55. Branch…
• Every indexed file in stage area is a blob!
• Every commit has a tree of blobs
GitInstall
Git
repo
add/
commit
Diff/log
Remote
Branch
tag
55
64. Basic Branch Commands
• List the current branches: $git branch [-v]
• Create a new branch: $git branch <New_Branch>
• Change to a branch: $git checkout <Your_Branch>
• Create & change to a new branch: $git checkout –b <Your_Branch>
• Delete a branch: $git checkout –d <Your_Branch>
• Merge the branch into current branch: $git merge <Your_Branch>
• List current merged branches: $ git branch --merged
• List the non-merged branches: $ git branch --no-merged
• List all of local and remote branches: $git branch –a
• List all of remote tracking branches: $git branch –r
64
65. Branch merging
The branch story(!) case study:
1- Do work on a web site.
2- Create a branch for a new story you’re working on.
3- Do some work in that branch.
you’ll receive a call that another issue is critical and you need a
hotfix. You’ll do the following:
1- Create a branch to add the hotfix.
2- After it’s tested, merge the hotfix branch, and push
to production.
3- Switch back to your original story and continue
working.
65
67. Branch merging …
1- Do work on a web site.
2- Create a branch for a new story you are
working on:
$git checkout -b iss53
67
68. Branch merging …
1- Do work on a web site.
2- Create a branch for a new story you are
working on:
$git checkout -b iss53
3- Do some work in that branch:3- Do some work in that branch:
$vim index.html
$git commit -a -m 'added a new footer [issue 53] '
68
69. Branch merging …
1- Do work on a web site.
2- Create a branch for a new story you are
working on:
$git checkout -b iss53
3- Do some work in that branch:3- Do some work in that branch:
$vim index.html
$git commit -a -m 'added a new footer [issue 53] ‘
Another critical issue, and you need a hotfix!
69
70. Branch merging …
Another critical issue, and you need a hotfix!
1- Create a branch to add the hotfix:
$ git checkout master
$ git checkout -b 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address'
70
71. Branch merging …
Another critical issue, and you need a hotfix!
1- Create a branch to add the hotfix:
$ git checkout master
$ git checkout -b 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address‘
2- After it’s tested, merge the hotfix branch,
and push to production:
$git checkout master
$git merge hotfix
71
72. Branch merging …
Another critical issue, and you need a hotfix!
1- Create a branch to add the hotfix:
$ git checkout master
$ git checkout -b 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address‘
2- After it’s tested, merge the hotfix branch,
and push to production:
$git checkout master
$git merge hotfix
3- Switch back to your original story and
continue working:
$git branch -d hotfix
$ git checkout issue 53
$ vim index.html
$ git commit -a –m 'finished issue 53' 72
73. Branch merging …
• And now you want to merge issue 53:
$git checkout master
$git merge iss53
• What does the ‘merge’ command do?
1) It comes back to the common ancestor of branches1) It comes back to the common ancestor of branches
2) It creates a new snapshot from the last commit of branches
3) It creates a new commit point on master branch (C6)
4) It merges branches into the new commit point
73
74. • what if conflicts happen:
– $git merge iss53
Merge conflicts
$git merge iss53
#Auto-merging index.html
#CONFLICT (content): Merge conflict in index.html#CONFLICT (content): Merge conflict in index.html
#Automatic merge failed; fix conflicts and then commit the result
74
75. • what if conflicts happen:
– $git merge iss53
• Which files unmerged?
– $git status
Merge conflicts
[master*]$ git status[master*]$ git status
#index.html: needs merge
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in
working directory)
#
# unmerged: index.html
75
76. • what if conflicts happen:
– $git merge iss53
• Which files unmerged?
– $git status
• Vi files with confilict:
Merge conflicts
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html
76
77. • what if conflicts happen:
– $git merge iss53
• Which files unmerged?
– $git status
• Vi files with confilict:
Merge conflicts
Headiss53
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html
77
78. • what if conflicts happen:
– $git merge iss53
• Which files unmerged?
– $git status
• Vi files with confilict:
– Replace the conflict with one of the options, or merge them
Merge conflicts
– Replace the conflict with one of the options, or merge them
together:
<div id="footer">
please contact us at support@github.com
</div>
– And Then:
$git add
$git status
$git commit
78