Collaborate with

Git & GitHub

{{ for fun & profit }}

WHAT we'll Cover

understand the working index, staging, and committing

use branches effectively

get yourself out of a git mess

What is a REPO

Subtitle

It's all about the history

$ git log --oneline

the magical realms of git

The workspace / working tree

$ git status

$ git status -v

what you see in your editor and where you make your changes

The staging area / index / cache

$ git add .

$ git add app/controllers/application_controller.rb

$ git add -p app/controllers/application_controller.rb

a snapshot of your working tree at a particular point in development

lets you gather changes for the next commit

Your local repo

$ git commit

$ git commit -m 'Initial commit'

$ git commit -v

Your copy of a project, initialized as a git repository

(i.e., it has a .git directory)

PRO TIP: if you aren't into vim, set your own code editor in git config

The remote repo

$ git pull origin master

$ git push origin master

The shared copy of the repo that lives on a remote server

Very often this is GitHub, but it doesn't have to be.

PRO TIP: Always pull before you push!

Conventionally named

origin

but doesn't have to be.

 

 

Anatomy of a commit

commit e83c5163316f89bfbde7d9ab23ca2e25604af29
Author: Linus Torvalds <torvalds@ppc970.osdl.org>

Date: Thu Apr 7 15:13:13 2005 -0700

Initial revision of "git", the information manager from hell
$ git show e83c516

A branch is essentially another copy of your repo that will allow you to isolate changes and leave the original copy untouched. 

BRANCHING

You can later choose to combine these changes in whole or part with the another branch or the mainline of development, or not.

The Production branch

MASTER

$ git branch

$ git checkout -b my_new_feature

$ git checkout master

$ git checkout -

This is the default branch that a new git repo comes with. It is often used as a production branch.

(Don't introduce broken code to the production branch!)

A Feature Branch (of many)

This is where you will do your development work. 

Break stuff here!

TEAM WOrkflow

  1. Establish a production branch (master)
  2. Each dev creates a feature branch
  3. Devs make commits and test their feature
  4. Devs keep their branches up to date with master
  5. OPTIONAL: dev opens a pull request
  6. OPTIONAL: code review; discuss changes
  7. push changes to feature if needed
  8. merge the feature to master
  9. deploy!

* This is basically the GitHub Flow.

There are other established workflows out there.

Your ASsignment workflow

👆This command creates the branch and switches you to it.

3. Do work here. Make your commit(s).

1. Clone the assignment repo

$ git checkout -b development
$ git status
$ git add <filename>
$ git commit -m "Center main image"

 4. Push your branch to the remote repo

2. Create a development branch

$ git push origin development

 5. Create a new pull request on GitHub, with notes

PUSH OFTEN

COMMIT OFTEN

PULL REQUEST
when your development branch is ready

PRO TIPS

WRITE GOOD COMMIT MESSAGES

Say why a change is needed or what it does.

git status...git status...git status

WE ALL MAKE MISTAKES

Don't worry. Git is your friend.

and the file has gone back to its state at the previous commit.

UNDOING CHANGES IN YOUR WORKING TREE

If you haven't added (staged) files  or committed yet.

$ git checkout app/views/layouts/application.html.erb

...Your changes are gone from the working tree (and your editor)

The file is removed from staging, but your working copy will be unchanged.

UN-STAGING A FILE 

$ git reset app/views/layouts/application.html.erb

When you want to keep the changes in your working tree

 $ git checkout app/controllers/main_controller.rb

DIscarding CHANGES completely

Your changes are gone from the working tree.

Reset the working tree to its state at the last commit

if you have not staged:

if you have ALREADY staged:

Just reset the file first to unstage it. 

 $ git checkout app/controllers/main_controller.rb
 $ git reset app/controllers/main_controller.rb

Undoing committed changes

$ git log --oneline 

Git lets you go back to any previous commit.

Find & copy the SHA of the commit you NOW regret:

 $ 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 bad commit is still there, but now you also have another commit that undoes the changes introduced by the original one.

GIT REVERT

ER, WHAT IF I ALREADY COMMITTED IT?

Git Learning Resources

Questions?

Intro to Git & GitHub

By amy_nc

Intro to Git & GitHub

  • 257