Download Class Materials



Download Class Materials

0 0


gdi-intermediate-git-cli


On Github kpytang / gdi-intermediate-git-cli

Download Class Materials

http://bit.ly/UPDATEME

Git - Part 2

Karen Tang

Introductions

Please ask questions.

Goals for the class

  • Review basic git commands (part 1)
  • A peek under the hood - how git works its magic
  • Troubleshooting common Git scenarios
  • Basic understanding of git as a collaboration tool

Review: how you start a git project

$ git init

or

$ git clone https://github.com/SomeProject

a .git folder is created

a master branch is created

The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.

What's in a .git folder?

  • everything git needs to know to replicate your directory
  • lets git know where to backup its files

Can I change my mind about adding git to a folder?

Yes! Just delete the .git folder. You'll keep all the other files in your project folder, but remember nothing will be tracked (under version control).

How does Git keep track of my files?

3 main parts to every git project:

  • Repository (repo)
  • Staging area
  • Working Directory

What's the difference?

INSERT IMAGE

Review: Add & Commit

$ git add README.md
$ git commit -m "Your Commit Message"

What's really happening with these commands?

INSERT IMAGE & show transitions

How does my repo look like?

File "snapshots" stored as "hashes"

What is a hash?

Hashes are an important way to reference changes in your repo

Review: Branching

$ git checkout -b [NEW_BRANCH_NAME]

What's really happening with this command?

INSERT IMAGE of git log & show addition of HEAD pointer

Common Git commands

git add git commit git checkout

Back to: Add & Commit

How can I tell git to not track certain files

show .gitignore

Back to: Add & Commit

What if I accidentally added too many files to staging?

$ git reset HEAD [file]

What is this command really doing?

Back to: Add & Commit

What if I only want to commit part of a file?

$ git add --patch [file]

INSERT IMAGE

Back to: Add & Commit

I want to fix my last commit message

$ git commit --amend

Back to: Add & Commit

I forgot a file in my last commit

$ git add [file]
$ git commit -c ORIG_HEAD

Back to: Add & Commit

I want a do-over on my last commit

$ git reset --soft HEAD~1
$ [make changes to your files]
$ git add [files]
$ git commit -c ORIG_HEAD

Back to: Add & Commit

I change my mind - I don't want my last commit at all

$ git reset --hard HEAD~1

Back to: Add & Commit

My directory is a mess - I want to start over!

No problem, we can always reset git to a previous state

How far back do you want to go?

$ git reflog
$ git reset --hard
http://stackoverflow.com/questions/17857723/whats-the-difference-between-git-reflog-and-log

Review Past Commits

$ git log 

or

$ git log --oneline --decorate

INSERT IMAGE showing hash numbers, as well as HEAD and branch name

Tip: looking at the git tree

$ gitg 

or

$ gitk

Back to: Branching

Other common things you might do with branches

$ git branch -m [new_branch_name]

or

$ git branch -d [branch_name]

Back to: Branching

I accidentally deleted a branch - how do I get it back?

If you just deleted it

$ git checkout -b [branch_name] [hash]

If it's been awhile look at the log first

$ git reflog
$ git checkout -b [branch_name] [hash]

Back to: Branching

I started making changes on master, but now I want to move things to a new branch

$ git branch [new_branch]
git reset --hard HEAD~3
git checkout [new_branch]
Go back 3 commits. You *will* lose uncommitted work on master, but you'll get it on the branch

Back to: Branching

I want to add a commit from my branch to the master branch

$ git checkout master
$ git cherry-pick [branch]~3
          

Common Git commands

git add git commit git checkout (git checkout -b) git push

Review: Push & Sync

$ git push origin master

What's really happening with this commands?

origin = points to a remote site INSERT IMAGE of .git/config file

master = name of branch you want to sync

Recap so far

  • Review basic git commands (part 1)
  • A peek under the hood - how git works its magic
  • Troubleshooting common Git scenarios
  • Basic understanding of git as a collaboration tool

Recap so far

  • Review basic git commands (part 1)
  • A peek under the hood - how git works its magic
  • Troubleshooting common Git scenarios
  • Basic understanding of git as a collaboration tool

Collaboration Ex: Part 1

Team of 3: Alice, Mary, and Paula

Alice & Mary: add features, Paula: maintains repo/project

  • Alice & Mary are assigned different parts to work on
  • Alice & Mary each create their own branch to work off of
  • Alice has finished her part and is ready to commit her changes

What scenarios might Alice encounter?

Rewrite local commit history

Alice may want to:

  • Combine smaller commits in a single bigger commit
  • Remove a commit from before
$ git cherrypick

Rewrite local commit history

Alice may want to combine smaller commits in a single bigger commit

$ git log
$ git rebase -i [hash]

INSERT image with pick/squash options

Rewrite commit history

Alice may want to remove a commit from her branch

$ git log
$ git rebase -i [hash]

INSERT image with a row deleted

Collaboration Ex: Part 2

Team of 3: Alice, Mary, and Paula

Alice & Mary: add features, Paula: maintains repo/project

  • Paula merges Alice's changes into the main project

What scenarios might Paula encounter?

Stashing

Paula might have been in the middle of working on the master branch and wants to save the files she's working on before she starts working on merging Alice's changes

$ git stash
$ git stash pop
$ git stash list
$ git stash save
$ git stash drop stash@{1}
$ git stash clear
          

Merge Conflicts

$ git merge

Git tries very hard to automatically merge files but sometimes there are conflicts that it doesn't know how to resolve

Git marks each conflict, which can be manually edited

INSERT IMAGE

Merge Conflicts

Often easier to do this using a GUI

$ git mergetool

3-way merging: LOCAL, REMOTE, BASE, what do these mean?

INSERT IMAGE of tool (meld?) and show GUI example

Collaboration Ex: Part 3

Team of 3: Alice, Mary, and Paula

Alice & Mary: add features, Paula: maintains repo/project

  • Mary is done with her changes, but needs to first re-synce her branch so that works off the latest version
  • Mary is ready to commit her changes
  • Paula merges Alice's changes into the main project

What scenarios might Mary encounter?

Rebasing

Mary should routinely make sure she's working off the latest version of the project

$ git rebase master

difference between rebase and merge

what is rebasing really doing?

Rebase Conflicts

deal with it similar to merge conflicts

Be aware of rewriting history

$ git push -f

Common Git commands

git add git status git rebase git commit git stash git merge git checkout (-b) git log git reset git push git branch

Summary of common git scenarios

http://justinhileman.info/article/git-pretty/

we've covered most of these!

THE END

Thank you!

Download Class Materials http://bit.ly/UPDATEME