nina zakharenko - introduction to git - start slc 2015
TRANSCRIPT
Start slc Salt Lake City, UT
January 31st, 2015
Intro to gitPRESENTED
BY
Nina Zakharenko
AND
PART 1
Overview
WHY USE VERSION CONTROL
COMMITTING TO YOUR REPOSITORY
GIT IS NOT GITHUB
REPOSITORY CREATE YOUR FIRST GIT PROJECT
PART 2
OVERVIEW
FINAL EXERCISE FORKING (IS NOT A DIRTY WORD)
BRANCHES PULL REQUESTS SHARING YOUR WORK
?
WHY USE VERSION CONTROL
Have you ever found yourself creating multiple versions of a file to “save” your changes? !Do you make multiple copies of files to make sure you have a working backup?
VERSION PROBLEMS
AND VERSIONS ARE ALL OF THEM.
I GOT 99 PROBLEMS
Version Control lets you maintain a history of your files. !
This history is easily browsed, attached to author names, and logged with extra commit information. !
This is especially useful when working in a team environment.
VERSION CONTROL
Git is a tool. !More specifically, it’s an open source distributed version control system with an emphasis on data integrity and speed.
GIT IS NOT GITHUB
GITgithub.com is a commercial website that allows you to host git repositories !You can use it to find many open source projects, and even share your own.
GIT HUB
You might hear these terms used interchangeably. They’re not the same!
OUR FIRST repo
LINGO
REPOSITORY
Local Repository - Where code is stored on your local machine !
Remote Repository - Where code is stored on a remote server (github, or internal server) !
Git stores information about the repository in a hidden .git directory contained in the same folder as the project. !
Will be commonly referred to as a repo. !
!
LINGO
CLONE
Copying a github repository to your local machine.
Let’s configure git on our local computer. !
Use the same email address that your github account is associated with. !$ git config --global user.name "Your Name Here” !
$ git config --global user.email "[email protected]" !
SETTING UP GIT
SET UP
To set up a git repository locally, we navigate via the command line to the root of our project directory, and run the command: !$ git init
TWO WAYS TO CREATE A REPO
LOCALLY
Log in to your github account. !Next, click on the ‘plus’ icon on the right side of the toolbar, then click new repository.
ON GITHUB
You skip a few setup steps by initializing on github.
GITHUB - CREATE NEW REPO
CLONING OUR NEW REPOSITORY
GET THE URL
CLONE THE PROJECT LOCALLY
EXERCISECREATE & CLONE A GITHUB REPO
Creating a fork of my repository will copy it over to your github account.
EXERCISE
1. GO TO GITHUB.COM
Click the plus button on the right hand side, then select new repository. !Be aware, public repos are visible on the internet.
2. CREATE A NEW REPOSITORY
Copy the clone URL, then type in : !$ git clone <copied_clone_url_here>
3. COPY THE REPO URL, GIT CLONE
COMMITTINGTO YOUR REPOSITORY
LINGO
GIT ADD
$ git add <filename> !
Git does not automatically keep track of all the files in a project directory. !
Use git add to make it aware of a new file, or use git add on an existing file to let git know you’ve made changes to it that you want to keep.
LINGO
THE STAGING AREA
When you use $ git add on a file, it gets added to the staging area. !
The staging area contains snapshots of files that will be included in the next commit.
STAGING AREA
Source: http://git-scm.com/book/en/v2/Getting-Started-Git-Basics
LINGO
GIT STATUS
A git command that will give you a status update about your repository. !
This is the command I use the most in my day to day git usage. !
LET’S TRY IT
LINGO
GIT COMMIT
Record a snapshot of the files in the staging area. !$ git commit -m “A commit message contains helpful information” !
Note: When we run a $ git status after a commit, we’ll see a message that says “nothing to commit”
LINGO
GIT LOG
A way to see what changes were made, and by who. !
$ git log!
Use $ git diff <filename> to show the difference between the last checked in version of that file, and the modified file.
LINGO
DIFF
SIDENOTE
WHEN SHOULD I COMMIT MY CODE?
Don’t wait for your code to be a in a perfect state before committing it. !
You’ll get the best benefits by committing early & often.
EXERCISECOMMIT A FILE TO OUR REPO
Using the command line, navigate to the repo directory.
EXERCISE
1. NAVIGATE TO THE REPO
Open the README.md file with your editor of choice. Make and save changes.
2. MODIFY THE README
Run the command: $ git add README.md3. ADD FILE TO STAGING AREA
Run the command: $ git status to ensure your file is actually in the staging
4. RUN GIT STATUS
Run the command: $ git commit -m “my commit message”5. COMMIT YOUR CHANGES
BRANCHES
LINGO
MASTER BRANCH
The master branch is usually the “production ready” version of your codebase. !
That means that everything compiles, and all your tests pass. This is the version that customers will see - even if the customer is just you. !
There is only one master branch, and generally code is not committed directly to it.
LINGO
FEATURE BRANCHES
Branches are extremely useful when you want to work on different features without interfering with a working copy of the code. !
When you’re done working on your feature branch, you can merge it back into master. !
If your repository is stored on a remote server, multiple developers can work on the same branch.
When you create a new branch, you’re automatically switched to it. !$ git checkout -b my_branch_name
USING BRANCHES
CREATE A NEW BRANCH
$ git branch -aSEE ALL AVAILABLE BRANCHES
$ git checkout my_other_branchCHECKOUT A DIFFERENT BRANCH
MERGING BRANCHES
MERGE FEATURE BRANCH INTO MASTER
Once you’re satisfied with your feature branch, it’s time to merge it back into master. !$ git checkout master !$ git merge my_feature_branch !
EXERCISECREATE A FEATURE BRANCH
$ git checkout -b my_new_branch
EXERCISE
1. CREATE A NEW BRANCH
Review the previous exercise if you need hints.
2. COMMIT CHANGES
3. CHECKOUT MASTER
$ git checkout master
4. MERGE YOUR BRANCH
$ git merge my_new_branch
1
2
34
CREATE A BRANCH
GIT COMMIT COMMIT TO STAGING
GIT ADD ADD FILES TO STAGING AREA
WRITE CODE HACK HACK HACK
5
GIT PUSH PUSH CHANGES
UP TO REPOSITORY
GIT CHECKOUT -B
GIT WORKFLOW
QUESTIOnS?
COLLABORATINGWITH GITHUB
LINGO
FORK
Forking a repository on github will create a copy of that project in your github account.
A copy that exists in your GitHub account and can be used to submit pull requests (changes) to the maintainers of the project.
FORKING VS CLONING
FORK
A copy that does not exist in your GitHub account, and cannot change the remote repo unless the person who cloned it is added as a collaborator on the project.
CLONE
LINGO
PUSH/PULL
PUSH: Send a copy of the latest committed changes to the remote server. !
PULL: Retrieve a copy of the latest committed changes from the remote server. !
Note: If you’re working on a branch with other people, always run a pull command before getting started.
LINGO
PULL REQUEST
A pull request is a proposal made by you to a project maintainer asking them to accept your changes to their code base. !
The maintainer can review a diff of the changes you made. They can also leave general and code-line specific comments.
FINAL EXERCISEPULL REQUESTS
https://github.com/nnja/gdi-pull-requests
Creating a fork of my repository will copy it over to your github account.
FINAL EXERCISE - PT1
1. FORK MY REPOSITORY
Go to the github page of your forked copy of gdi-pull-requests. !Copy the repository URL, clone the project to your local machine.
2. CLONE YOUR FORKED REPOSITORY
https://github.com/nnja/gdi-pull-requests
git checkout -b <yourname>_branch
3. CREATE A FEATURE BRANCH
Change over to the repository directory. !Create a file called <yournamehere>.txt.
FINAL EXERCISE - PT2
4. CREATE A FILE WITH A FUN FACT ABOUT YOU
git add <yourname>.txt git commit -m “here’s a fun fact about me!”
5. ADD, THEN COMMIT YOUR FILE
https://github.com/nnja/gdi-pull-requests
git push -u origin <yourname>_branch
6. PUSH YOUR BRANCH TO THE REMOTE REPO
FINAL EXERCISE - PT3
7. CREATE A PULL REQUEST
https://github.com/nnja/gdi-pull-requests
FIN
RESOURCES!The entire Pro Git e-book. !The definitive manual. !Dense, time consuming, but chock full of information. !http://git-scm.com/book/en/v2
!Short, concise, and visual guides on beginner to advanced github topics. !https://guides.github.com/
A 15 minute interactive tutorial. Bonus: Works in your browser, no setup required. !https://try.github.io/levels/1/challenges/1
ADDITIONAL
TRY GIT PRO GIT GITHUB GUIDES
THANK YOUINTRO TO GIT
@nnja
girldevelopit.com