GIT - Patrick's slideshow
Git for Users Training Course ⌘
What is Git?⌘
- File system
- Version control
- Free and Open Source (GPLv2)
- Designed for Distribution
- Branching & Merging
- Staging
- A really great system
What is a Git?⌘
British slang
- derived from the word get
- a foolish or worthless person
- an unpleasant or contemptible person
Why Git?⌘
"I'm an egotistical bastard, and I name all my projects after myself. First Linux, now git.“ |
|
But why Git?⌘
- Speed
- Support a distributed architecture
- Support a non-linear development process
- Support very large projects
“Take CVS as an example of what not to do; if in doubt, make the exact opposite decision” - annon
“The difference in the improvement of Git over SVN & CVS is as big as the difference between SVN over FTP.” - me
Short History of Git⌘
- A reaction to licensing changes to BitKeeper, the versioning system [at the time] of Linux
- 2002 - Linux Kernel develops in BitKeeper
- 2005 Apr 6 - BitMover drops free license and Linus starts GIT project
- 2005 Jun 16 - GIT is officially used to track Linux
- 2007 Feb 14 - GIT 1.5.0 is released (more humane)
- Those in the Linux community adopted Git quickly (some as early as summer 2005)
Installing Git⌘
Just grab the binary and go!
Use your package manager (apt, yum, emerge, etc.) to install “git”
Homebrew - install “git” MacPorts – install “git-core”
msysGit via http://msysgit.github.com
Use pkg to install “developer/versioning/git” |
Terms to know in Git⌘
|
Focus and Design⌘
Others GIT
Others | Git |
---|---|
|
|
Git Object Types⌘
Blob
- File / inode
- They’re all hashed
Tree
Commit
- A collection of SHA-1 hashes related to specific changes (trees) with context!
Tag
- A label
- Like a commit, but points to commits instead of trees
Branching and Merging ⌘
- It’s important
- It’s easy
- It’s personal!
- Super easy: http://training-course-material.com/training/Git_-_Branching_and_Merging
- Visualization of a branching structure:
- Bad braches:
Bad Branching⌘
Bad Branching⌘
Branching Utilization vs Abuse⌘
- Don’t fear branches (not branching is badness)
- Don’t merge everything
- Keep your own stuff [your own]
The Git Directory⌘
- .git
- A hidden directory within working copy
- Retains Git config data
- Holds all your datas
The Big Three Trees⌘
- Working Directory
- Index
- Head
Working Directory⌘
- Any folder you choose
- Almost like standard work
- Use Git to move/remove files/folders
- Don’t nest working folders (use submodules)
- Allow you to bring in content managed by other repos (DRY!)
The Index⌘
- The staging tree
- This is where changes go when they are “added” to Git
- Can be thought of as “on deck”
The Head⌘
- A generic term
- Highly contextual
- Always refers to your local repository
Non-SCM Uses of Git⌘
- File sharing (i.e. Dropbox clone, sparkleshare)
- Distributed database (prophet, gimd)
- Bug tracking (ditz)
- Code Deployment (CI & automating sys-ops tasks)
- Generic backup (Word, Excel, pictures, etc.)
- More to come!
Roles using Git⌘
- Git allows for flexibility
- Developers are responsible
- A cushy job for SysOps
- Distributed architecture = distributed knowledge
Git in Practice⌘
Setting Up Your Profile⌘
On the server
- Keep it accessable
- LDAP/Exchange friendly
- SSH is good
On your local machine
- Commit hooks
- Viewing Git (console integrations, etc.)
- IDE integration
- Recommended for CLI
- git config color.ui true
- git config format.pretty oneline
- git add -i
Getting a Git Repository⌘
- Github - Probably the most well known
- Bitbucket - Free private repos
- GitLab - Targeting enterprise hosts (self managed) with open sourced Community Edition
- Your servers - Just install and go (ain’t distributed architecture great?)
Normal Workflow Examples⌘
Release-Centric | Continuously Integrated |
---|---|
|
|
Log - the Commit History⌘
- git log
- Pretty it up with CLI customizations
oneline git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit git blame XYZ
- Woops
git commit –amend Don’t amend after push
Browsing Git⌘
|
Searching Git⌘
git bisect <subcommand> <options>
- Bisecting
A indispensable for larger teams and highly active repos
- git bisect help
- git bisect start [--no-checkout] [<bad> [<good>...]] [--] [<paths>...]
- git bisect bad [<rev>]
- git bisect good [<rev>...]
- git bisect skip [(<rev>|<range>)...]
- git bisect reset [<commit>]
- git bisect visualize
- git bisect replay <logfile>
- git bisect log
- git bisect run <cmd>...
Git - Extras ⌘
- git-bisect
- git stash
- Interactive mode
- git add -i
- git rebase -i
- Porcelain and Plumbing
- GitHUB
Git Diff⌘
|
Cheat/lazy
|
Branching⌘
Create a branch (options!)
- git branch <branch name>
- && git checkout <branch name>
- git checkout -b <branch name>
Delete a branch
- git branch –d <branch name>
- git branch –D <branch name>
- forces the delete – be sure!]
Simple Merging⌘
$ git checkout -b iss53 Switched to a new branch "iss53” $ vim index.html $ git commit -a -m 'added a new footer [issue 53]‘ $ git checkout master Switched to branch "master“ $ git checkout -b hotfix Switched to a new branch "hotfix“ $ vim index.html $ git commit -a -m 'fixed the broken email address‘ [hotfix]: created 3a0874c: "fixed the broken email address“ 1 files changed, 0 insertions(+), 1 deletions(-) $ git checkout master $ git merge hotfix Updating f42c576..3a0874c Fast forward README | 1 – 1 files changed, 0 insertions(+), 1 deletions(-)
Rebasing⌘
- Always rebase!
- git config --global branch.autosetuprebase always
- Force all new branches to auto-rebase
- git config --global branch.*branch-name*.rebase true
- Force all existing branches to auto-rebase
- git pull will fetch new commits from remote and merge in
- Is good right? NO!
- merge conflicts between your unpushed commits and the pulled commits
- --rebase will fetch new commits and rebase your commits on top of the newly fetched changes
- Pro tip: push push push!
Stashing⌘
- git stash [stashes current working copy]
- git stash pop [return latest stash and delete the save]
- Git stash list [list all stashes available]
- Git stash [option] -- lots of options
- Stashing takes all unstaged changes to the working copy and “stashes” them into a temp tree for you.
- Why?
- You started down a rabbit hole and aren’t sure where it’s leading you
- You’re in the middle of something that’s unresolved, but a high-priority bug requires immediate attention
- Caution
- Expensive
- Messy
Tagging⌘
git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]<tagname> [<commit> | <object>]
Is
- A meaningful naming tool
- A way to track deployments
- Concise and recognizable
Is not
- A copy of a commit
- A backup
- A way to name branches or commits
- A cool way to get your name out
Exporting Git⌘
git archive
- git archive --format=zip HEAD > project.zip
- Creates archive of the specified format containing the tree structure for the named tree and writes it out to the standard output.
git fast-export
- Dumps the given revisions in a form suitable to be piped into git fast-import. You can use it as a human-readable bundle replacement
Git Aliases⌘
From within the .gitconfig try
[alias] lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
The Care and Feeding of Git⌘
git-gc git gc [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
Runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects which may have been created from prior invocations of git add.
Users are encouraged to run this task on a regular basis within each repository to maintain good disk space utilization and good operating performance.
Some git commands may automatically run git gc; see the --auto flag below for details. If you know what you’re doing and all you want is to disable this behavior permanently without further considerations, just do:
$ git config --global gc.auto 0
Distributed Workflow Examples⌘
Distributed Workflow Examples⌘
Integration Manager
- Can be used with CI workflow
Distributed Workflow Examples⌘
Dictator and Lieutenants Workflow
Setting Up A Central Repo⌘
What does it take to manage your own Git repos?
The Install⌘
User Management⌘
- You can run everything through SSH (i.e [[1]])
- Run a local solution like GitLab ([[2]])
- Use a private cloud solution (like Github or Bitbucket)
So let's walk through something on our machines.
Bisecting⌘
I've planted a bad seed in code.sh, let's see how bisect helps us do this. Let's enter the branch and do a pull to make sure we're up to date. Now let's enter bisect mode:
git bisect start
mark the current version as bad:
git bisect bad
and mark the first instance of the file as good:
git bisect good de73b45
Now let's run the bisect with some magic
git bisect run bash code.sh
What just happened?
Submodules⌘
The Good:
- Excellent compartmentalization of code from different projects
- allows for specific state references to your submodule
- you can have multiple submodules in a single repo
The bad:
- there's a lot of [justified] resistance from using submodules (you may not get a lot of support from the Git community)
- use cases are restricted in scope (vs. remotes or subtrees)
The Ugly:
- the implementation is fragile (it's easily managed with automation tools, but tends to be headaches for teams new to Git)
- records of the submodule's version in use can get overwritten eaisly since it's not maintained in code (you have to run
git submodule update
command, and if you don't your commit can revert someone else's submodule changes. this also means when you pull someone's changes, you still have to run that command manually
Submodules (con't)⌘
Are submodules worth the trouble? What other options are there?
- use remotes with structured build processes
You don't get to manage the remotes as separate projects with independent histories, but it's conceptually a simple thing to understand and run with.
- use subtrees
A lot of people say subtress are what submodules should have been, but since they aren't, they don't have the clean and user-friendly commands. For example, to create and later update a subtree:
git subtree add --prefix .vim/bundle/tpope-vim-surround https://bitbucket.org/vim-plugins-mirror/vim-surround.git master --squash ... git subtree pull --prefix .vim/bundle/tpope-vim-surround https://bitbucket.org/vim-plugins-mirror/vim-surround.git master --squash
[the squash flags are optional to strip history from the other repo]
Subtrees (con't)⌘
So how do you make subtrees more user friendly?
- Aliases!!!
- add your subtree as a remote
git remote add -f subremote https://path.com/to/project.git
usually a combination of remote aliases and command aliases can do the trick pretty well
So what's the catch?
- all the files in the subproject are contained in the parent directory, so the clean "separation of concerns" you get from submodules isn't really present any more
- developers may forget they are in subtrees and make local changes that then have to get split back out and sent back to the source of the subproject as a separate thing (again, failure to separate concerns)