Transformative Git Practices
-
Upload
nicola-paolucci -
Category
Software
-
view
602 -
download
4
Transcript of Transformative Git Practices
![Page 1: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/1.jpg)
@durdn#DevoxxPL
Platinum Sponsors:
Transformative Git Practices
Nicola Paolucci
New Techniques for Hyper-productivity
![Page 2: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/2.jpg)
git push --force
![Page 3: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/3.jpg)
Today we’ll cover some powerful Git goodies
Worktree clones Painless sub-projectsThe magic of --fixup
For when you have to work concurrently on multiple long running branches
You can use git subtree to handle external libraries in a clean and efficient way
A powerful technique to cleanup your history before sharing with it the team
and a final, cool topic…
![Page 4: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/4.jpg)
@durdn
Git WorktreeLocal clones for parallel work
![Page 5: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/5.jpg)
The old simple way: branch, commit, push
master
HEAD
HEAD
new-feature
git checkout -b new-feature
Uns
![Page 6: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/6.jpg)
@durdn
It’s clumsy to work in parallel on multiple branches
![Page 7: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/7.jpg)
Or are a pro at using the Stash (which is
cool btw)
You either work around it having
intermediate WIP commits
It leaves hanging uncommitted work
lying around
To work on 2+ branches at the same time
![Page 8: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/8.jpg)
@durdn
git stash is awesomeIt’s a way to temporarily store your unsaved work
on a stack of patches
stash@{0}
stash@{1}
stash@{2}
stash@{3}
git stash save
![Page 9: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/9.jpg)
@durdn
But there’s a new way: git worktreeAvailable since Git 2.5
![Page 10: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/10.jpg)
git worktree creates additional clones inside your project
Add a new worktree
git worktree add \ -b feature/PRJ-1-new-fancy-button \ fancy-button
Branch to checkout in clone
Folder where to checkout
![Page 11: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/11.jpg)
It shows the folder
git worktree list - shows the worktrees created
And the branch is pointing atWhere the additional cloned has been checked out
So that you have an immediate overview of all the trees you have created
git worktree list$
/Users/np/p/dac f1f7f8b [master]
/Users/np/p/dac/git-the-all-new-modern-workflow 7fe9096 [blog/git-…-new-modern-workflow]
/Users/np/p/dac/provision-a-cluster-with-rancher b4b84fb [blog/provision-…-with-rancher]
/Users/np/p/dac/static-site-with-pipelines-and-middleman d6bd9fd [blog/static-site-…-middleman]
![Page 12: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/12.jpg)
git worktree prune - once you’re done with a clone
git worktree prune$ rm -rf fancy-button$
![Page 13: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/13.jpg)
Demo Time! Let’s see how “git worktree” works in practice
![Page 14: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/14.jpg)
Replace the normal branching?Keep working while testingBetter work in parallel
Why git worktree is a great choice
Having multiple clones with all the branches you’re working on available in your repo is a big win.
If you have a big battery of tests which are slow to run, you can run them in a worktree clone while you keep working.
When you get used to worktrees, they become very natural for any feature branch work.
![Page 15: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/15.jpg)
@durdn
The magic of --fixupSmooth history cleanup for perfectionists
![Page 16: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/16.jpg)
@durdn
It’s great to work in small chunks
![Page 17: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/17.jpg)
@durdn
There’s a tension between…
![Page 18: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/18.jpg)
Commits as throw away save points
Commits as a polished meaningful way to communicate a feature
![Page 19: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/19.jpg)
@durdn
There’s a way to square the circle
![Page 20: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/20.jpg)
@durdn
Helps you clean up your private branches before publishing them interactively
What is an --interactive rebase?
master
feature
![Page 21: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/21.jpg)
First step: Turn on the “autosquash” feature
git config --global rebase.autosquash true$
![Page 22: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/22.jpg)
@durdn
Work in small throwaway bits, use commits as save points
as you please
![Page 23: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/23.jpg)
Fixup
Annotate your raw commits with “--fixup” or “--squash”
SquashUse fixup to quickly fix problems in a commit, without the need to change or add to the commit message
For additions to a commit where you want to add also an additional note to the commit message
git commit --fixup <commit>$
git commit --squash -m”Add to feature” <commit>$
![Page 24: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/24.jpg)
Fixup
Annotate your raw commits with “--fixup” or “--squash”
SquashUse fixup to quickly fix problems in a commit, without the need to change or add to the commit message
For additions to a commit where you want to add also an additional note to the commit message
git commit --fixup :/<string pattern>$
git commit --fixup :/PRJ-123$
![Page 25: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/25.jpg)
@durdn
Rebase is now pre-filled and easy
![Page 26: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/26.jpg)
Demo Time! Let’s see how “--fixup” and “--squash” work in practice
![Page 27: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/27.jpg)
Let’s talk about project dependencies
![Page 28: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/28.jpg)
git subtreeExtract project
Alternative to git submodule to handle external dependencies.
Inject dependencyIt allows you to inject an external project into a sub-folder
Introduced in 1.7.11
It can be also used to extract a sub-folder as separate project
![Page 29: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/29.jpg)
Clean integration pointsStores in regular commitsNo training
When and why is git subtree a great choice
Does not require your entire team to be trained in the use of the command
The command stores the external dependency in regular Git commits. Squashing the history optionally.
It makes it easy to see when integrations happened and makes it easy to revert them.
![Page 30: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/30.jpg)
Syntax to inject a project
Command to inject project
git subtree add \ --prefix target-folder \ https://bitbucket.org/team/sub.git \ master --squash
Folder where to insert code
Repository URL
v1.1
![Page 31: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/31.jpg)
Under the hood of git subtree
commit ab54c4e0b75c3107e3e773ab9b39268abddca002
Author: Nicola Paolucci <[email protected]> Date: Tue Sep 29 15:27:35 2015 +0200
Squashed ‘src/sub-project‘ content from commit df563ed git-subtree-dir: src/sub-project git-subtree-split: df563ed15fa6…6b2e95d3
![Page 32: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/32.jpg)
Result of git subtree add
commit 8fb507baf7b270c30c822b27e262d0b44819b4c5
Merge: 606cd3e ab54c4e Author: Nicola Paolucci <[email protected]> Date: Tue Sep 29 15:27:35 2015 +0200
Merge commit 'ab54c4e0b75c3107e3e773ab9b39268abddca002' as '.vim/bundle/fireplace'
commit ab54c4e0b75c3107e3e773ab9b39268abddca002
Author: Nicola Paolucci <[email protected]> Date: Tue Sep 29 15:27:35 2015 +0200
Squashed '.vim/bundle/fireplace/' content from commit df563ed git-subtree-dir: .vim/bundle/fireplace git-subtree-split: df563ed15fa685ce2508bf16b3ca7e176b2e95d3
![Page 33: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/33.jpg)
To keep the sub-project up to date
git subtree pull \ --prefix target-folder \ https://bitbucket.org/team/sub.git \ master --squash
Command to pull project
Folder where to insert code
Repository URL
v1.5
![Page 34: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/34.jpg)
Under the hood of git subtree
commit ab54c4e0b75c3107e3e773ab9b39268abddca002
Author: Nicola Paolucci <[email protected]> Date: Tue Sep 29 15:27:35 2015 +0200
Squashed ‘src/sub-project‘ content from commit df563ed git-subtree-dir: src/sub-project git-subtree-split: df563ed15fa6…6b2e95d3
![Page 35: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/35.jpg)
Find the symbolic ref matching a hash (sha-1)
sha-1 of last commit pulled
Git plumbing to list all remote refs
Repository URL
git ls-remote https://bitbucket.org/team/sub.git | grep df563ed df563ed15fa685ce2508bf16b3ca7e176b2e95d3 v1.1 5eaff1232acedeca565er7e1333234dacccebfff v1.5
git ls-remote https://bitbucket.org/team/sub.git | grep <sha-1>
![Page 36: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/36.jpg)
Aliases to make your life easier!
[alias] sba = "!f() { git subtree add --prefix $2 $1 master --squash; }; f" sbu = "!f() { git subtree pull --prefix $2 $1 master --squash; }; f"
Alias section of your .gitconfig
http://bit.do/git-aliases
![Page 37: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/37.jpg)
How to use the aliases
git sba <repo URL> <destination-folder>
git sba https://bitbucket.org/team/sub.git src/sub
![Page 38: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/38.jpg)
When everyone in the team must work on
sub-projects
When you have constant updates to your dependencies
When you have many dependencies
When NOT to use git subtree
![Page 39: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/39.jpg)
@durdn
For complex project dependencies Use a dependency tool. Really.
![Page 40: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/40.jpg)
Alternatives? Read my rant on project dependencies
http://bit.do/git-dep
![Page 41: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/41.jpg)
@durdn
How to extract a projectLet’s learn how to use subtree split
![Page 42: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/42.jpg)
Git subtree to extract a project
Command to split out project
git subtree split \ --prefix my/project/ \ --branch extracted
Folder prefix to extract
where we store it
![Page 43: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/43.jpg)
Push to new remote
We can remove the contents of the folder from the repo
Import extracted branchInitialise a new repo and import the extracted branch
Remove from old repo
After we imported the code we can push it to a new repository
git rm -rf my/project
git initgit pull ../path/ extracted
git remote add origin …
git push origin -u master
![Page 44: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/44.jpg)
@durdn
Have a Pipeline with your repoBuild your project in the cloud
![Page 45: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/45.jpg)
• Big cool statistic
• 2,569
• Add-Ons in Marketplace
![Page 46: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/46.jpg)
• Big cool statistic
• 2,569
• Add-Ons in Marketplace
![Page 47: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/47.jpg)
• Big cool statistic
• 2,569
• Add-Ons in Marketplace
![Page 48: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/48.jpg)
Deploy to any of these and more!
…
![Page 49: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/49.jpg)
NICOLA PAOLUCCI • ATLASSIAN • @DURDN
Twitter: @durdn
http://bit.do/pipelines
![Page 50: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/50.jpg)
We’ve covered some powerful Git goodies
Worktree clones Painless sub-projectsThe magic of --fixup
For when you have to work concurrently on multiple long running branches
You can use git subtree to handle external libraries in a clean and efficient way
A powerful technique to cleanup your history before sharing with it the team
![Page 51: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/51.jpg)
Thank you! Follow me @durdn
![Page 52: Transformative Git Practices](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f9b9b51a28ab8e4a8b45d9/html5/thumbnails/52.jpg)
NICOLA PAOLUCCI • ATLASSIAN • @DURDN
Twitter: @durdnhttp://bit.do/pipelines
Thank you! Follow me @durdn