Ben Emmons presented on using Git and BitBucket for version control. He discussed configuring Git locally, establishing a workflow with remote repositories and branches, troubleshooting when issues arise, using SSH keys with BitBucket, and additional resources. The goal is a 3-tier version control system with 10 or fewer daily commands to manage changes across development, test, and production environments via pull requests on BitBucket.
2. Prerequisites
10/22/2013
• Basic understanding of version control and Git
(see Mike Hagedon’s presentation for a refresher)
• Acknowledge that Git is better than the competition
(let’s not waste time on flame wars)
• command line is your friend
Source:
Google Trends
3. Agenda
10/22/2013
• Local
• Git config – go beyond user.name and user.email
• Start with goal in mind – desired end-state
• Workflow – remote repositories, branches, submodules
• When things go wrong…
• Remote
• SSH
• BitBucket
• Pro Tips & Additional resources
15. Git config – alias
10/22/2013
• “svn status -u” equivalent?
16. Git config – alias (git check)
10/22/2013
• git config --global alias.check '!f() { local branch=`git rev-parse
--abbrev-ref HEAD`; git fetch origin --quiet && git diff --name-
status $branch origin/$branch $1; }; f'
17. Git config – alias
10/22/2013
• Other aliases (explained later)
• git config --global alias.changed 'diff-tree --no-commit-id --name-only
-r'
• git config --global alias.release '!git push; git tag -a "`date
+%Y%m%d`-`whoami`-`date +%s`" -m `date +%s`; git push --tags;'
• git config --global alias.hotfix '!git tag -a "`date +%Y%m%d`-`whoami`-
`date +%s`-hotfix" -m `date +%s`'
• git config --global alias.bleach '!git reset --hard -q; git clean -d -f
-n; read -p "Apply changes? (Y/n) " -n 1 -r; echo; if [[ $REPLY =~
^[Yy]$ ]]; then git clean -d -f; fi'
• Optional (ignores executable bit differences)
• git config --global core.filemode false
# git status
old mode 100755
new mode 100644
18. Agenda
10/22/2013
• Local
Git config – go beyond user.name and user.email
• Start with goal in mind – desired end-state
• Workflow – remote repositories, branches, submodules
• When things go wrong…
• Remote
• SSH
• BitBucket
• Pro Tips & Additional resources
19. Goal
10/22/2013
3-tier version control with 10 or less daily commands:
• git status checks local repo to see if there are any pending changes to
add/push
• git check checks local and remote repo to see if there are any pending changes
• git pull applies pending changes to local repo
• git info <file> brief report on status of file
• git add <file> (or git add .) adds a file (or many files) to git
• git rm <file> deletes a version controlled file (normal rm works as well)
• git commit <file> -m 'My descriptive commit message' (or git commit -am 'My
descriptive commit message') commits changes to local repo
• git push pushes changes to remote repo branch
• git release pushes changes to remote repo branch and tags it as a release
• https://bitbucket.org/<account>/<site>/pull-requests review and pull changes
from one environment to another (e.g. test > master)
20. Agenda
10/22/2013
• Local
Git config – go beyond user.name and user.email
Start with goal in mind – desired end-state
• Workflow – remote repositories, branches, submodules
• When things go wrong…
• Remote
• SSH
• BitBucket
• Pro Tips & Additional resources
23. Workflow (continued)
10/22/2013
• .gitignore file: https://gist.github.com/anonymous/8610387
(note: the sites/* line includes a subtle hack to avoid tracking .gitmodules files in the Drupal
core repository; this will cause two side effects: a false error will be thrown upon submodule
creation and you need to run git commands in both core and site folders)
• Allow “git pull”: git checkout –t origin/master
• In DEV and TEST, delete master branch: git branch –d master
• Allow “git push”: git push –u origin master
• Use BitBucket pull requests to move code between branches
(more on this in a second…)
24. Agenda
10/22/2013
• Local
Git config – go beyond user.name and user.email
Start with goal in mind – desired end-state
Workflow – remote repositories, branches, submodules
• When things go wrong…
• Remote
• SSH
• BitBucket
• Pro Tips & Additional resources
26. When things go wrong…
10/22/2013
Analyze
1. git l see detailed info regarding recent commits
2. git changed <commit hash> list files changed in specific commit
3. git d <path/to/filename> see local modifications to a single file
4. git d <path/to/folder/> (or simply 'git d’ for current folder) see local
modifications to all files within a folder (recursive)
5. git d <tagname> HEAD --stat shows changed files between tagged release
and current local version
6. git d <tagname> HEAD <path/to/changed/filename> shows change in
specific file between tagged release and current local file
27. When things go wrong…
10/22/2013
Undo
7. git r <path/to/filename> revert local uncommitted modifications to a single file
8. git r . revert all local uncommitted modifications within a folder (recursive)
9. git reset <path/to/filename> reverts 'git add' of a single file
10. git reset . reverts all 'git add' commands within a folder (recursive)
11. git reset --hard reverts all file modifications and 'git add' commands
12. git clean -d -f -n . dry-run test to see what untracked files would be deleted
within a folder (recursive)
13. git clean -d -f . delete untracked files within a folder (recursive)
14. git bleach reverts all file modifications and new/unignored files (does NOT
delete gitignored files/folders; use with caution)
Pro Tip: when in doubt, git stash
28. When things go wrong…
10/22/2013
Rollback
15. git commit --amend -m "New commit message" change most recent
commit message
16. git reset HEAD~1 <or commit hash> use to uncommit most recent commit
but keep modifications (ONLY use if commit is NOT pushed)
17. git reset --hard HEAD~1 <or commit hash> use to fully rollback most recent
commit (ONLY use if commit is NOT pushed)
18. git revert HEAD <or commit hash> use to fully rollback most recent pushed
commit (type ":wq" to accept the default commit message, then type "git push" to
submit the rollback)
29. Agenda
10/22/2013
• Local
Git config – go beyond user.name and user.email
Start with goal in mind – desired end-state
Workflow – remote repositories, branches, submodules
When things go wrong…
• Remote
• SSH
• BitBucket
• Pro Tips & Additional resources
30. SSH
10/22/2013
• Use Secure Shell (SSH) keys for communicating between servers:
ssh-keygen –t rsa –b 2048 –C your@email.com
• ~/.ssh/id_rsa file is your private key (protect like you would a password)
• ~/.ssh/id_rsa.pub file is your public key (share with anyone, including
BitBucket)
31. Agenda
10/22/2013
• Local
Git config – go beyond user.name and user.email
Start with goal in mind – desired end-state
Workflow – remote repositories, branches, submodules
When things go wrong…
• Remote
SSH
• BitBucket
• Pro Tips & Additional resources
32. BitBucket
10/22/2013
• Why not GitHub ? BitBucket provides free, unlimited public and private
repositories for higher education
• Supports SSH keys: /account/user/<usename>/ssh-keys
• Supports Personal and Team accounts (with role-based groups and
authorization rules)
• Supports Pull Requests to pull changes from another repository or
branch
• Clean, intuitive interface with similar functionality to GitHub (wiki, issue
tracking, dashboard, etc.)
• Issue tracking integration with commit messages: http://goo.gl/HoC75k
• Integration options with other Atlassian products such as JIRA and
Confluence
33. BitBucket (continued)
10/22/2013
• On /account/user/<team account>/groups
o Set up Administrators, Developers, and Readers groups (Pro Tip: add Team
account username to Administrators group by typing full name since it may not appear
in auto-search)
• On /<account>/<repo>/admin/access (Pro Tip: gear icon in top-right is easy to miss)
o Add users and apply appropriate permission (usually “write”)
• On /<account>/<repo>/admin/branches
o Limit pushes for master to “<account>:administrators“ group
o Optionally limit pushes for test and dev
o Prevent deletion of master, test, dev branches (Pro Tip: due to BitBucket bug
when clicking on textbox initially you may get dropdown, but you must type name not
select from dropdown)
o Optionally prevent history re-writes (rebase)
35. Agenda
10/22/2013
• Local
Git config – go beyond user.name and user.email
Start with goal in mind – desired end-state
Workflow – remote repositories, branches, submodules
When things go wrong…
• Remote
SSH
BitBucket
• Pro Tips & Additional resources
36. Pro Tips
10/22/2013
• After a site is “live”, code only “moves upstream” (dev → test
→ master), except for TEST bug fixes and PROD hotfixes
• After a site is “live”, the database (content) and files only
“move downstream” (dev ← test ← master)
• Beware any git command including --force
DEV
PROD
37. Additional Resources
10/22/2013
• “Pro Git” eBook (free): http://git-scm.com/
• Other Git models:
• http://svn.haxx.se/users/archive-2007-10/att-
0101/SCMBranchingModels.pdf
• http://nvie.com/posts/a-successful-git-branching-model/
• Other Git workflows: https://www.atlassian.com/git/workflows
• http://gitignore.io/
• UITS Web & Mobile Services Git documentation (* restricted
access, complex, and customized to our environment so email me offline if you
want a copy):
http://confluence.arizona.edu/display/uitswt/GIT+and+BitBucket+documentation