Git & Github
Database Journalist, Politics - FiveThirtyEight
Democracy and Technology Fellow - Harvard Kennedy School
dhrumil.mehta@fivethirtyeight.com
@datadhrumil
@dmil
whoami # your username
hostname # my computer's network name
echo "Hello, World!" # print text to terminal
pwd # print working directory
cd # change directory
ls # list directory
cat <filepath> # print a whole file
whoami # your username
hostname # my computer's network name
echo "Hello, World!" # print text to terminal
pwd # print working directory
cd # change directory
ls # list directory
cat <filepath> # print a whole file
mkdir <directory_path> # make directory
rmdir <directory_path> # remove directory
cp <from> <to> # copy a file or directory
mv <from> <to> # move a file or directory
# Command
ls
# Command with flag
ls --all
# Shorthand flag
ls -a
ls -l
ls -la
# Command with argument
ls folder/
# Command with flags and argument
ls -la folder/
# Manual, to learn all the different flags and arguments
man ls
# or
ls --help
echo "Hello, World." > hello.txt
# ">" Grabs the output from the command on the
# left and feeds it to the file on the right
echo "Goodbye, World." >> hello.txt
# ">>" is similar to ">" except if the file already
# exits it will append to the end of it rather than
# overwriting the file
grep 'ood' hello.txt
# grep is a command that searches a file for a
# regular expression, we can try it on hello.txt
ls -a1 | grep .txt
# "|" takes the output of the command to the left and
# uses it as the input for the command to the right
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Make a folder on the Desktop with a file called index.html
index.html
mkdir ~/Desktop/dhrumil-website
cd ~/Desktop/dhrumil-website
touch index.html
# Initialize the local directory as a Git repository.
git init
# View which files are in the staging area
git status
# Add the files to your new repository's staging area.
git add index.html
# View which files are in the staging area
git status
# Commit the files that you've staged in your local repository
# with a descriptive commit message.
git commit -m "add index page"
git add index.html
git commit -m "add index page"
index.html
.git
Example:
<!DOCTYPE html>
<html>
<body>
<h1>Dhrumil Mehta</h1>
<p>This is my simple website.</p>
<a href="http://www.github.com/dmil">Find me on Github!</a>
</body>
</html>
git log
Create an empty github repository: https://github.com/new
# Sets the new remote
git remote add origin <remote repository URL>
# Verifies the new remote URL
git remote -v
# Push the changes in your local repository to GitHub.
git push origin master
git push
git add index.html
git commit -m "add index page"
index.html
.git
cd ~/Desktop
git clone <remote_url>
# See your branches
git branch
# Checkout a new branch
git checkout -b <branch_name>
# push the branch to github
git push origin <branch_name>
# Checkout an existing branch
git checkout <branch_name>
Create a branch called "endorsement"
Write something nice about your neighbor
Commit the change and issue a "pull request"
(Open Source Collaboration Model)
http://stackoverflow.com/questions/3611256/forking-vs-branching-in-github
(Open Source Collaboration Model)
# 1) Fork this repo in github
# https://github.com/dmil/learning-github
# 2) Make a local clone of your fork
git clone <url_of_your_fork>
# 3) Add yourself to the attendance list
echo "Dhrumil Mehta" >> README.md
# 4) Commit your changes
git add README.md
git commit -m "add dhrumil to attendance"
# 5) Push the file to your own github
git push origin master
# 6) Issue a pull request to me on github
# 7) Wait for everyone to finish and for me to merge
# the pull requests into the upstream branch.
# 8) Pull the changes from upstream to get the full attendance list
git remote add upstream https://github.com/dmil/tools-of-the-trade.git
git pull upstream
# 9) Push back to your repo
git push origin master
dhrumil.mehta@fivethirtyeight.com
@datadhrumil
@dmil