(Intro to Version Control and Related Topics)
https://github.com/thecodeferret/intro-to-git
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
- We are here for you— ask questions!
- Every question is important.
- Help each other.
- Have fun.
##Tell us about yourself
- Who are you?
- What do you hope to get out of the class?
- What is your favorite animal?
##Join Us On Slack http://slack.gdicincy.com
- What is version control?
- Using the command line
- Git in practice
Software projects have lots of files and often require multiple developers making updates to the same codebase.
Leads to bugs, accidental overwrites.
- Who changed what? when?
- How to merge your code and mine?
- Collaborating remotely is hard.
"Version Control" is a system that records changes to files over time.
- Track every change to every file.
- Manage multiple versions of file(s).
- Who added/edited/deleted what?
- Attach descriptive messages to each change.
Over the years, different strategies for version control have evolved.
Two types of version control systems:
Centralized and Distributed
Assumes a single "central" copy of your project somewhere (probably on a server), and programmers will "commit" their changes to this central copy.
Examples include Subversion, CVS and SourceSafe
Doesn't rely on a central server. Every developer "clones" a copy of a repository and has the full history of the project on their own hard drive.
Examples include Bazaar, Mercurial and Git
...designed to manage everything for small or large projects with speed & efficiency.
- Easy to learn
- Provides multiple development workflows
- Open source
Created by Linus Torvalds in 2005 for Linux Kernel Development
- Very fast and efficient
- Cheap local branching
- Works great remotely
- Popularized by Github
-
OSX:
Download Git for Mac -
Windows
Download Git for Windows
There are also some GUIs for Git...
Github helps you visualize your repository while providing some powerful collaboration and code management tools.
- Forks & Pull Requests
- Issues
- Wikis & Github Pages
- Organizations
- Releases & Tags
- ...and more
A Unix "shell" is a command-line interpreter that provides a traditional user interface for the Unix operating system and for Unix-like systems.
- There are a bunch of different shells... Bourne shell, C shell, Korn shell, Z shell, etc.
- The first major shell was the Bourne Shell, developed in the late 70's.
- We're going to focus on the "Bourne again shell" or Bash (default shell for OSX and Linux)
Hint: You can install Bash bindings in Windows via Git for Windows
- A fully-capable scripting language
- Vast adoption (approximately 70% of public servers are Unix or Unix-like)
- Tons of command line features...
- Tab completion
- Pipes
- Aliases
- Environment variables
- Startup scripts
- ...and more!
In this next part, we'll learn some basic commands that we can use to interact with our Bash shell.
The outline below is provided as a handy reference of the commands we'll cover in class. This tutorial contains alot of additional information if you're confused or just curious.
pwd
- Display your "present working directory".
ls
- Display the contents of a directory specified by <path>
.
Optional Flags:
Long listing (with details)... ls -l
List all files... ls -a
Optional Arguments:
Apply to files or directories... ls <path>
Hint:
You can use the wildcard character too... ls *.txt
man
- Display documentation for a given command. "Man" is short for "manual".
Required Arguments:
The command to display documentation for... man <command>
Hints:
Exit a man page by pressing the 'q' key on your keyboard.
Windows
help pwd
cd
- Change to directory specified by <path>
Optional Arguments:
The location to move to... cd <path>
Special Characters:
Move to the parent directory... cd ..
Return to previous working directory... cd -
Root of filesystem... cd /
Your home directory... cd ~
or cd --
open
- View directory or file specified by <path>
.
Required Arguments:
Directory or file... open <path>
Special Characters:
A dot character refers to the current directory... open .
Note For Windows Users...
Use this command instead... explorer .
mkdir
- Make a new directory.
Required Arguments:
The name of the new directory... mkdir <name>
Optional Flags:
Create intermediate directories as required. ... mkdir -p <path>
wget
- Download a file.
Required Arguments:
File url... wget <url>
Hint:
Try executing this...
wget http://www.gutenberg.org/files/2600/2600-0.txt
Note for Mac Users:
If you don't have
wget
try this... curl -O http://www.gutenberg.org/files/2600/2600-0.txt
cat
- Concatenate and print files
Required Arguments:
The file to concatenate... cat <file>
Hint:
Path multiple paths to concatenate files... cat <file> <file>
Special Characters:
Send output to a new file... cat file1.txt file2.txt > combined.txt
Wildcards... cat *.txt > all-the-files.txt
cat file1.txt file2.txt > combined.txt
cat *.txt > all-the-files.txt
head
- Show the first ten lines of a file.
Required Arguments:
The file to display... head <file>
Optional Flags:
Specify n
lines... head -n 25 [file]
tail
- Show the last ten lines of a file.
Required Arguments:
The file to display... tail <file>
Optional Flags:
Specify n
lines... tail -n 25 [file]
mv
- Move (or rename) a file or directory
Required Arguments:
The target and destination... mv <target> <destination>
cp
- Copy a file or directory
Required Arguments:
The target and destination... cp <target> <destination>
Optional flags:
Copy recursively (directories)... cp -R
rm
- Remove files and directories
Required Arguments:
The file or directory to remove... rm <path>
Optional Flags:
Remove recursively (directories)... rm -r
history
- Show command history
Special Characters:
Recall previous command... !!
Repeat command in your history... !<linenumber>
vim
- Create and edit text files.
Optional Arguments:
The directory or file(s) you want edit... vim <path>
Vim (which stands for Vi-improved) is a heavy-duty text-editor alot of developers use full-time.
It is, admittedly, confusing as hell for new users because it relies solely on keyboard shortcuts to navigate the interface. But it's installed almost everywhere (and is generally the default editor on most Unix-esque systems) so its worth being familiar with.
The trickiest part about using vim
is understanding that there are multiple "modes". The current mode you're in determines what features are available. For example– if you want to edit an open file, you'll need to enter "insert mode" by pressing the a
key or i
key on your keyboard.
You can tell that you're in "insert mode" if you see -- INSERT --
displayed in the bottom left corner of your editor window. Once you're in "insert mode" you can type normally and move the cursor around the window with your arrow keys.
After you make your edits, you'll probably want to save them right? To do this, you first need to exit "insert mode" by pressing the esc
key on your keyboard.
Next you issue a couple keystrokes in order to tell Vim you want to save your changes. Type the following characters and hit enter to "write" your changes to disk... :w
(that is a colon followed by the letter w).
Closing your open file is alot like saving your edits above. Type the following characters and hit enter to "quit" vim... :q
(colon then the letter q).
You can also combine these two actions into one like so... :wq
You can create a new file or open an existing file the same way. Just pass a filename as the first argument to the vim
command...
$ vim some-filename
Here's list of common keyboard shortcuts, the vast majority of which are not applicable .
vimtutor
- Get a vim tutorial
i
or a
- Enter insert mode
v
- Enter visual mode
esc
- Exit your current mode
Ctrl + f
- Page down
Ctrl + b
- Page up
gg
- Go to the top of the file
G
- Go to the bottom of the file
0
- (zero) Go to beginning of line
$
- Go to end of line
:w
- Write changes to a file
:q
- Quit vim
:wq
- Write and quit
cw
- Change word and enter insert mode
yy
- Copy current line
dd
- Delete current line
p
- Paste
u
- Undo
Ctrl + R
- Redo
k
or ↑
- Move cursor up
j
or ↓
- Move cursor down
l
or →
- Move cursor right
h
or ←
- Move cursor left
- Download starter kit... http://bit.ly/intro-to-git-starter-kit
- Open a new Bash window and
cd
in to the starter kit's directory
First, let's personalize Git by setting some configuration values...
`git config --global user.name "Your Name"`
`git config --global user.email "your_email@whatever.com"`
Git will track the contents of every file within a directory.
- What changed?
- When did it change?
- Who changed it?
As you're working on the files, you can create "save-points" (aka "revisions"; aka "commmits").
If you ever make a mistake, you can recover a previous state of a file.
git init
- Initializes a new "repository"
- Tells git to keep track of changes in your current directory
- Try:
ls -la .git
git status
- Shows what has changed since our last commit.
- Use this command constantly.
- Top of your “how do I figure out what happened” tools
- Try:
git status -s
##Creating a save-point is a two-step process...
- You "stage" your changes before "committing" your changes.
- Staged files will be included in the next save-point.
- Helps with organizing your files.
##Step 1:
git add
- Adds the specified files to the stage
(i.e. tell Git to incude this file in the next save-point). git add .
##Step 2:
git commit -m "initial commit."
- Create a new save-point by commiting our changes to the repository.
- Use the
-m
flag to pass commit message inline. - Hint: you can combine these steps as
git commit -am "message"
git log
- See all the save-points over the lifetime of this repository.
- It's your safety net– you can always get back to anything.
Demo:
- Add new
README.md
and updateindex.html
- Make two seperate commits to illustrate staging area.
(Checkout git log -p
)
git reset <filename>
- Remove a file from the stage.
git diff
- Displays changes since the last save-point
- Edit some files
- Stage your changes
- Commit your changes
Undoing the last commit:
git reset HEAD~1
Discarding local changes:
git checkout .
A little more about this command
Why would you use remote repos?
- Collaboration
- Backups
- Deployment
- Automated Services
Each repository can have multiple "remotes", you refer to them with aliases.
Github is just another remote repository.
##In-Class Exercise (5-minutes):
Create a New Github Repo
- Login to Github.
- Create a new public repository.
- Follow on-screen instructions to add remote to your local repository.
- Push changes to Github.
- Checkout how you can clone.
git clone
- Creates a "clone" of a remote repository.
- Copies the entire commit history to your computer.
git push <remote-alias> <branch-name>
- Push code on specific branch to a remote repository.
- Copies your entire commit history to the remote.
Demo:
- Edit a file on Github.
- Pull it's changes down.
git pull <remote-alias> <branch-name>
- Copies updates from a remote repository to your computer.
- Git will try to automatically merge your code.
##Did You Know?
There are three ways to start a new project...
git init
git clone
- Fork a repository and
git clone
- Git automatically calls the main branch “master”.
Demo:
- Draw a linear-picture of what branches look like.
Create a new branch:
git branch <branch-name>
See all the branches:
git branch
Checkout a specific branch:
git checkout <branch-name>
Delete a branch:
git branch -D <branch-name>
git merge <branch-name>
- Merging combines the histories of two branches.
- Git will try to automatically merge your code.
###Merge Conflicts
Conflicts happen when Git doesn't know how to merge your code.
They look like this...
<<<<<<< HEAD
This was once a test, but is no longer.
=======
This is a test.
>>>>>>> some-branch-name
When you encounter a conflict, you need to edit the file by hand.
##In-Class Exercise:
- Create a merge conflict by editing the same file on two branches.
- Merge the branches.
- Resolve the conflict.
These are Github specific terms (though other platforms have adopted them).
- A "fork" is clone of a repository to your Github account.
- A "pull-request" is a formal request to merge forked code back into the original repo.
##In-Class Exercise:
- Create a new fork of... girldevelopitcincinnati/memehub
- Clone the fork and add new animated gif the respository.
- Submit a pull-request.
###Additional Resources
- Code Academy: Command Line
- Vim Adventures
- Conceptual Git Cartoon
- Version Control
- Code Academy: Learn Git
- Free Code Camp: Git
- Git Branching Tutorial
- Github Flow
- Interactive Git Cheat Sheet
- Official Git Hub Git Cheat Sheet
- Saving guide for fixing common git fails
Huge Thank you to Sara Powell and Sarah Sachs for letting me shamelessly steal your great advice 😘