Rails Girls Presentation - Basic Intro to Git & GitHub

82
Git Basics Dionne Saunders

description

A very basic introduction to Git & GitHub. Presented at Rails Girls Canberra

Transcript of Rails Girls Presentation - Basic Intro to Git & GitHub

Page 1: Rails Girls Presentation - Basic Intro to Git & GitHub

Git BasicsDionne Saunders

Page 2: Rails Girls Presentation - Basic Intro to Git & GitHub

What Git isn’t…

Dionne Saunders

Page 3: Rails Girls Presentation - Basic Intro to Git & GitHub

What Git isn’t…

Dionne Saunders

• The insult seen at least once every three pages in a Harry Potter novel.

Page 4: Rails Girls Presentation - Basic Intro to Git & GitHub

What Git isn’t…

Dionne Saunders

• The insult seen at least once every three pages in a Harry Potter novel.

• Git is not a pre-requisite to writing code. You can accomplish everything covered in this workshop without any knowledge of Git.

Page 5: Rails Girls Presentation - Basic Intro to Git & GitHub

So… what is Git?

Dionne Saunders

Page 6: Rails Girls Presentation - Basic Intro to Git & GitHub

So… what is Git?

Dionne Saunders

• Backup

Page 7: Rails Girls Presentation - Basic Intro to Git & GitHub

So… what is Git?

Dionne Saunders

• Backup

• Tracked Changes

Page 8: Rails Girls Presentation - Basic Intro to Git & GitHub

So… what is Git?

Dionne Saunders

• Backup

• Tracked Changes

• Peace of mind

Page 9: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

Page 10: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

• Branches provide a means of isolating new code which is currently in development or testing without affecting other branches, or the current working state of your application.

Page 11: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

• Branches provide a means of isolating new code which is currently in development or testing without affecting other branches, or the current working state of your application.

This is useful for both individual, and collaborative development.

Page 12: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

• Here’s an example of the benefit of branching, even when you’re coding solo:

Page 13: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

• Here’s an example of the benefit of branching, even when you’re coding solo:

Imagine for a moment that you are developing an app called Railsbook. You’re working on a new feature called ‘relationship statuses’. Part way through, someone reports a bug. Your users are no longer able to change the privacy settings of their profiles!

Page 14: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

• Here’s an example of the benefit of branching, even when you’re coding solo:

Imagine for a moment that you are developing an app called Railsbook. You’re working on a new feature called ‘relationship statuses’. Part way through, someone reports a bug. Your users are no longer able to change the privacy settings of their profiles!

You need to fix this fast, but you spent hours working on implementing ‘relationship statuses’. You don’t want to lose the work you’ve done for that, but it certainly isn’t ready to be deployed onto the server.

Page 15: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

Page 16: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

You go back to the ‘master’ branch, create yet another branch called ‘privacy-fix’, and write some code to fix that bug.

Page 17: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

You go back to the ‘master’ branch, create yet another branch called ‘privacy-fix’, and write some code to fix that bug. When you’re done, you merge the ‘privacy-fix’ branch into the ‘master’ branch and deploy your code.

Page 18: Rails Girls Presentation - Basic Intro to Git & GitHub

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

You go back to the ‘master’ branch, create yet another branch called ‘privacy-fix’, and write some code to fix that bug. When you’re done, you merge the ‘privacy-fix’ branch into the ‘master’ branch and deploy your code.

Now, you can switch back to the ‘relationships’ branch and continue to code this new feature.

Page 19: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 20: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 21: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 22: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 23: Rails Girls Presentation - Basic Intro to Git & GitHub

Create an account

Dionne Saunders

Page 24: Rails Girls Presentation - Basic Intro to Git & GitHub

Create an account

Dionne Saunders

• Go to http://github.com and create an account

Page 25: Rails Girls Presentation - Basic Intro to Git & GitHub

Create an account

Dionne Saunders

• Go to http://github.com and create an account

• Accounts are free, unless you want private repositories

Page 26: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 27: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 28: Rails Girls Presentation - Basic Intro to Git & GitHub
Page 29: Rails Girls Presentation - Basic Intro to Git & GitHub

Cloning your repo

Dionne Saunders

Page 30: Rails Girls Presentation - Basic Intro to Git & GitHub

Cloning your repo

Dionne Saunders

• Open a terminal (or “command prompt” for you windows peeps)

Page 31: Rails Girls Presentation - Basic Intro to Git & GitHub

Cloning your repo

Dionne Saunders

• Open a terminal (or “command prompt” for you windows peeps)

• cd /somewhere

Page 32: Rails Girls Presentation - Basic Intro to Git & GitHub

Cloning your repo

Dionne Saunders

• Open a terminal (or “command prompt” for you windows peeps)

• cd /somewhere

• git clone [email protected]:lenoretres/fakeapp.git #replace with correct URL

Page 33: Rails Girls Presentation - Basic Intro to Git & GitHub

This is what the repository looks like after creation via GitHub, then cloning to your computer.

Page 34: Rails Girls Presentation - Basic Intro to Git & GitHub

Basic Workflow

Dionne Saunders

Page 35: Rails Girls Presentation - Basic Intro to Git & GitHub

Basic Workflow

Dionne Saunders

• It’s a matter of personal preference. I’ll show you a simple strategy I use to help you get started.

Page 36: Rails Girls Presentation - Basic Intro to Git & GitHub

Branch off of ‘master’

Dionne Saunders

Page 37: Rails Girls Presentation - Basic Intro to Git & GitHub

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

Page 38: Rails Girls Presentation - Basic Intro to Git & GitHub

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

Page 39: Rails Girls Presentation - Basic Intro to Git & GitHub

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

• To create a new branch:

Page 40: Rails Girls Presentation - Basic Intro to Git & GitHub

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

• To create a new branch:

• git checkout –b feature

Page 41: Rails Girls Presentation - Basic Intro to Git & GitHub

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

• To create a new branch:

• git checkout –b readme

• In this command, we are essentially saying: git go to a new branch called readme

Page 42: Rails Girls Presentation - Basic Intro to Git & GitHub

Now that we’ve branched off, we see our new local branch called ‘readme’.

Notice that there isn’t a corresponding ‘origin’ branch. We have not yet pushed the branch to the remote repository.

Page 43: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin

Dionne Saunders

Page 44: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin

Dionne Saunders

• To push your branch to the remote repository:

Page 45: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

Page 46: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

• In this command, we are essentially saying: git upload my changes to a remote branch called readme

Page 47: Rails Girls Presentation - Basic Intro to Git & GitHub

Now that we’ve pushed to the remote repository, we see that there is a branch called origin/readme

Page 48: Rails Girls Presentation - Basic Intro to Git & GitHub

Write some code, commit, and push

Dionne Saunders

Page 49: Rails Girls Presentation - Basic Intro to Git & GitHub

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

Page 50: Rails Girls Presentation - Basic Intro to Git & GitHub

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

• First, I need to ‘commit’ the files locally:

Page 51: Rails Girls Presentation - Basic Intro to Git & GitHub

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

• First, I need to ‘commit’ the files locally:

• git commit –a –m ‘Modified readme to demonstrate a commit’

Page 52: Rails Girls Presentation - Basic Intro to Git & GitHub

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

• First, I need to ‘commit’ the files locally:

• git commit –a –m ‘Modified readme to demonstrate a commit’

• In this command, we are essentially saying: git commit all changed files with a message saying ‘Modified readme to demonstrate a commit’

Page 53: Rails Girls Presentation - Basic Intro to Git & GitHub

Yay! We can now see that the changes have been commited to our local readme branch.

Page 54: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin… again

Dionne Saunders

Page 55: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin… again

Dionne Saunders

• To push your branch to the remote repository:

Page 56: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin… again

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

Page 57: Rails Girls Presentation - Basic Intro to Git & GitHub

Pushing to origin… again

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

• In this command, we are essentially saying: git upload my changes to a remote branch called readme

Page 58: Rails Girls Presentation - Basic Intro to Git & GitHub

Our changes are now on origin/readme

Page 59: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

Page 60: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

Page 61: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

Page 62: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

• git pull origin master• git grab changes from the remote branch called master

Page 63: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

• git pull origin master• git grab changes from the remote branch called master

• git checkout readme• git go to the branch called readme

Page 64: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

• git pull origin master• git grab changes from the remote branch called master

• git checkout readme• git go to the branch called readme

• git rebase master• git make sure this branch is on top of the branch called master

Page 65: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

Page 66: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

Page 67: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

• We’re ready to merge the branch into master

Page 68: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

• We’re ready to merge the branch into master

• git checkout master• git go to the branch called master

Page 69: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

• We’re ready to merge the branch into master

• git checkout master• git go to the branch called master

• git merge readme• git merge the changes from readme into the current branch (master)

Page 70: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

Page 71: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• Master now has all the code we wrote in the readme branch. Let’s delete both the local and remote readme branches:

Page 72: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• Master now has all the code we wrote in the readme branch. Let’s delete both the local and remote readme branches:

• git –d readme• git delete the branch called readme

Page 73: Rails Girls Presentation - Basic Intro to Git & GitHub

Merging back to master

Dionne Saunders

• Master now has all the code we wrote in the readme branch. Let’s delete both the local and remote readme branches:

• git –d readme• git delete the branch called readme

• git push origin :readme• This command is non-intuitive and weird. What it’s doing is deleting the

remote branch. The colon is necessary. Don’t ask me why they did it this way =)

Page 74: Rails Girls Presentation - Basic Intro to Git & GitHub

We only have a master branch now, but we still have all the changes that were made on the readme branch.

Page 75: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

Page 76: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

Page 77: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

Page 78: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

Page 79: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

• http://gitweekly.com/gitcasts is a git tutorial site.

Page 80: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

• http://gitweekly.com/gitcasts is a git tutorial site.

• Go to GitHub, find an open source project, and make a code contribution.

Page 81: Rails Girls Presentation - Basic Intro to Git & GitHub

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

• http://gitweekly.com/gitcasts is a git tutorial site.

• Go to GitHub, find an open source project, and make a code contribution.

• Open source contributions are a great way of giving back to the community, and showing off your code kung-fu to potential employers. Most of the gems you will end up using are open source projects.

Page 82: Rails Girls Presentation - Basic Intro to Git & GitHub

The End

Dionne Saunders

Visit http://ihacked.it to download this presentation.