git basics

why git?

Text files

  • Most of the files we work with are just text.
  • Programmers have already figured out how to collaborate with text files: Version Control (e.g. git)!

git is a supplement to your workflow

workflow

Workflow

Edit files
$EDITOR
Save changes
git commit

git commit

What's your story

git history is a graph

Workflow

git init
Edit files
$EDITOR
Add changes
git add
Review status
git status
Save changes
git commit

demo

Diff(erence)

Review status with git status

View changes with git diff

Track changes easily

Find who is to blame!

git blame

branches

git branch master

git checkout -b feature

git checkout master

git merge master

branches are just labels

Go back in time!

Go back in time!

Find a commit you wanna go back to with
git log

commit c0f3b38e959fbd43888ffb0aed52289f0a0fd132 (HEAD -> gh-pages, origin/gh-pages, origin/HEAD)
Author: Eddie Schoute <censored@gmail.com>
Date:   Wed Nov 29 12:50:11 2017 -0500

	Added .gitignore

commit c0fvev3df94c763d23ec8d41b37a0e9e7e7ef39f
Author: Eddie Schoute <...@gmail.com>
Date:   Wed Nov 29 12:49:30 2017 -0500

	Added slides for newlines and diff.
then
git checkout c0fvev3
(partial matches are OK)

the remote

Collaborate with others.

Distributed Version Control

  • Everyone owns a full copy of the repository.
  • "The remote" is a special branch that tracks the state of the server.
Download changes?

git pull

Upload changes?

git push

If the remote has changed, get the changes with git pull before pushing.
git operations git pull = git fetch + git merge

Remote is special branch, but a branch nonetheless

merge conflict


Uh-oh

Merges don't always go smoothly.
If git cannot figure out what to do: Merge conflict!
Remote animals.md:

								# Favorite animal
								Horse
								Dolphin
								
Local animals.md:

								# Favorite animal
								Horse
								Chicken
								
git pull gives a merge conflict!
git status:

							On branch master
							You have unmerged paths.
  					  	  	  (fix conflicts and run "git commit")
  					  	  	  (use "git merge --abort" to abort the merge)

							Unmerged paths:
  					  	  	  (use "git add <file>..." to mark resolution)

								both modified:   animals.md

							no changes added to commit (use "git add" and/or "git commit -a")
							
animals.md:

							# Favorite Animal
							Horse
							<<<<<<< HEAD
							Dolphin
							=======
							Chicken
							>>>>>>> branch1
							
Use your editor and fix manually:

							# Favorite Animal
							Horse
							Dolphin
							Chicken
							
Then
git add animals.md
and git commit

Review of big ideas

Track changes using git diff

Branches are just labels

Remote is a branch too

Resolve conflicts

Cheat Sheet

Run "git" (or git help <command>)

Summarized cheat sheet

git <command> (or just run "git" for help):
  • init: Initialise git repo.
  • status: Check status of repo. Use often!
  • add: Add a file to staging (before committing).
  • commit: Create a commit with your changes
  • pull: Pull changes from a remote repository.
  • push: Push changes to a remote repository.
  • branch: Create a branch.
  • merge: Merge a branch.
  • checkout: Switch to a branch.

Source code for this presentation

git-repo

Forked from think-git by Dheepak Krishnamurthy