Skip to content

Instantly share code, notes, and snippets.

@ysadka
Last active December 21, 2015 14:59
Show Gist options
  • Save ysadka/6323934 to your computer and use it in GitHub Desktop.
Save ysadka/6323934 to your computer and use it in GitHub Desktop.
Intro to Git/Github

##Initializing a new repo

Link to video

Type in what comes AFTER the $...do not type in the $

BASIC git initialization and repo creation:

$ git init

This command creates an empty git repository - basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial HEAD file that references the HEAD of the master branch is also created.

Now, choose which file to be added - in this case, 'my_file.rb'

$ git add my_file.rb

Using a '.' instead of a file name will add all files and folders within the CURRENT WORKING DIRECTORY

Don't know what you added? Use the following:

$ git status

And you'll see:

# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#  new file:   my_file.rb

Once you have your files added, commit them

$ git commit -m 'message goes here in between quotes (single or double) '

This commits the added files/folders along with the message. Now, you'll want to give it the info on your repo and tell it where your origin master is located:

Step required only if this is a brand new repo, not a clone

$ git remote add origin https://github.com/YOUR_GITHUB_NAME/REPO_NAME.git

You can just copy paste the above line once you create your repo on github.com. This can also be done with an SSH key but we aren't using that today.

The last piece, push your local files up to 'The Clooouuud'

$ git push origin master

And you'll see something along the lines of:

Counting objects: 3, done.
Writing objects: 100% (3/3), 208 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/ysadka/git_tutorial.git
 * [new branch]      master -> master

Want to see that your origins are set up correctly?

$ git remote -v

Returned (if your github name is ysadka and the repo name is git_tutorial) :

origin  https://github.com/ysadka/git_tutorial.git (fetch)
origin  https://github.com/ysadka/git_tutorial.git (push)

Basic Git steps (once repo has been created)

AWESOME! You've created your new repo and linked it with the local file on your machine!

Now that everything is setup the rest is pretty basic.

You continue adding files:

$ git add my_one_file_to_add.rb

OR if you have multiple files

$ git add .

Note: when using a '.' it will add all files that are in that working directory.

Don't know what you've added? Use:

$ git status

and receive a printout of your "stage". The stage area will show you files that have been commited, and files which have been edited but not yet commited.

Made an error? Not to worry, revert adding the file with:

$ git reset <file> 

It might also be worth mentioning that

$ git rm <file>

also removes the file from the commit, but additionally deletes the file from the project

-labe

You've added and now to commit them:

$ git commit -m "I am commiting new updates to my project"

A commit is like a checkpoint in a game. It gives you a reference point to refer to toand commiting often will make it easier to digest what changes were commited.

Pulling, to make sure you're up to date:

git pull origin master

And pushing up your commits!

$ git push origin master
def repeat_these_steps
  
  add files
  
  git_status if unsure?
  
  commit those files
  pull branch
  push commits to THE CLOOUUUD!
  
  repeat_these_steps
  
end

TIP: To avoid merge conflicts pull early and pull often. Unlike pushing you don't have to commit in order to pull. Think of pulling as an update to your local repo.

So you screwed up and now you have to remove that commit...bummer. Luckily, git provides you with a way to do this. There are 2 ways to go about this, $ git reset <commit> or $ git revert <commit>.

What's the difference?

$ git reset will backtrack and remove the commit $ git revert will create a new commit that has the exact opposite of the commit you are trying to reverse (i.e. a line of code removed in the origional commit will now be added - similar to up/down when talking about migrations.)

While $ git reset is nice for local changes, it doesn't mesh well with commits that have been pushed. So, you use revert to backtrack...by moving forward

Branching

So we now know how to add files, commit them, and push them up.

BUT...there's always a but

WE NEVER WORK ON MASTER!!!
NEVER EVER!!!!!
SERIOUSLY, YOU SHOULD NEVER WORK ON MASTER!!!
NEVER NEVER NEVER NEVER!!!

So what do we do? We create a 'branch'. Think of master as the tree trunk.A trunck always keeps growing straight up - or is meant to - and rarely grows sideways. Same thing here. Master is the trunck and we branch off of it to create alterations.

Let's create a branch!

$ git checkout -b branch_name

Now, just like on master, I make changes to my files. The only difference is that now my changes don't affect the master, only the branch I'm on. This prevents errors from going live and lets me F-around without breaking my current master.

I can always go back to my master branch by typing:

$ git checkout master

Notice there is no '-b' as master is an existing branch. Cool, now lets go back to our branch:

$ git checkout branch_name

Now I'm on the branch, all my changes work, and I'm ready to push to master. What do I do?

  1. Checkout local master
$ git checkout master
  1. Pull origin master (incase someone pushed some changes since you've branched)
$ git pull origin master
  1. Checkout branch you're merging in
$ git checkout branch_name
  1. Merge master into your branch
$ git merge master
  1. Checkout master
$ git checkout master
  1. Merge your updated branch into local master
$ git merge branch_name
  1. Push updated local master up to the origin master (THE CLOOUUUDD)
$ git push origin master

Quick and dirty:

  1. Checkout Master
  2. Pull Master
  3. Checkout Branch
  4. Merge Master into Branch
  5. Checkout Master
  6. Merge Branch into Master
  7. Pull Master (optional safety step)
  8. Push Master to Origin Master

Sometimes you'll see people type in strange commands like

$ git co branch_name

instead of

$ git checkout branch_name

You're able to do this be creating an 'alias' in your bash profile. How do we do this?

First, open up your '.bash_profile' document in your editor of choice. Mine would look like this:

~ $ subl .bash_profile

In the file you'll add prefered aliases like such:

alias co="checkout"
alias add="git add"
...

...that's it! Now save the file and restart your terminal window for the changes to take effect! It may not look like much but each keystroke adds up and you'll appreciate this later. So, alisas away!

@ysadka
Copy link
Author

ysadka commented Sep 2, 2013

Added! Thanks @labe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment