- 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 "[email protected]"
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
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
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.
git add
- Adds the specified files to the stage
(i.e. tell Git to incude this file in the next save-point). git add .
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.
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
In-class Exercise (5 minutes):
- Edit some files
- Stage your changes
- Commit your changes
Undoing the last commit:
git reset HEAD~1
Discarding local changes:
git checkout .
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.
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.