(Intro to Version Control and Related Topics)
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Software projects have lots of files and often require multiple developers making updates to the same codebase.
Leads to bugs, accidental overwrites.
"Version Control" is a system that records changes to files over time.
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.
Created by Linus Torvalds in 2005 for Linux Kernel Development
OSX:
Download Git for Mac
Windows
Download Git for Windows
Github helps you visualize your repository while providing some powerful collaboration and code management tools.
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
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
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
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 modev
- Enter visual modeesc
- Exit your current mode
Ctrl + f
- Page downCtrl + b
- Page upgg
- Go to the top of the fileG
- Go to the bottom of the file0
- (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 modeyy
- Copy current linedd
- Delete current linep
- Pasteu
- UndoCtrl + R
- Redo
k
or ↑
- Move cursor upj
or ↓
- Move cursor downl
or →
- Move cursor righth
or ←
- Move cursor left
1. Download starter kit... http://bit.ly/intro-to-git-starter-kit
2. 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.
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
ls -la .git
git status
git status -s
git add
git add .
git commit -m "initial commit."
-m
flag to pass commit message inline.
git commit -am "message"
git log
Demo:
Add new README.md
and update index.html
Make two seperate commits to illustrate staging area.
(Checkout git log -p
)
git reset <filename>
git diff
Undoing the last commit:
git reset HEAD~1
Discarding local changes:
git checkout .
Why would you use remote repos?
Each repository can have multiple "remotes", you refer to them with aliases.
Github is just another remote repository.
Create a New Github Repo
git clone
git push <remote-alias> <branch-name>
Demo:
Edit a file on Github.
Pull it's changes down.
git pull <remote-alias> <branch-name>
There are three ways to start a new project...
1. git init
2. git clone
3. Fork a repository and git clone
Demo:
1. 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>
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.
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).
You did it!