Slides: bit.ly/gdic-git-workshop-2016
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
Version control allows you (and/or your team) to:
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?
Do you have files somewhere that look like this?
Resume-September2013.docx
Resume-for-Duke-job.docx
ResumeOLD.docx
ResumeNEW.docx
ResumeREALLYREALLYNEW.docx
1990s -- CVS (Concurrent Version Systems)
2000s -- SVN (Apache Subversion)
2005 -- Git
Examples: CVS, SVN
One central server, each person checks out and merges changes to main server.
Examples: Git, Mercurial
Each person has a local copy of the project, which they can then reconcile with the main server.
Examples: CVS, SVN
Examples: Git, Mercurial
Source: Google Trends
Set your name and email:
$ git config --global user.name "YOUR_NAME_HERE"
$ git config --global user.email "YOUR_EMAIL_HERE"
View your Git config:
$ git config --list
1. Go to your home directory.
$ cd ~/
OR
$ cd Users\username
2. Create a "working directory".
$ mkdir my-repo
$ cd my-repo
3. Initialize your directory as a local Git repository.
$ git init
$ git status
1. Create a new file in your folder called "kitten.txt".
$ touch kitten.txt
2. Check repo status.
$ git status
3. Tell Git to track our new file with the "git add".
$ git add kitten.txt
$ git status
Success! Your file is now tracked by Git.
1. Open kitten.txt, add some text, and save it.
$ git status
2. Stage your changes and check the status.
$ git add kitten.txt
$ git status
3. Commit the changes. Include a commit message that describes the changes.
$ git commit -m "Initial commit. Add kitten.txt to repository."
How is this all different than just saving a file?
$ git log
commit 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b
Author: Your name <you@your-email.com>
Date: Thu Feb 05 16:01:22 2016 -0700
Initial commit. Add kitten.txt to repository.
** Don't forget to run git status regularly so that you can see what is happening after each step! **
Source: Giphy (also, Simone Giertz!)
If you haven't added/committed yet
Open kitten.txt and make some changes or add something new. Then:
$ git checkout kitten.txt
Look at kitten.txt. Your uncommitted changes are gone.
$ git add possum.txt
$ git status
$ git reset possum.txt
$ git status
The file is removed from staging, but your working copy is unchanged.
Open kitten.txt in your editor and add some new text.
$ git add kitten.txt
$ git reset HEAD kitten.txt
$ git status # the file has been unstaged.
$ git checkout kitten.txt #resets the working copy to its state at the last commit
Now look at kitten.txt in your editor again: your changes are gone, and the file is removed from staging.
** HEAD refers to the most recent commit on the current branch.
Git lets you go back to any previous commit.
Open kitten.txt and add some new text. Then commit your changes:
$ git add kitten.txt
$ git status
$ git commit -m "Make a change I will soon regret making"
$ git log --oneline
# you should see (at least) two commits here at this point
# copy the short form of the hash id
Use git revert:
$ git revert 53d23c4
# Your default editor will open here
# you can just save it and close it as is.
$ git log --oneline
Notice that the original, regrettable commit is still there, but now you also have another commit that undoes the changes introduced by the original one.
A branch is essentially another copy of your repo that will allow you to isolate changes and leave the original copy untouched. You can later choose to combine these changes in whole or part with the "master" copy, or not.
Create and switch over to a new branch called "feature".
$ git checkout -b feature
Add new lines to kitten.txt.
$ git add kitten.txt
$ git commit -m "Add changes to kitten.txt on feature branch."
View all your local branches. Your active branch, the one you're "on," is marked with an *.
$ git branch
Switch to master branch and look at the commit history.
$ git checkout master
$ git log --oneline
Switch to feature branch and look at the commit history.
$ git checkout feature
$ git log --oneline
Merge to get changes from one branch into another.
Switch to master and merge changes.
$ git checkout master
$ git merge feature
$ git log --oneline
Change the first line in kitten.txt in master branch.
$ git add kitten.txt
$ git commit -m "Change kitten.txt in master."
Now change first line in kitten.txt in feature branch.
$ git checkout feature
# open kitten.txt and change the first line
$ git add kitten.txt
$ git commit -m "Change kitten.txt in feature."
Merge the changes from master into the feature branch.
$ git merge master
# Remember, you are on the feature branch here.
You will be notified of a conflict.
Go to kitten.txt in your editor and you'll see something like:
<<<<<<< HEAD
Changing the first line on feature.
=======
Changing the first line on master.
>>>>>>> master
Fix the problem. Then add and commit your edits.
Sign up for an account: github.com
You will need to be logged into your GitHub account to do this.
Set up your remote repository to sync with your local repository.
After you click the big green button to create your repo, follow GitHub's instructions for next steps.
$ git remote add origin https://github.com/YOUR-GITHUB-USERNAME/REPO.git
$ git push -u origin master
# With the -u option, you are setting a default.
# You only need to use this flag the first time.
Now check out your GitHub repo online!
Think
not
If you want to use or contribute to a repository, you can fork it. A fork is just a copy of a repository, saved to GitHub.
Let's practice forking!
To get a local copy of the fork you just made, use the git clone command.
# Change out of your my-repo directory.
$ cd ../
# Create a copy of the tiny-repo project in your home directory.
$ git clone https://github.com/your-github-username/tiny-repo.git
$ cd tiny-repo
To sync your fork with the original repo, you need to add another remote named upstream
$ git remote -v
$ git remote add upstream https://github.com/amygori/tiny-repo.git
$ git remote -v
If team members are contributing to a single repo, each member of the team will want to make sure that she has everyone else's changes before pushing her own changes to the GitHub repo.
$ git commit -m "My latest commit."
Get changes that have been pushed to the remote repo:
$ git pull origin master
$ git commit -m "Fix merge conflicts."
Now you are ready to push local changes to GitHub.
$ git push origin master
You need to do this on GitHub, not from the command line.
If you are the owner of the repo, you will review and decide whether to merge in the pull requests you receive.
You can learn more from the
Github Collaborating Tutorials.