A quick intro to Git and GitHub
Miguel Lazaro-Gredilla
[email protected]
June 2014
Machine Learning Group
http://www.tsc.uc3m.es/~miguel/MLG/
Contents
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Git
I
Dont confuse Git with GitHub
I
Git is a version control tool
GitHub provides cloud services using Git
(remote repositories, bug tracking, wiki page...)
1/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Git
I
Dont confuse Git with GitHub
I
Git is a version control tool
GitHub provides cloud services using Git
(remote repositories, bug tracking, wiki page...)
Git is not like Dropbox or Google Drive
I
True version control, not just file history
Need to resort to console sooner or later
1/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Git
I
Dont confuse Git with GitHub
I
Git is a version control tool
GitHub provides cloud services using Git
(remote repositories, bug tracking, wiki page...)
Git is not like Dropbox or Google Drive
I
True version control, not just file history
Need to resort to console sooner or later
Git is not like CVS, Subversion or Perforce
I
There is no need for a central (such as cloud) repository
You can work offline most of the time
Each local copy contains the full repositorys history
Devised by Linus, motivated by Linux Kernel development
1/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Git: General concepts (I/II)
I
Local operations (staging area == index)
Evolution over time
2/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Git: General concepts (II/II)
I
clone: Clone remote repository (and its full history) to your
computer
stage: Place a file in the staging area
commit: Place a file in the git directory (repository)
push: Update remote repository using local repository
pull: Update local repository using remote repository
add: Start tracking a new file, or stage a modified file
branch: An end point in the commit tree
fork: A copy of another repository for reuse
merge: Combine two commits
3/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Github: trending repositories
4/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Github: repository view
5/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Github pricing
6/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Set up Git and GitHub
Go to http://www.github.com
and create a user. Choose a nickname you like!
Then go to http://help.github.com, look forSet up Git and
download and install the native app for your platform.
In the process you should also be installing the command-line
tools. The apps arent that good, but keep them just in case.
You should be all set!
7/31
Contents
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Cloning a repository
This clones a repository (and its full history) to your computer
$ git clone https://github.com/lazarox/charla.git
I
It creates folder charla (your working directory)
It creates folder charla/.git (the local repository)
Might include text files
charla/.gitignore
charla/.gitattributes
Remote server will be referred to as origin.
8/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Possible file statuses
9/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Checking the status of things
Tells you about modified files, untracked files, staged files ready for
commit, etc. Even provides suggestions about what to do next.
Very useful!
Also informs you about which branch you are at (master)
$ cd charla
$ git status
ACTION: Put a file with your name (e.g., miguel.txt) in directory
charla and then check the status
10/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Start tracking a file
Untracked files in the working directory cant be staged or
committed. You can track a file using
$ git add miguel.txt
Multiple files can be added at a time (dont forget the quotes)
$ git add *.txt
$ git add folder
ACTION: Track the file you just added and check the status
QUESTION: was the previous diagram accurate?
ACTION: Modify the file that you just added and check the status
QUESTION: What if I commit now? How can we avoid that issue?
11/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Committing changes
Commit everything from the staging area to the repository (locally)
$ git commit -m I improved all files
The message is compulsory! Otherwise, youd avoid it and soon
forget that commits purpose
Each commit has an identifying SHA-1 hash and comment. You
can roll back to past commits.
You can stage everything that is tracked and commit in a single
step using
$ git commit -a -m I improved all files
ACTION: Commit everything.
12/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Removing and moving files
Untrack a file and delete a file, or rename/move a file are
modifications
$ git rm miguel.txt
$ git mv miguel.txt newmiguel.txt
You will need to commit this changes! As any commit, they can be
undone.
If you delete or rename files on your own, Git will notice and youll
still have to stage and commit
ACTION: Delete the file you created, check state, commit
13/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Browsing existing commits
You can see the list of previous commits with
$ git log
There are many options to filter commits, compare them, see what
was added or removed, etc.
http://git-scm.com/book/en/
Git-Basics-Viewing-the-Commit-History
ACTION: Browse your current commits
14/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Rolling back a previous commit
You can destructively roll back to a previous commit (as if
posterior commits never happened) with
git reset --hard <HASH> (to move to that commit)
git reset --hard master^ (to move one step back in master)
Or you can move to a previous commit and play around without
destroying anything
git checkout <HASH> (to move to that commit)
git checkout master^^ (to move two steps back in master)
15/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Working with remotes (such as GitHub)
You can download and merge from the remote repository you
cloned from
$ git pull origin
Or upload your local repository to the remote repository you
cloned from
$ git push origin master
This will fail if the repository is ahead of you. Pull, check
everything is fine, then push. You can force it with
$ git push --force, but that destroys the remote! Never do
that!
To get info about the remote: $ git remote show origin
ACTION: Push your changes. Try an actual merge with collisions!
16/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Browsing current changes
Differences between your working directory and staging area
$ git diff
Differences between your staging area and your repository
$ git diff --staged
17/31
Contents
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Branching
Each commit has a pointer to its parent(s) commits.
Each branch is a pointer to a concrete commit.
The HEAD is a pointer to the current branch.
When create a new commit, the current branch moves forward
to point to it
18/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Several commits
19/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Creating a new branch
$ git branch testing
The current branch is still the old one
Use $ git branch to list existing branches
20/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Moving to a different branch
$ git checkout testing
Short-hand for the previous steps: $ git checkout -b testing
You could also checkout commits, and then provide a branch name
Always commit before leaving the current branch!
21/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Bifurcations may appear (I/II)
If you now make a new commit while on branch testing...
22/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Basic merge
Given this structure
you can merge two commits using
$ git checkout master
$ git merge iss53
23/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Deleting branches
After the previous merge, we get
since iss53 is no longer needed, we can delete it
$ git branch -d iss53
24/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Solving merge conflicts
In case of conflict, no commit occurs. Instead, the working
directory has conflicting files like this
<<<<<<< HEAD
<div id="footer">contact :
[email protected]</div>
=======
<div id="footer">
please contact us at
[email protected]</div>
>>>>>>> iss53
Conflicting files are shown on $ git status
To solve conflicts, fix each file and stage it
25/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Sample workflow
26/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Remote repositories
The remote repository and its local snapshot may diverge
Option 1: Pull, solve any conflicts, and then push
Option 2: Use another branch, push it, and issue a pull request
27/31
Contents
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Using Issues
28/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Pull requests
29/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Progress tracking towards milestones
Other Features: labels, cross-referencing, mark-down, per-line
comments in commits, mentions, gists, etc.
30/31
Contents
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Exercise
1. Make sure your local repo is up-to-date (i.e, pull)
2. Dont interact again with the server unless you are told to
31/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Exercise
1. Make sure your local repo is up-to-date (i.e, pull)
2. Dont interact again with the server unless you are told to
3. Make a branch called myfixName (use your own name) at the
current commit and switch to it
31/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Exercise
1. Make sure your local repo is up-to-date (i.e, pull)
2. Dont interact again with the server unless you are told to
3. Make a branch called myfixName (use your own name) at the
current commit and switch to it
4. Go to folder fibos and fix only the file with your name on it
31/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Exercise
1. Make sure your local repo is up-to-date (i.e, pull)
2. Dont interact again with the server unless you are told to
3. Make a branch called myfixName (use your own name) at the
current commit and switch to it
4. Go to folder fibos and fix only the file with your name on it
5. There is a bug that requires your immediate attention! Switch
to branch master and pull new files from server
6. Go to folder factorials and fix only the file with your name
7. Push corrections in master to server
31/31
Introduction
Basic Git
Branching in Git
GitHub
Hands-on practice
Exercise
1. Make sure your local repo is up-to-date (i.e, pull)
2. Dont interact again with the server unless you are told to
3. Make a branch called myfixName (use your own name) at the
current commit and switch to it
4. Go to folder fibos and fix only the file with your name on it
5. There is a bug that requires your immediate attention! Switch
to branch master and pull new files from server
6. Go to folder factorials and fix only the file with your name
7. Push corrections in master to server
8. Switch to myfixName and finish corrections in folder fibos
9. Push that branch to GitHub and issue a Pull Request to get
your branch merged
31/31