Intro to Git and Github
Click here to access recording
Learning Objectives
| Student will be able to: |
|---|
| Describe what a Version Control System is |
| Describe the difference between Git and GitHub |
| Distinguish between local and remote repositories |
What is version control, and why should you care?
A Version Control System (VCS) records changes to files over time so that you can recall specific versions later.
It also makes working in teams easier, because it enables developers to submit changes to be merged into the codebase.
More specifically, a VCS allows you to:
- Revert files back to a previous state
- Review changes made over time
- Collaborate on a set of files with others
- Create separate "branches" of the codebase to develop new features on without impacting the "master", or production, branch.
In this program, we'll be using the world's most popular version control system - git.
Git was created by Linus Torvolds in 2005 to help with the development of his main project at the time - developing Linux.
Git vs. GitHub
GitHub is not the same as git. GitHub is a social network built around git. It has completely changed the way we, as programmers, share and work on code. GitHub is now the largest online storage space of collaborative works, and it works with git in order to keep track of versions, issues, and requests for changes.
GitHub also plays the important role of a cloud-based backup system - a safe place for all your work! Your code, and the time you spent writing it, is valuable, therefore, you don't want to risk losing it to hardware failure, etc. So we "connect" our local git repo to a "remote" repo on GitHub where we can then "push" code to, and "pull" code from - on demand.
In summary:
- Git provides us with local repositories on our computers
- GitHub provides us with remote repositories stored in the cloud
- A local repository is "linked" to a remote repository by adding a "remote" with this command
$ git remote add <name of remote> <URL of repo on GitHub>
Summary of Common Git Commands
By following along today and having done the pre-work, you should now be familiar with basic git commands.
In SEIR, you'll get plenty of practice using git, especially during project week because each of your projects will be stored in its own directory and will be made a git repository in that directory tracking the changes.
For your convenience, Click Here for a Git Cheatsheet.
However for a quick reference, the following summary of commands will "git" you far:
| Command | Purpose |
|---|---|
git init |
Initializes a local repository. Used in lieu of cloning a GitHub repo. All local repos contain a hidden .git directory responsible for holding repo-related data. |
git status |
Checks and reports on the status of your repo. It will inform you what changes to tracked (staged) files will be included in next commit, if there are any untracked files that have been added to the project or have changes, etc. |
git add <path> |
Adds an entire directory or individual file (or files using a * as a wildcard) to the "staging area" for the next commit. |
git add -A |
Adds all changes within the repo to the staging are for next commit. |
git commit -m "<message>" |
Commits all staged changes to the local repo. The message should be in worded such that it describes what the commit does, not what it did. For example, "Style nav bar" instead of "Styled nav bar". |
This graph diagrams the flow of making changes to a repo:
This is the most simple workflow, things get a bit more complex when you start sharing code and manage larger codebases.
IMPORTANT: Do not create a repo within an existing repo! If you find your computer very sluggish, it might be because you have "nested" repos. It's not uncommon for students to accidentally make their home folder (
~) a repo - so start there if you suspect something is wrong.
Setup Instructions
- Create a folder in your Desktop or any other designated location on your machine; you can name it
git-github-and-terminal - Initialize a
gitrepo inside that folder with the commandgit init - Create a repository on
git.generalassemb.ly- Your Github Enterprise Account - Add your remote from
githubto your local repo with the following command:git remote add origin https://www.git.generalassemb.ly/YOURUSERNAME/git-github-and-terminal.git - Create a file called
README.mdinside yourgit-github-and-terminalfolder - Write your answers to the questions below in your
README.mdfile - Commit your work at each point when directed (remember to
git add .and thengit commit -m "your commit message") - when done run
git branch -M mainand thengit push origin main, this will send it tohttps://www.git.generalassemb.ly/YOURUSERNAME/git-github-and-terminal.git
Git & Github - Questions
Refer back to the notes from today and/or use the internet and google-fu to find the answers to the questions below:
Answer the following questions
- What command do you use to setup a git repository inside of your folder?
- What command do you use to ask git to start tracking a file?
- What command do you use to ask git to move your file from the staging area to the repository?
Terminal Practice In Breakout Rooms
Episode X: A New Terminal
A long time ago in a Unix environment far, far away, young Jedi padawans who knew only of desktop software were seduced by the dark side of the Force to enter… The Terminal.
Follow the instructions below using all the console commands introduced in Fundamentals, class, or that you find on your own.
Setup
- Open the Terminal/HYPER app
- Inside the git-github-and-terminal folder, create another folder called:
galaxy-far-far-away - Then create a file inside
galaxy-far-far-awaycalledcommands.txt - Paste the answer to each numbered question (i.e. the command(s) that accomplished the task) in
commands.txtonce you get it to work - Remember, you can learn about any Unix command by typing
manand then the command name. E.g.,man ls. TypeQto get out of the Manual page ("man page") viewer
Part I: Set the Scene
Complete all work inside the galaxy-far-far-away folder.
- Create a directory called
death_star, and make the following files inside of it:darth_vader.txt,princess_leia.txt,storm_trooper.txt - In
galaxy-far-far-away, make a directory namedtatooineand create the following files in it:luke.txt,ben_kenobi.txt - Inside of
tatooinemake a directory calledmillenium_falcon, and in it create:han_solo.txt,chewbaca.txt
Part II: mv - rename
You can rename a file using the mv command.
- Rename
ben_kenobi.txttoobi_wan.txt
Part II: cp - copy
You can copy a file from one location to another using the cp command. (man cp for more info)
- Directories can be sibling (parrell to each other) or can be parents (the folder that contains the folder you are in)
- Copy
storm_trooper.txtfromdeath_startotatooine
Part IV: mv - move
You can use the mv command to move files from one location to another. mv can be used for renaming, moving, or both. Run man mv to see the options—remember hit the Q key to get out of the manual page viewer.
- Move
luke.txtandobi_wan.txtto themillenium_falcon - Move
millenium_falconout oftatooineand intogalaxy-far-far-away - Move
millenium_falconintodeath_star - Move
princess_leia.txtinto themillenium_falcon
Part V: rm - remove
BE CAREFUL WITH rm!!! THERE IS NO "TRASH" IN THE UNIX CLI. WHEN YOU DELETE SOMETHING IT IS GONE FOREVER!!!
You can use rm to delete a file.
- Delete
obi_wan.txt.
Part VI: all together
- In
galaxy-far-far-away, make a directory calledyavin_4 - Move the
millenium_falconout of thedeath_starand intoyavin_4 - Make a directory in
yavin_4calledx_wing - Move
princess_leia.txttoyavin_4andluke.txttox_wing - Move the
millenium_falconandx_wingout ofyavin_4and intogalaxy-far-far-away - In
death_star, create directories fortie_fighter_1,tie_fighter_2andtie_fighter_3 - Move
darth_vader.txtintotie_fighter_1 - Make a copy of
storm_trooper.txtin bothtie_fighter_2andtie_fighter_3 - Move all of the
tie_fightersout of thedeath_starand intogalaxy-far-far-away
Part VII: rm -r: remove directories and everything they contain
BE CAREFUL WITH rm -r THERE IS NO TRASH CAN IN THE UNIX CLI. WHEN YOU DELETE SOMETHING IT IS GONE FOREVER
Before you hit enter, make sure are deleting the right thing, or you could accidentally delete the contents of your computer (it has happened).
This command will not typically ask you if you "really want to delete." It will just delete.
- Remove
tie_fighter_2andtie_fighter_3
Part VIII:
- Touch a file in
x_wingcalledthe_force.txt - Destroy the
death_starand anyone inside of it - Return
x_wingand themillenium_falcontoyavin_4
Celebrate. You've reached the end of this homework :)
Commit and push your updated code:
"Add" your changes (prepare them to be "committed"):
$ git add -A"Commit" your changes—any time you make a commit, you can always restore the files in the repo to that point:
$ git commit -m "Completed homework""Push" your commits to github:
$ git push origin masterConclusion
You will "git" plenty of practice as we progress through this program, so if the concept of git/github still seems a little fuzzy at this point, rest assured you will soon "git" it once you "git" some more practice in. 😎