Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Git Tips
by Arthur Shvetsov
CoreValue/MalkosUA
Agenda
● Git common principles and design goals
● Merging vs. Rebasing
● How to rewrite history?
● A successful Git branch...
What is Git?
● Git is a distributed revision control and source code
management (SCM) system with an emphasis on
speed, da...
Git design goals
● Speed
● Simple design
● Strong support for thousands of parallel branches
● Fully distributed
o full lo...
Git drawbacks
● Big committed files will be in every clone of
repository
● No partial checkout
● No lock
● No support of e...
Git philosophy
● Commit early, commit often
● One commit represents one idea or one
change
o Make it easy to read patches
...
Git architecture
Nearly Every Operation Is Local
Snapshots, Not Differences
The major difference between Git and any
other VCS (Subversion and friends included) is
the way...
Tracking changes (most VCS)
Storing data as changes to a base version of each file
Tracking changes (the Git way)
Storing data as snapshots of the project over time
Git Generally Only Adds Data
File status lifecycle
The Staging Area
The staging area is a buffer between the
working directory and the project repository.
What happens when you create a
commit ?
What happens when you create a
new branch?
$ git branch testing
How does Git know what branch
you’re currently on?
What happens when you switch a
branch?
$ git checkout testing
Merging vs. Rebasing
Merging vs. Rebasing
git rebase and git merge - both of these
commands are designed to solve the same
problem - integrate ...
A forked commit history
To incorporate the new commits into your feature branch,
you have two options: merging or rebasing.
Merging master into feature branch
$ git checkout feature
$ git merge master
Pros
● Simple to use and understand
● Merging is a non-destructive operation
● The existing branches are not changed in an...
The Rebase Option
$ git checkout feature
$ git rebase master
Pros
● Much cleaned project history
● Eliminates the unnecessary merge commits required by git merge
● A perfectly linear ...
Git rebase golden rule
Never use git rebase on public branches. Only rebase
local branches
When you rebase pushed branch, you’re
abandoning existing commits and creating
new ones that are similar but different
Interactive Rebase
$ git checkout feature
$ git rebase -i master
Pros
● Eliminating insignificant commits like this makes
your feature’s history much easier to understand.
This is somethi...
Force Pushing
$ git push -f origin feature
Integrating an Approved Feature
Integrating an Approved Feature
Integrating an Approved Feature.
“Fast forward” effect
Rebasing a temporary branch
git checkout feature
git checkout -b temporary-branch
git rebase -i master
git checkout master...
Git pull --rebase
By default, the git pull command performs a
merge, but you can force it to integrate the
remote branch w...
How to rewrite history?
Git revert
Reverting undoes a commit by creating a new
commit. This is a safe way to undo changes, as
it has no chance of ...
$ git checkout hotfix
$ git revert HEAD~2
Git reset
Git reset as the dangerous method. When you
undo with git reset, there is no way to retrieve
the original copy—i...
$ git checkout hotfix
$ git reset HEAD~2
Reverting vs. Resetting
git checkout
● checking out branches
● checking out commits - makes the entire
working directory match that commit
● check...
“Detached HEAD” state
$ git checkout HEAD~2
Multiple ways how to rewrite
history?
● $ git reset
● $ git rebase
Git cherry-pick
Applies the changes introduced by some
existing commits to specific branch
$ git checkout feature-branch
$...
Stashing
Records the current state of the working
directory and the index, and cleans up the
working directory
$ git stash
Submodules
Submodule allows you to keep another Git
repository in a subdirectory of your repository
$ git submodule
A successful Git branching model
Decentralized but centralized
The main branches
● master
o contains production ready code
● develop
o an integration branch
o contains the latest change...
Supporting branches
● Feature branches
● Release branches
● Hotfix branches
Feature branches (topic branches)
● May branch off from:
o develop
● May merge back into:
o develop
● Branching name conve...
Creating a feature branch
$ git checkout -b myfeature develop
Switched to a new branch “myfeature”
Incorporating a finishe...
Release branches
● May branch off from:
o develop
● Must merge back into:
o develop and master
● Branch naming convention:...
Creating a release branch
$ git checkout -b release-1.2 develop
Switched to a new branch "release-1.2"
Finishing a release branch
$ git checkout master
Switched to branch 'master'
$ git merge release-1.2
Merge made by recursi...
Hotfix branches
● May branch off from:
o master
● Must merge back into:
o develop and master
● Branch naming convention:
o...
Creating a hotfix branch
$ git checkout -b hotfix-1.2.1 master
Switched to a new branch "hotfix-1.2.1"
$ git commit -m "Fi...
Finishing a hotfix branch
$ git checkout master
Switched to branch 'master'
$ git merge hotfix-1.2.1
Merge made by recursi...
Links and Literature
● Pro Git
● A successful Git branching model
● https://www.atlassian.com/git/tutorials/
● https://hel...
Thank You!
Git tips
Upcoming SlideShare
Loading in …5
×

Git tips

  • Login to see the comments

  • Be the first to like this

Git tips

  1. 1. Git Tips by Arthur Shvetsov CoreValue/MalkosUA
  2. 2. Agenda ● Git common principles and design goals ● Merging vs. Rebasing ● How to rewrite history? ● A successful Git branching model
  3. 3. What is Git? ● Git is a distributed revision control and source code management (SCM) system with an emphasis on speed, data integrity, and support for distributed, non- linear workflows. ● Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005, and has since become the most widely adopted version control system for software development.
  4. 4. Git design goals ● Speed ● Simple design ● Strong support for thousands of parallel branches ● Fully distributed o full local repository o offline commits o full size repository ● Able to handle large projects like Linux kernel effectively ● Ensure integrity
  5. 5. Git drawbacks ● Big committed files will be in every clone of repository ● No partial checkout ● No lock ● No support of empty directories
  6. 6. Git philosophy ● Commit early, commit often ● One commit represents one idea or one change o Make it easy to read patches o Easy to revert unwanted changes later ● Your working directory, index and local repository are your scratch pads
  7. 7. Git architecture
  8. 8. Nearly Every Operation Is Local
  9. 9. Snapshots, Not Differences The major difference between Git and any other VCS (Subversion and friends included) is the way Git thinks about its data.
  10. 10. Tracking changes (most VCS) Storing data as changes to a base version of each file
  11. 11. Tracking changes (the Git way) Storing data as snapshots of the project over time
  12. 12. Git Generally Only Adds Data
  13. 13. File status lifecycle
  14. 14. The Staging Area The staging area is a buffer between the working directory and the project repository.
  15. 15. What happens when you create a commit ?
  16. 16. What happens when you create a new branch? $ git branch testing
  17. 17. How does Git know what branch you’re currently on?
  18. 18. What happens when you switch a branch? $ git checkout testing
  19. 19. Merging vs. Rebasing
  20. 20. Merging vs. Rebasing git rebase and git merge - both of these commands are designed to solve the same problem - integrate changes from one branch into another branch — they just do it in very different ways.
  21. 21. A forked commit history To incorporate the new commits into your feature branch, you have two options: merging or rebasing.
  22. 22. Merging master into feature branch $ git checkout feature $ git merge master
  23. 23. Pros ● Simple to use and understand ● Merging is a non-destructive operation ● The existing branches are not changed in any way Cons ● The feature branch will have an extraneous merge commit every time you need to incorporate changes ● Visual charts of repository can have non-informative branch lines Git merge pros and cons
  24. 24. The Rebase Option $ git checkout feature $ git rebase master
  25. 25. Pros ● Much cleaned project history ● Eliminates the unnecessary merge commits required by git merge ● A perfectly linear project history Cons ● Re-writing project history can be potentially catastrophic for your collaboration workflow ● Rebasing loses the context provided by a merge commit - you can’t see when upstream changes were incorporated into the feature Git rebase pros and cons
  26. 26. Git rebase golden rule Never use git rebase on public branches. Only rebase local branches
  27. 27. When you rebase pushed branch, you’re abandoning existing commits and creating new ones that are similar but different
  28. 28. Interactive Rebase $ git checkout feature $ git rebase -i master
  29. 29. Pros ● Eliminating insignificant commits like this makes your feature’s history much easier to understand. This is something that git merge simply cannot do Interactive rebase pros, no cons :)
  30. 30. Force Pushing $ git push -f origin feature
  31. 31. Integrating an Approved Feature
  32. 32. Integrating an Approved Feature
  33. 33. Integrating an Approved Feature. “Fast forward” effect
  34. 34. Rebasing a temporary branch git checkout feature git checkout -b temporary-branch git rebase -i master git checkout master git merge temporary-branch
  35. 35. Git pull --rebase By default, the git pull command performs a merge, but you can force it to integrate the remote branch with a rebase by passing it the -- rebase option. $ git config branch.autosetuprebase always
  36. 36. How to rewrite history?
  37. 37. Git revert Reverting undoes a commit by creating a new commit. This is a safe way to undo changes, as it has no chance of re-writing the commit history.
  38. 38. $ git checkout hotfix $ git revert HEAD~2
  39. 39. Git reset Git reset as the dangerous method. When you undo with git reset, there is no way to retrieve the original copy—it is a permanent undo.
  40. 40. $ git checkout hotfix $ git reset HEAD~2
  41. 41. Reverting vs. Resetting
  42. 42. git checkout ● checking out branches ● checking out commits - makes the entire working directory match that commit ● checking out files - lets you see an old version of that particular file, leaving the rest of your working directory untouched
  43. 43. “Detached HEAD” state $ git checkout HEAD~2
  44. 44. Multiple ways how to rewrite history? ● $ git reset ● $ git rebase
  45. 45. Git cherry-pick Applies the changes introduced by some existing commits to specific branch $ git checkout feature-branch $ git cherry-pick <commit hash>
  46. 46. Stashing Records the current state of the working directory and the index, and cleans up the working directory $ git stash
  47. 47. Submodules Submodule allows you to keep another Git repository in a subdirectory of your repository $ git submodule
  48. 48. A successful Git branching model
  49. 49. Decentralized but centralized
  50. 50. The main branches ● master o contains production ready code ● develop o an integration branch o contains the latest changes o used for nightly builds o getting a production release when merging with master
  51. 51. Supporting branches ● Feature branches ● Release branches ● Hotfix branches
  52. 52. Feature branches (topic branches) ● May branch off from: o develop ● May merge back into: o develop ● Branching name convention: o anything except master, develop, release-* or hotfix-*
  53. 53. Creating a feature branch $ git checkout -b myfeature develop Switched to a new branch “myfeature” Incorporating a finished feature on develop $ git checkout develop Switched to branch 'develop' $ git merge myfeature Updating ea1b82a..05e9557 (Summary of changes) $ git branch -d myfeature Deleted branch myfeature (was 05e9557). $ git push origin develop
  54. 54. Release branches ● May branch off from: o develop ● Must merge back into: o develop and master ● Branch naming convention: o release-* ● Used for preparation of a new production release ● Allow minor bug-fixes
  55. 55. Creating a release branch $ git checkout -b release-1.2 develop Switched to a new branch "release-1.2"
  56. 56. Finishing a release branch $ git checkout master Switched to branch 'master' $ git merge release-1.2 Merge made by recursive. (Summary of changes) $ git tag -a 1.2 $ git checkout develop Switched to branch 'develop' $ git merge release-1.2 Merge made by recursive. (Summary of changes) $ git branch -d release-1.2 Deleted branch release-1.2 (was ff452fe).
  57. 57. Hotfix branches ● May branch off from: o master ● Must merge back into: o develop and master ● Branch naming convention: o hotfix-*
  58. 58. Creating a hotfix branch $ git checkout -b hotfix-1.2.1 master Switched to a new branch "hotfix-1.2.1" $ git commit -m "Fixed severe production problem" [hotfix-1.2.1 abbe5d6] Fixed severe production problem 5 files changed, 32 insertions(+), 17 deletions(-)
  59. 59. Finishing a hotfix branch $ git checkout master Switched to branch 'master' $ git merge hotfix-1.2.1 Merge made by recursive. (Summary of changes) $ git tag -a 1.2.1 $ git checkout develop Switched to branch 'develop' $ git merge hotfix-1.2.1 Merge made by recursive. (Summary of changes) $ git branch -d hotfix-1.2.1 Deleted branch hotfix-1.2.1 (was abbe5d6).
  60. 60. Links and Literature ● Pro Git ● A successful Git branching model ● https://www.atlassian.com/git/tutorials/ ● https://help.github.com/
  61. 61. Thank You!

×