Top Banner
Git Basics I Credit to Git (http://git-scm.com/book)
26

Git Basics 1 Carenza

May 06, 2015

Download

Engineering

Peter Carenza

Beginning git
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Git Basics 1 Carenza

Git Basics ICredit to Git (http://git-scm.com/book)

Page 2: Git Basics 1 Carenza

Why Git?

● Most Operations are local● High integrity - use of SHA-1 hash● Most of the time, git adds data

○ Hard to mess up○ You can recover quite a bit○ All is not lost!

Page 3: Git Basics 1 Carenza

United (actually 3) States of Git

Page 4: Git Basics 1 Carenza

Git Configs

user.email user.namecore.editor merge.toolcolor.ui color.diffformat.signoff … and many, many more!See git help config

Page 5: Git Basics 1 Carenza

Initializing and Adding to Repos

● git init○ creates .git directory○ nothing is initially tracked

● git add○ Adds files/paths to a git○ can also use wildcards

● git clone○ git clone url [-b branch] [target]

Page 6: Git Basics 1 Carenza

The File Status Lifecycle

Page 7: Git Basics 1 Carenza

Checking the file status

● git status● .gitignore● git diff● Exercise: demonstrating the lifecycle, pt 1

○ Creating○ Tracking○ Staging○ Committing

Page 8: Git Basics 1 Carenza

More life cycle commands you may not be aware of!

● git diff revisited● git rm● git mv

Page 9: Git Basics 1 Carenza

Viewing past changes

● git log○ -p switch - view code diffs for each commit○ -[#] (ex. -2) - view the past # commits○ --stat - view stats for each commit○ --pretty - format log

■ oneline■ format

○ --graph○ --name-only

Page 10: Git Basics 1 Carenza

Limiting log output

● --since● --until● --author● --grep● --committer● --no-merges

Page 11: Git Basics 1 Carenza

Log --pretty=format: options%H,%h Commit hash

%T,%t Tree hash

%P,%p Parent hashes

%an Author name

%ae Author e-mail

%ad Author date (format respects the --date= option)

%ar Author date, relative

%cn Committer name

%ce Committer email

%cd Committer date

%cr Committer date, relative

%s Subject

Page 12: Git Basics 1 Carenza

Git to the rescue! Preventing derps

● git commit --amend● git reset HEAD <filename>● git checkout -- <filename>

Hint: Look at the git status command for clueson what to do next!

Page 13: Git Basics 1 Carenza

Using remote repositories - git remote● By itself - lists the remotes by short name

○ “origin” when cloned● -v option shows expanded URL● git remote add <shortname> <url-to-repo>● git remote set-url <shortname> <url>● git remote show <shortname>● git remote rm <shortname>● git remote rename <shortname>

Page 14: Git Basics 1 Carenza

The fetch/push cycle

● git fetch <shortname>○ Fetches all the branches/commits from the remote

● git pull <shortname> <branch>○ Shorthand merge (more on this later)

● git push <shortname> <branch>○ Pushes local commits to the remote

Page 15: Git Basics 1 Carenza

Cool git shortcut!

● aliases using git config○ git config --global alias.<shortname> <command>○ Ex: git config --global alias.co checkout○ git config --global alias.unstage ‘reset HEAD --’○ git config --global alias.last ‘log -1 HEAD’

Page 16: Git Basics 1 Carenza

Creating Branches

● git branch <branchname> - create branch● git checkout <branchname> - switch to

existing branch● git checkout -b <branchname> -

combination of git branch and git checkout

Page 17: Git Basics 1 Carenza

Visualizing branches (Exercise)

Page 18: Git Basics 1 Carenza

git branch iss53

Page 19: Git Basics 1 Carenza

git checkout iss53; git commit -a

Page 20: Git Basics 1 Carenza

git checkout -b hotfix; git commit -a

Page 21: Git Basics 1 Carenza

git checkout master; git merge hotfix

Page 22: Git Basics 1 Carenza

git branch -d hotfix; git checkout iss53; git commit -a

Page 23: Git Basics 1 Carenza

git merge iss53

Page 24: Git Basics 1 Carenza

Resolving merge conflicts

● Choose one section or another or groups of either/both depending on what conflicts

● Requires a little knowledge & intuition● Git status tells all!<<<<<<< HEAD<div id="footer">contact : [email protected]</div>=======<div id="footer"> please contact us at [email protected]</div>>>>>>>> iss53

Page 25: Git Basics 1 Carenza

Branch management

● git branch○ git branch -v see the last commit on each branch○ git branch --merged find out which branches have

merged○ git branch --no-merged○ git branch -d / git branch -D delete branch

Page 26: Git Basics 1 Carenza

Remote branches

● <remote>/<branch>● to synchronize git fetch <remote>● merging with remote

git merge <remote>/<branch>● git push <remote> <branch>

○ git push origin test○ git push origin test:dummy○ git push origin :dummy (removes dummy)