Git Github PDF
Git Github PDF
Source: http://bit.ly/1SH4E23
What is git?
What is git?
• created by Linus Torvalds, April 2005
https://www.openhub.net/repositories/compare
http://bit.ly/1QyLoOu
http://www.indeed.com/jobtrends/q-svn-q-git-q-subversion-q-
github.html?relative=1
Git distributed version control
• “If you’re not distributed, you’re not worth using.” – Linus Torvalds
• every copy of a Git repository can serve either as the server or as a client
(and has complete history!)
Repository
checkout commit
working
Git uses a three-tree architecture
Repository
commit
add
working
A simple Git workflow
1. Initialize a new project in a directory:
git init
git add .
.
After initializing a new git repo…
Repository
3. Commit changes with
a message commit
Staging index
2. Add changes
add
1. Make changes
working
A note about commit messages
• Tell what it does (present tense)
• HEAD points to parent of next commit (where writing the next commit
takes place)
Last commit
HEAD
commit
Staging
index
add
working
What changed in working directory?
git diff – compares changes to files between
repo and working directory
add
• It is not enough to delete the file in
your working directory. You must working
commit the change.
Deleting files from the repo
Moving (renaming) files
git mv filename1.txt filename2.txt
Repository
commit
Note: File file1.txt was committed to repo earlier.
Staging
index
add
working
Good news!
git init
git status
git log
git add 75% of the time you’ll be using
git commit only these commands
git diff
git rm
git mv
What if I want to undo changes made
to working directory?
git checkout something
(where “something” is a file or an entire branch)
Repository
commit
Staging
index
add
working
What if I want to undo changes
committed to the repo?
git commit --amend -m “message”
Repository
• allows one to amend a change to the last
commit commit
Staging
• anything in staging area will be amended index
to the last commit add
working
Note: To undo changes to older commits, make a new commit
HEAD
commit
Staging
index
add
working
Repository
comit
Staging
index
working
Which files are in a repo?
git ls-tree tree-ish
h4Rt5uEl9p Ge8r67elOp
new branch
HEAD changes from new
branch merged into
master
Source: http://hades.github.io/2010/01/git-your-friend-not-foe-vol-2-branches/
16 forks and 7 contributors to the master branch
Source: https://www.tablix.org/~avian/blog/archives/2014/06/vesna_drivers_git_visualization/
In which branch am I?
git branch
How do I create a new branch?
git branch new_branch_name
Note: At this point, both HEADs of the branches are pointing to the same
commit (that of master)
How do I switch to new branch?
git checkout new_branch_name
At this point, one can switch between branches, making commits, etc. in either
branch, while the two stay separate from one another.
HEAD
new_branch h4Rt5uEl9p
HEAD HEAD
Y4f7uiPRRo Pu87rRi4DD Qs2o0k64ja i7Ewd37kL9 he8o9iKlreD kle987yYieo mN34i4uwQ
HEAD
h4Rt5uEl9p Ge8r67elOp
merge conflicts
What if there are two changes to same line in two
different commits?
file1.txt file1.txt
apple banana
master new_feature
Resolving merge conflicts
Git will notate the conflict in the files!
Solutions:
1. Abort the merge using git merge –abort
2. Manually fix the conflict
3. Use a merge tool (there are many out there)
Graphing merge history
git log --graph --oneline --all --decorate
Tips to reduce merge pain
• merge often
• keep commits small/focused
• bring changes occurring to master into your
branch frequently (“tracking”)
What is ?
GitHub
• a platform to host git code repositories
• http://github.com
• launched in 2008
• Free to start
fork
GitHub Forked
master
branch
remote pull request
pull/ someone
server fetch else’s master
push
origin/master “branch”
push
merge merge
origin/master
references remote
repo server branch and
tries to stay in sync
checkout commit
Local
Stage
add
Working
directory
Important to remember
origin/master “branch”
push
merge
clone
repo
checkout commit
Local
Stage
add
Working
directory
How do I link my local repo to a remote repo?
Note: Yes! You may have more than one remote linked to your local
directory!
If you want write access and you haven’t been invited, “fork” the
repo
fork
GitHub Forked
master
branch
remote pull request
pull/ someone
server fetch else’s master
push
origin/master “branch”
push
merge merge
repo
checkout commit
Local
Stage
add
Working
directory
GitHub Gist
https://gist.github.com/
Good resources
• Git from Git: https://git-scm.com/book/en/v2
Note: If you are *really* sure that you want to delete branch
with commits
Tagging
• Git has the ability to tag specific points in history
as being important, such as releases versions
(v.1.0, 2.0, …)
git tag
Tagging
Two types of tags:
Lightweight tag
Annotated tag