19
An Introduction to Git

SouthEast LinuxFest 2015 - intro to git

Embed Size (px)

Citation preview

Page 1: SouthEast LinuxFest 2015 -  intro to git

An Introduction to Git

Page 2: SouthEast LinuxFest 2015 -  intro to git

Disclaimer

My opinions are my own.

Page 3: SouthEast LinuxFest 2015 -  intro to git

About Me

Jason Edgecombe (@edgester)● Linux Administrator in the College of

Engineering at UNC Charlotte● OpenAFS contributor & buildbot admin● http://engrmosaic.uncc.edu● https://github.com/edgester● http://rampaginggeek.com

Page 4: SouthEast LinuxFest 2015 -  intro to git

What is git?

● Distributed version control system○ Version control system

■ Keeps snapshots of a set of folders and files■ Let’s you go back in time to any snapshot

○ Distributed■ All repositories are peers■ Each repository has full history since last sync■ Let’s you work without a network connection■ Authority is determined by choice/convention

Page 5: SouthEast LinuxFest 2015 -  intro to git

You should use git because...

● “cp” doesn’t scale (“cp file file.bak5.works”)● allows for better metadata on snapshots

o description, timestamp, author, committer● doesn’t clutter folders with copies● space-efficient, only stores differences● you don’t need a server to use it locally

Page 6: SouthEast LinuxFest 2015 -  intro to git

Common Concepts

● commit - a snapshot in timeo referenced by SHA1 hash of contentso links to all of its previous commitso immutable/non-writable!

● branch - a writable variable that refers to a commit

● tag - a read-only variable that refers to a commit, with an optional message

Page 7: SouthEast LinuxFest 2015 -  intro to git

Common Concepts, Part 2

● repository (a.k.a. “repo”) - A set of folders that contains the git commit history, optionally a working folder. Includes branches & tags

● “remote” - “git remote” - a remote git repo that is a peer of the current repo

● tracking branch - a local branch that is linked to a remote branch

Page 8: SouthEast LinuxFest 2015 -  intro to git

Concept Hierarchy

● repos contain remotes, branches, tags● remotes link to tags, branches● branches and tags refer to commits● commits refer to files, folders,

parent commits● repository→remote→branch/tag→commit

→files/folders

Page 9: SouthEast LinuxFest 2015 -  intro to git

Common local commands

● Initialization git config --global user.name "John Doe" git config --global user.email [email protected] git init

● Working with fileso Modify files - git add ; git rm ; git mv

● Snapshotso Save - git commito Query - git log; git blameo Discard changes - git reset; git checkout

Page 10: SouthEast LinuxFest 2015 -  intro to git

Common Remote Commands

● Copy a remote repoo git clone <url/address>o git checkout origin/master

● Download changeso git pull --rebase (fetch and rebase)o git pullo git fetch

● Upload changeso git push

Page 11: SouthEast LinuxFest 2015 -  intro to git

A Sample Project Workflow

1. “git init” / “git clone” # only once2. “git checkout -b origin/master3. “git pull --rebase” # before each session4. #while working (loop frequently)

a. # edit filesb. “get add .” # to add new files!c. “git commit .” # commit all modified files

5. “git push” # when work unit is done6. # goto 2

Page 12: SouthEast LinuxFest 2015 -  intro to git

Best Practices

● Jason’s guideline: use “git pull --rebase” (fetch & rebase) workflow until proficient

● Don’t store code and configuration in the same repo. (Use .gitignore files if needed)

● DO NOT STORE SECRETS in git!○ Passwords, ssh keys, etc.○ May be relaxed for encrypted secrets/isolated repo.

Page 13: SouthEast LinuxFest 2015 -  intro to git

Cage Match: Git vs. Subversion

Git: Svn:

Full history in all repos

Only latest checkout

Branching is easy

Branching is harder

Offline commits

Online-only commits

Commit ID==SHA1

Commit ID==integer

Page 14: SouthEast LinuxFest 2015 -  intro to git

Sources of Confusion

● No pre-defined source of truth for multi-repo● Many workflows available, all are valid● Deleted things (git rm) are still in the archive● History not easily re-written

o “Removed” objects/commits can still persisto History rewrites can be rejected in receiving repo

● Index (.git/) vs. working folder (./)

Page 15: SouthEast LinuxFest 2015 -  intro to git

Git Clients

● Command-line - included in Linux distros● GUI

o gitk - visualizer, included in distroso IDE’s: Eclipse, Emacs, Vimo TortoiseGit (Windows) - terminology mismatch!

‘reset’ and ‘revert’ don’t directly map to command-line git behavior.

Page 16: SouthEast LinuxFest 2015 -  intro to git

Demo Time

Page 17: SouthEast LinuxFest 2015 -  intro to git

Git Hosting

● Github - https://github.com/o Free and unlimited repos for open source projectso Can pay for closed reposo De facto standard for OSS project collaboration

● BitBucket - https://bitbucket.org/o Free for a project with up to 5 collaborators

Page 18: SouthEast LinuxFest 2015 -  intro to git

Self-Hosted Git

● Freeo Remote ssh server with a git repoo Gitolite - http://gitolite.com/o Gitlab CE

● Non-freeo Gitlab - https://about.gitlab.com/o Atlassian Stash -

https://www.atlassian.com/software/stash

Page 19: SouthEast LinuxFest 2015 -  intro to git

More Reading

● “man git-<command>”o man git-commit

● Git web siteo https://git-scm.com/

● Pro Git booko https://git-scm.com/book