Skip to content

Instantly share code, notes, and snippets.

@winnab
Created December 10, 2014 19:47
Show Gist options
  • Save winnab/a00689aa260c4239a0d6 to your computer and use it in GitHub Desktop.
Save winnab/a00689aa260c4239a0d6 to your computer and use it in GitHub Desktop.

Git and Github

Reading and tuts

Creating your SSH Key

$ cd ~/.ssh

# if ~/.ssh doesn’t exist:
$ mkdir ~/.ssh
$ cd ~/.ssh

$ ssh-keygen -t rsa -C put_your_email_here
# use the email you use for your Github account

# Ignore the returned passphrase message, 
# or set it to something simple and memberable (e.g. your computer password). 
# When typing it, nothing will show. That’s normal.

$ pbcopy < ~/.ssh/id_rsa.pub
# copies your key to your clipboard

Add your key to github.com

  • Log in to github
  • In the top right corner of the page, click on the gear icon
  • On the profile page, under the Personal Settings menu on the left side of the page, click SSH Keys
  • Click Add SSH Key
  • Give the key a title
  • Paste (cmd + v) your key from the command line into the text input box
  • Click Add Key and enter your password if needed

Check that your key works on your machine

$ ssh -T git@github.com
# enter your password

If you don't have errors then Github is now fully operational on your machine!

Save your github username and email on your computer

$ git config --global user.name put_your_github_username
# sets your github username on your computer
$ git config --global user.email put_your_github_email 
# sets your github email on your computer

Getting started with Git

Misc commands

$ git init # initializes repository in current directory 
$ git add <file name> # stages (adds) changes for specified file
$ git add . # stages (adds) changes from the whole project
$ git status # tells what is currently staged and untracked
$ git commit -am “Initial commit” 
# makes a git commit (snapshot)
# -am is a shortcut to add and commit ALL files 
$ git commit -m “Description of the changes made” 
# makes a commit of the current version of the file
$ git rm # unstages/removes file from git
$ git log # gives a history of our commits with their IDs
$ q # quits the git log screen
$ git remote -v # lists remote versions
$ git log --oneline  # gives a simpler version of git log
$ git config list  # lists  your settings
$ git config --global user.name “your name” # sets your name
$ git config --global user.email “your email” # sets your email

Rolling back changes

After staging

$ git reset <filename> 
# unstages file changes (HEAD refers to our last commit on current branch)
$ git checkout <filename> 
# remove the changes

After committing

Undoing
$ git reset --soft <commit ID> 
# reverts back after changes that have been committed. 
# --soft will keep these changes.
$ git reset --hard <commit ID>  
# reverts back after changes have been committed. 
# --hard will destroy these changes.

Creating a new Repo from the GitHub website

  • On github, click on the + in the top right corner
  • Click New Repository
  • Give your repo a name--usually hyphen-delimited
  • Choose public (or pay to create a private repo)
  • Click to add a README if you want--it gets desplayed as the content on the webpage for your repo
  • We can now return to our terminal and create a the repository on our local machine:
$ git remote add origin <SSH address>
$ git push -u origin master # -u tells github that the remote branch has the same name as our local branch. next time, we can just use git push
  • If we now return to github and see the page for our repository, all the files which we have just committed and pushed should show.

Branching

$ git branch <branch name> # create a new branch 
$ git checkout <branch name> # move to this branch
$ git checkout -b <branch name>   # create and move to a branch
$ git checkout master # move to the master branch
$ git push origin <branch name>  # push branch up to github.com
$ git merge <branch name> # merge <branch name> branch onto your current branch (or onto master, if you’re on master).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment