Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Version control allows you (and your team) to do two powerful things
Create anything with other people, from academic papers to entire websites and applications.
Mistakes happen. Wouldn't it be nice if you could see the changes that have been made and go "back in time" to fix something that went wrong?
The Horror!
Beautiful Organization! 😍
1990s -- CVS (Concurrent Version Systems)
2000s -- SVN (Apache Subversion)
2005 -- Git (well, Git)
Examples: CVS, SVN
One central server, each client (person) checks out and merges changes to main server
Examples: Git, Mercurial
Each client (person) has a local repository, which they can then reconcile with the main server.
Goals of Git Design
Git v Github
Let's download Sublime Text 2 to use as a text editor. If you already use a different text editor for coding, feel free to use that instead (Atom, Brackets, etc - just not a word processor like Microsoft Word)
Sublime Text 2Open the application and you should see a screen like this:
If you don't have one already, create a GitHub account. We'll need it later on, so keep track of your username and password in case you get logged out.
GithubInstall git
Setup ssh keys
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "your_email@example.com"
# Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]
Get SSH Key
Your identification has been saved in /Users/YOUR_USERNAME/.ssh/id_rsa.
# Your public key has been saved in /Users/YOUR_USERNAME/.ssh/id_rsa.pub.
# An example of the key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
#
# To find your username to replace YOUR_USERNAME, type:
$ pwd
# pwd prints your working directory which should contain your username
Add SSH Key to Github
Setup name and email in gitconfig
Your name will be visible on your commit history. This can be updated at any time.
$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
$ git config --global user.email "your_email@example.com"
# Sets the default email for git to use when you commit
$ git config --list
Go to home directory
$ cd ~/
# OR
$ cd Users\username
Create a "working directory"
$ mkdir my-first-repo
$ cd my-first-repo
Initialize repository with Git
$ git init
$ git status
Create a new hello_world.txt file in your new folder
Check repo status
$ git status
Tell Git to track our new file
$ git add hello_world.txt
$ git status
File is now tracked by Git
Open hello_world.txt and add some more text
$ git status
Stage and commit the change
$ git add hello_world.txt
$ git commit -m "First commit. Added hello world to repository."
How is this all different than just saving a file?
$ git log
commit [HASH HERE]
Author: Your name
Date: [DATE HERE]
First commit. Added hello world to repository.
If you haven't committed yet
Open hello_world.txt and add some new text
$ # change hello_world.txt
$ git checkout hello_world.txt
Look at hello_world.txt. Your changes are gone.
Open hello_world.txt and add some new text
$ git add hello_world.txt
$ git reset HEAD hello_world.txt
$ git checkout hello_world.txt
Look at hello_world.txt. Your changes are gone.
Open hello_world.txt and add some new text
$ git add hello_world.txt
$ git commit -am "Changing and committing some lines"
$ git log --pretty=oneline
$ git revert [HASH]
Look at hello_world.txt. Your changes are gone.
Create new file my_new_file.txt
$ git add my_new_file.txt
$ git reset my_new_file.txt
Create new file my_other_file.txt
$ git add my_other_file.txt
Manually delete your file
$ git rm my_other_file.txt
Create a new branch called version2
$ git checkout -b version2
Add new lines to hello_world.txt
$ git add hello_world.txt
$ git commit -m "Adding changes to version 2"
See all branches. Branch with * is active
$ git branch
Switch to master and look at hello_world.txt
$ git checkout master
Switch to version2 and look at hello_world.txt
$ git checkout version2
Switch to master and merge changes
$ git checkout master
$ git merge version2
*rebase is another option, but will not be covered in this workshop
Don't force a push unless you really know what you are doing
Change first line in hello_world.txt in master branch
$ git add hello_world.txt
$ git commit -m "Changing first line in master"
Change first line in hello_world.txt in version2 branch
$ git checkout version2
# open hello_world.txt and change first line
$ git add hello_world.txt
$ git commit -m "Changing first line in version2"
Merge from master into version2
$ git merge master
You will be notified of a conflict. Go to the file and fix the problem. Then commit your edits.
$ git merge master
# Auto-merging hello_world.txt
# CONFLICT (content): Merge conflict in hello_world.txt
# Automatic merge failed; fix conflicts and then commit the result.
#In our txt file:
<<<<<<< HEAD
Hello World! This change was made on my version2 branch
=======
Hello World! This change was made on my master branch, how exciting!
>>>>>>> master
Let's git this together
$ git add hello_world.txt
$ git commit -m "Merged master fixed conflicts."
# Recorded resolution for 'hello_world.txt'.
# [645c4e6] Merged master fixed conflict.
While a README isn't a required part of a GitHub repository, it is a very good idea to have one. READMEs are a great place to describe your project or add some documentation such as how to install or use your project. You might want to include contact information - if your project becomes popular people will want to help you out.
$ cd ../ # Back in root directory
$ mkdir hello-github
$ cd hello-github
$ git init
$ git remote add origin git@github.com:username/NAME-OF-REPO
$ git pull origin master
Edit the ReadMe file
$ git add README
$ git commit -m "Updating readme file"
$ git push origin master
Go look at your github repo online
If you are working with a team, you want to make sure that you have everyone's changes before pushing your changes to the GitHub repo
# Commit local changes
$ git commit -m "My latest commit"
# Pull changes other people have made
$ git pull origin master
# Fix any conflicts (see merge conflicts above) and commit
$ git commit -m "Fixing merging conflicts"
# push local changes to GitHub
$ git push origin master
Clone to get a local repository of your fork
$ cd ../
$ git clone https://github.com/username/FORKED-REPO-NAME.git
$ cd FORKED-REPO-NAME
$ git remote add upstream https://github.com/original-username/FORKED-REPO-NAME.git
# Assigns the original repository to a remote called "upstream"
$ git fetch upstream
# Pulls in changes not present in your local repository, without modifying your files
How to manage pull requests is out of the scope of this short workshop, but you can learn more from the Github Collaborating Tutorials
You made it through and we'd love to hear what you think! Please leave some feedback with this 5 minute survey