git
Follow the instructions for your OS here.
Git client
See Materials for the course for links to different possibilities.
“Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency” (Wikipedia)
A Version Control System (VCS) is a methodology or tool that…
Synchronized vs Asynchronized Solution to the problem of collaborative editing.
“A practice by which dispersed users are able to concurrently modify shared artifacts with the guarantee that changes by different users will not automatically overwrite each other” (Altmanninger et al. 2009)
Collaborative Text Editors
VCS
git
?“GitHub is a web-based Git repository hosting service, which offers all of the distributed revision control and source code management (SCM) functionality of Git as well as adding its own features.” (Wikipedia)
but there are others, see BitBucket
Let’s introduce a bit of vocabulary (Last bit of me talking alone!)
See the repository README for a glossary of terms
All tracked files in your project plus the full history of changes (commits and branches).
Ways to change the working tree:
revert
)commit
)checkout
)A snapshot including changes to a previous state of the repository.
Any of the parallel states in the repository (a particular commit history).
For this tutorial, we are going to create a (very basic) resume homepage.
Create the remote
Sync locally (set a local to track the remote)
Initialize the repository
$ git init
Initialized empty Git repository in /home/enrique/Documents/courses/resume
Add files to the repository
$ git add --all
Track the remote
$ git remote add origin https://github.com/emanjavacas/resume.git
Commit changes to the remote
$ git push -u origin master
Clone the remote
Alternatively, click on Set up in Desktop (same effect)
The git workflow
Make edit
Checking the status of the Working Tree
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: index.html no changes added to commit (use "git add" and/or "git commit -a")
Diffing
Add (stage)
$ git add index.html
Commit
$ git commit -m "Changed name"
[master 9a23987] Changed name
1 file changed, 2 insertions(+), 2 deletions(-)
Push
$ git push Counting objects: 3, done. Delta compression using up to 2 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 395 bytes | 0 bytes/s, done. Total 3 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 2 local objects. To https://github.com/emanjavacas/resume.git eb6121f..9a23987 master -> master
Add (stage) & commit
Push (sync)
index.html
Make changes to multiple files
Check the changes
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: index.html modified: css/main.css modified: images/spectrum.jpg
Git flow (add, commit)
Add email update
$ git add index.html
$ git commit -m "Changed mailto"
Add background update (both files at once)
$ git add css/main.css images/spectrum.jpg
$ git commit -m "Changed background"
Push
$ git push Pushing to https://github.com/emanjavacas/resume.git Counting objects: 16, done. Delta compression using up to 2 threads. Writing objects: 100% (16/16), 252.52 KiB | 0 bytes/s, done. Total 16 (delta 7), reused 0 (delta 0) POST git-receive-pack (258725 bytes) remote: Resolving deltas: 100% (7/7), completed with 3 local objects. To https://github.com/emanjavacas/resume.git 9a23987..b45d6be master -> master
Add (stage) & commit
Add (stage) & commit multiple changes
Push both commits
main.css
We are going to edit the content in the “resume” tab
Create a new branch
Create branch
$ git branch resume
Move to it (checkout)
$ git checkout resume
Switched to branch 'resume'
Short version
$ git checkout -b resume
See which branches are there
$ git branch -v master 0a7be54 Changed background * resume 299b6f9 Changed background
Switch to a particular branch (magic!)
$ git checkout master
Switched to branch 'master'
Create a branch
See all branches
Select a branch
Example of changes
Compare branches
$ git diff resume master
Merge source branch (resume) into target branch (master)
Switch to target branch (master)
git checkout master
Do the merge (’resume’ gets merged into ’master’)
$ git merge resume Updating 299b6f9..5032375 Fast-forward index.html | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)
Push
$ git push
Switch to target branch (in our case, master)
Select “merge from resume”
A Merge is just a commit
Push (Sync)
Merges are not restricted to local branches. You can also merge:
Now you are going to merge a pull request that your colleague is going to submit.
Introduce (evident) typos in your local index.html file (push them too)
Fork (i.e. “copy”) your colleague’s project on their GitHub repository page
Clone the fork locally (to be able to modify the project)
Modify the typos and commit
Issue a pull request to your colleague’s repository
Merge your colleague’s pull request into your master branch
That was easy, but sometimes merging doesn’t work automatically.
Explanation of git automerge algorithm here.
We are going to do the following
Modify your local repository (change the Twitter handle)
Also modify your local copy of the repo you forked from your colleague (introduce a conflict)
Issue pull request to your colleagues with your conflicting changes
Check your colleague’s incoming pull request
Resolving merge conflicts (two solutions)
Solve the conflicts yourself and merge
To proceed you need a command line (use GitHub Desktop built-in)Create a local branch to pull your colleague’s fork branch to
$ git checkout -b yourcolleagueusername-branch starting-branch
Pull from the remote branch into your local branch
$ git pull https://github.com/yourcolleageusername/resume.git branch
Switch to the target branch
$ git checkout starting-branch
Attempt merge
$ git merge yourcolleaguesusername-branch
Conflict syntax
Solve the conflict
Revert is an operation that is less common than what you might think.
The “git philosophy” encourage you to try things in a new branch and only merge if everything worked.
username.github.io
, you can get your own personal page served by GitHub