This document provides an introduction to using version control with Git. It covers why version control is useful, setting up Git locally and on GitHub, creating repositories, committing changes, branching, merging, forking repositories, and making pull requests. The document includes exercises for readers to practice these Git concepts. It aims to explain the key terminology and workflows for getting started with Git.
6. 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
7. 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
8. 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
GIT
github.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!
10. 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.
!
!
12. 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 "your_email@youremail.com"
!
SETTING UP GIT
SET UP
13. 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.
17. 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
19. 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.
20. 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.
22. 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.
!
24. 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”
25. LINGO
GIT LOG
A way to see what changes were made, and by who.
!
$ git log
!
26. Use $ git diff <filename> to show the difference between
the last checked in version of that file, and the modified file.
LINGO
DIFF
27. 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.
29. 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.md
3. 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
31. 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.
32. 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.
34. 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 -a
SEE ALL AVAILABLE BRANCHES
$ git checkout my_other_branch
CHECKOUT A DIFFERENT BRANCH
35. 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
!
42. 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
43. 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.
44. 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.
46. 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
47. 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
48. FINAL EXERCISE - PT3
7. CREATE A PULL REQUEST
https://github.com/nnja/gdi-pull-requests
50. 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