Git tips
-
Upload
arthur-shvetsov -
Category
Software
-
view
337 -
download
0
Transcript of Git tips
![Page 1: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/1.jpg)
Git Tips
by Arthur ShvetsovCoreValue/MalkosUA
![Page 2: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/2.jpg)
Agenda● Git common principles and design goals● Merging vs. Rebasing● How to rewrite history?● A successful Git branching model
![Page 3: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/3.jpg)
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.
![Page 4: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/4.jpg)
Git design goals● Speed● Simple design● Strong support for thousands of parallel branches● Fully distributed
o full local repositoryo offline commitso full size repository
● Able to handle large projects like Linux kernel effectively● Ensure integrity
![Page 5: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/5.jpg)
Git drawbacks● Big committed files will be in every clone of
repository● No partial checkout● No lock● No support of empty directories
![Page 6: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/6.jpg)
Git philosophy● Commit early, commit often● One commit represents one idea or one
changeo Make it easy to read patcheso Easy to revert unwanted changes later
● Your working directory, index and local repository are your scratch pads
![Page 7: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/7.jpg)
Git architecture
![Page 8: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/8.jpg)
Nearly Every Operation Is Local
![Page 9: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/9.jpg)
Snapshots, Not DifferencesThe major difference between Git and any other VCS (Subversion and friends included) is the way Git thinks about its data.
![Page 10: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/10.jpg)
Tracking changes (most VCS)
Storing data as changes to a base version of each file
![Page 11: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/11.jpg)
Tracking changes (the Git way)
Storing data as snapshots of the project over time
![Page 12: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/12.jpg)
Git Generally Only Adds Data
![Page 13: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/13.jpg)
File status lifecycle
![Page 14: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/14.jpg)
The Staging AreaThe staging area is a buffer between the
working directory and the project repository.
![Page 15: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/15.jpg)
What happens when you create a commit ?
![Page 16: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/16.jpg)
![Page 17: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/17.jpg)
What happens when you create a new branch?$ git branch testing
![Page 18: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/18.jpg)
How does Git know what branch you’re currently on?
![Page 19: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/19.jpg)
What happens when you switch a branch?$ git checkout testing
![Page 20: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/20.jpg)
Merging vs. Rebasing
![Page 21: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/21.jpg)
Merging vs. Rebasinggit 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.
![Page 22: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/22.jpg)
A forked commit historyTo incorporate the new commits into your feature branch, you have two options: merging or rebasing.
![Page 23: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/23.jpg)
Merging master into feature branch$ git checkout feature$ git merge master
![Page 24: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/24.jpg)
Pros● Simple to use and understand● Merging is a non-destructive operation● The existing branches are not changed in any wayCons● 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
![Page 25: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/25.jpg)
The Rebase Option$ git checkout feature$ git rebase master
![Page 26: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/26.jpg)
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
![Page 27: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/27.jpg)
Git rebase golden ruleNever use git rebase on public branches. Only rebase local branches
![Page 28: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/28.jpg)
When you rebase pushed branch, you’re abandoning existing commits and creating
new ones that are similar but different
![Page 29: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/29.jpg)
Interactive Rebase$ git checkout feature$ git rebase -i master
![Page 30: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/30.jpg)
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 :)
![Page 31: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/31.jpg)
Force Pushing$ git push -f origin feature
![Page 32: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/32.jpg)
Integrating an Approved Feature
![Page 33: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/33.jpg)
Integrating an Approved Feature
![Page 34: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/34.jpg)
Integrating an Approved Feature.“Fast forward” effect
![Page 35: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/35.jpg)
Rebasing a temporary branch git checkout featuregit checkout -b temporary-branchgit rebase -i master
git checkout master git merge temporary-branch
![Page 36: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/36.jpg)
Git pull --rebaseBy 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
![Page 37: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/37.jpg)
How to rewrite history?
![Page 38: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/38.jpg)
Git revertReverting 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.
![Page 39: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/39.jpg)
$ git checkout hotfix$ git revert HEAD~2
![Page 40: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/40.jpg)
Git resetGit 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.
![Page 41: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/41.jpg)
$ git checkout hotfix$ git reset HEAD~2
![Page 42: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/42.jpg)
Reverting vs. Resetting
![Page 43: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/43.jpg)
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
![Page 44: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/44.jpg)
“Detached HEAD” state$ git checkout HEAD~2
![Page 45: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/45.jpg)
Multiple ways how to rewrite history?● $ git reset● $ git rebase
![Page 46: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/46.jpg)
Git cherry-pickApplies the changes introduced by some existing commits to specific branch
$ git checkout feature-branch$ git cherry-pick <commit hash>
![Page 47: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/47.jpg)
StashingRecords the current state of the working directory and the index, and cleans up the working directory
$ git stash
![Page 48: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/48.jpg)
SubmodulesSubmodule allows you to keep another Git repository in a subdirectory of your repository
$ git submodule
![Page 49: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/49.jpg)
A successful Git branching model
![Page 50: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/50.jpg)
Decentralized but centralized
![Page 51: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/51.jpg)
The main branches● master
o contains production ready code● develop
o an integration brancho contains the latest changeso used for nightly buildso getting a production release
when merging with master
![Page 52: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/52.jpg)
Supporting branches● Feature branches● Release branches● Hotfix branches
![Page 53: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/53.jpg)
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-*
![Page 54: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/54.jpg)
Creating a feature branch$ git checkout -b myfeature developSwitched 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
![Page 55: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/55.jpg)
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
![Page 56: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/56.jpg)
Creating a release branch$ git checkout -b release-1.2 develop Switched to a new branch "release-1.2"
![Page 57: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/57.jpg)
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).
![Page 58: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/58.jpg)
Hotfix branches● May branch off from:
o master ● Must merge back into:
o develop and master ● Branch naming convention:
o hotfix-*
![Page 59: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/59.jpg)
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(-)
![Page 60: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/60.jpg)
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).
![Page 61: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/61.jpg)
Links and Literature● Pro Git● A successful Git branching model● https://www.atlassian.com/git/tutorials/● https://help.github.com/
![Page 62: Git tips](https://reader036.fdocuments.us/reader036/viewer/2022070510/58ab8bf31a28ab3e738b6a55/html5/thumbnails/62.jpg)
Thank You!