"The version control system that was ... designed to make you feel less intelligent" (source)
Setup name and email in gitconfig
$ git config --global user.name "Your Name Here" # Sets the default name for Git to use when you commit
$ git config --global user.email "email@example.com" # Sets the default email for Git to use when you commit
$ git config --list
By default Git is set up to use Vim as the text editor.(esc + :q or :q! to get out of Vim)
Follow these instructions to change your default text editor to whatever you prefer.
BONUS LEVEL: Set up your GitHub account and authentication from Git.
Version control is a tool that allows you 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-September2016.docx Resume-for-Duke-job.docx ResumeOLD.docx ResumeNEW.docx ResumeREALLYREALLYNEW.docx
One central server, each client (person) checks out and merges changes to main server
Examples: CVS, Subversion (SVN), Perforce
Each client (person) has a local repository, which they can then reconcile with the main server.
Examples: Git, Mercurial
1. Go to your home directory
$ cd OR $ cd Users/username
2. Create a new "working directory" and cd into it
$ mkdir my-repo $ cd my-repo
3. Initialize it as a local Git repository
# make sure you are in the right directory! $ pwd $ git init $ git status
1. Create a new file in your new folder named kitten.txt
$ touch kitten.txt
2. Check the status of your repo with git status
$ git status
3. Tell Git to track our new file with git add
$ git add kitten.txt $ git status
Success! The file you just added is now tracked by Git
1. Open kitten.txt, add some text, and save it
$ git status
2. Stage the change and check the status
$ git add kitten.txt $ git status
3. Commit the change with a good commit message that explains and describes what you did
$ git commit -m "First commit. Added kitten.txt to repository."
$ git log
commit 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b Author: Your name <firstname.lastname@example.org> Date: Tues May 23 16:01:22 2017 -0700 First commit. Added kitten.txt to repository.
What you see in your editor and where you make your changes
A snapshot of your working tree at a particular point in development
lets you gather changes for the next commit
Your copy of a project, initialized as a Git repository
(i.e., it has a .git directory)
The shared copy of the repo that lives on a remote server
Very often this is GitHub, but it doesn't have to be.
Conventionally named origin but doesn't have to be.
You decide what goes into version control.You can, and should, leave some things out.
libraries, .dotfiles, api keys...
The.gitignore file at the root of your project directory
Check out gitignore.io for some suggestions.
Don't worry. Git is your friend.
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 in your editor: your changes are gone (you've gone back to the previous commit state).
$ git add possum.txt $ git status $ git reset possum.txt $ git status
The file is removed from staging, but your working copy will be 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 the same file in your editor again: your changes are gone, and the file is removed from staging.
Git lets you go back to any previous commit.
Open kitten.txt and add some new text
$ 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
$ git reset --soft 53d23c4 $ git log --oneline
Notice that the commit is gone from your log, but your changes are still in your editor.
$ git reset --hard 53d23c4 $ git log --oneline
Notice that the commit is gone from your log, AND your changes are removed in your editor.
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.
Branches are good for features!
Branches are cheap!
Create a new branch called feature
$ git checkout -b feature
Add new lines to kitten.txt
$ git add kitten.txt $ git commit -m "Adding changes to feature" $ git log --oneline
See 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
Switch to master and merge changes
$ git checkout master $ git merge feature $ git log --oneline
When you merge, you create a new commit on the branch you just merged into
You will see this in the affected file your text editor
Here are lines that are either unchanged from the common ancestor, or cleanly resolved because only one side changed. <<<<<<< yours:sample.txt Your changes are reflected here in this section. ======= Their changes are here in this section, in conflict with yours. >>>>>>> theirs:sample.txt And here is another line that is cleanly resolved or unmodified.
Change the first line in kitten.txt in master branch
$ git add kitten.txt $ git commit -m "Changing kitten in master"
Now change the 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 "Changing kitten 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 the file in your editor and fix the problem. Then add and commit your edits.
The merge conflict occurred because the feature branch (which is based off of master) both had divergent histories for the same file.
GitHub has over 20 million users,
and over 57 million repositories
You will need to be logged into your GitHub account to do this.
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 # that -u is an option that signals that you are setting # a tracking reference to the remote branch as the default; # you only need to use this flag the first time
Now check out your GitHub repo online!
FORK a repo: Find some code you want to use and grab a copy of it.
(Then you'll also need to CLONE the repo — that is, make your own local copy of it)
PUSH to a remote repo you own: post some code you want others to see.
submit a PULL REQUEST to the owner of a repo you'd like to contribute to.
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.
$ cd ../ $ git clone https://github.com/your-github-username/tiny-repo.git $ cd tiny-repo $ git remote -v
To sync your fork with the original repo, you need to add another remote named upstream
Downloads Git references not present in your local repository, but does not modify your files (think of it like getting a table of contents rather than the contents themselves)
$ git remote -v $ git remote add upstream https://github.com/gdisf/tiny-repo.git $ git fetch upstream
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.
Get changes that have been pushed to the remote repo
$ git commit -m "My latest commit"
Git may prompt you to fix any conflicts, then commit
$ git pull origin master
Now you are ready to push local changes to GitHub
$ git commit -m "Fixing merging conflicts"
$ git push origin master
You need to do this on GitHub, not from the command line.
If you are the owner of repo, you will review and decide whether to merge in the pull requests you receive.
You can learn more from the
Github Collaborating Tutorials.
To make changes to an existing pull request, commit changes to the same branch, and push them to GitHub. They'll be added to the PR automatically.
$ git pull origin my-pr-branch $ git commit -m "Fix typos" $ git push origin my-pr-branch
Because any new commits to a branch will be added to that branch's pull request, you need to make any new, unrelated changes to a new branch.
$ git checkout master $ git checkout -b new-feature-branch $ git commit -m "Add a fancy new feature" $ git push origin new-feature-branch
Be sure the new branch is based off of the master branch, and not your other working branch.