SlideShare a Scribd company logo
GIT BASICS
How to quickly use Git for day to day developments
...
AND STASH
Leverage Git on the Enterprise
Created by /
Original presentation on
François D'Agostini @DagoFrancesco
GitHub
WHAT IS GIT ?
DISTRIBUTED VERSION CONTROL
Speed
Simple
Non Linear Development
Fully Distributed
Large Projects
SNAPSHOT BASED, NOT DELTAS
DIFFERENT FROMCVS,SVN...
GIT CONFIGURATION
--system: /etc/gitconfig
--global: ~/.gitconfig
default: repo/.git/config
COLORS IN LINUX:
gitconfig--globaluser.name"FrançoisD'Agostini"
gitconfig--globaluser.email"fdagosti@nds.com"
gitconfig--globalcolor.uialways
CREATION OF A REPO
No need for network
Can do a lot locally on PC
Best for testing
Git init: repo + working directory
git init --bare: Repo only
git clone:From an existing repo
EXAMPLE: FIRST COMMIT
mkdirgit-tests
gitinitjohn.git
gitconfiguser.name"johnDoe"
gitconfiguser.email"john@example.com"
echo"firstlinefromJohn">file1
gitaddfile1
gitstatus
gitcommit-m"initialcommitfromJohn"
ANOTHER COMMIT
cat>>file1
SecondCommitfromJohn
gitcommit-a-m"anothercommit"
WORKING DIRECTORY, INDEXES AND
COMMITS
VIEWING COMMITS HISTORY
...lots of other options !!
git blame: one files only
gitk: for graphic visualization
same options as git log
gitlog
--decorate
--graph
--stat
--oneline
-n
COMMITS HISTORY (2)
git show: details about a commit by default, shows the detail
of the most recent commit
git show-branch: show details about the current branch
HANDY ALIAS
gitconfig--globalalias.graph"log--decorate--graph--oneline"
VIEWING COMMIT DIFFERENCES
git diff: differences between the working directory and the
index
--cached: between the index and the most recent commit
HEAD: between the working directory and the most recent
commit
CANCELLATION, RESTORATION, CLEANING
git reset: cancels changes about to be commited in the
index
--hard: changes also the working directory
git checkout: cancels changes made to working directory
compared to index
git clean: deletes files not followed by Git.
-f option to really delete them
-n to simulate
.gitignore file: to avoid Git to track useless files
BRANCH MANAGEMENT
A Branch is just a pointer to a commit
nothing fancy
very lightweight
very similar to "tags"
stored in .git/refs/[heads|remotes|tags]
Default branch name: master
BRANCH MANAGEMENT (2)
git branch: list branches
-r: remote branches as well
-a all branches
git branch dev: create branch "dev"
Does not change the current branch
git checkout dev: move the current branch to the "dev"
branch
-b: create the branch while switching to it
MERGING
git merge dev:merges "dev" branch into current branch
does not destroy "dev"
git branch -d dev:deletes the "dev" branch
only deletes the pointer
can be deleted only if the branch is accessible from the
current branch
This is usually what is needed after merging. The old
"dev" pointer is no longer useful
in case of future branching, a new branch can be
created
CONFLICT RESOLUTION
git merge can lead to conflicts if the same file has been
modified in two branches
manual conflict resolution is needed on this files
conflict markers are added by git and need to be removed
by the developper
git add on the conflict files
git commit to end the merge
and of course, delete the merged branch
if conflict resolution is too complex:
git merge --abort: restores the working directory to the
state it was prior to merge
REMOTE REPOSITORIES
git remote: lists all remote repos linked to the current
local repo
git remote add name url: adds the specified url as a
remote repo to the local repo
No need in case repo has been created with git clone
git push repo: push local commits on the current branch to
the same branch on the remote repo
warning: the remote repo must already have the same
branch, else use git push repo branch
REMOTES: FETCHING AND PULLING
There is a separate copy of the remote commits separated
from the local branch
Can be seen using git branch -a
this means that the remote and the local copy can easily
be compared
git fetch repo: updates the local copy of the remote repo
git pull: like git fetch, but also merges the remote commit
into this repo's branch
this one can lead to conflicts
GIT REBASE
When fetching commits from remote repos, there are two
ways to merge them:
regular merge: it will create a new commit every time
rebase: it will not create a new commit
allows to change the commits that were not published to
have new parents
very handy when you need to integrate other people
changes but continu your work
use: git rebase branch
git pull --rebase: will use the reboase algorithm in case of
conflicts instead of merge
STASH: SAVE YOUR WORK WHILE SWITCHING
CONTEXT
Allows to save your current context and switch work
Then, you can restore the exact state back
git stash save messages: stores the working director and
the index into a stack
git stash list: lists all the saved stacks
git stash show: shows the details of a stack item
git stash pop: pops a state and applies it on the current
working directory
git stash drop: removes an item on the stack
STASH (2)
warning, stashing does not store the branch state
This means that you can recover your state on any branch
popping a state can create conflicts that needs to be
merged
if a pop failed because of a conflict, it will need to be
removed manually
GIT HOSTING IN THE
COMPANY
Different from git stash seen previously !!
STASH
WHY NEED A GIT HOSTING TOOL?
Git alone is not sufficient
To improve knowledge sharing
to improve code visibility
to get the code out of the darkness !!
STASH CARACTERISTICS
Structuring projects, repos and roles
Browse files, branches, commits and tags
Code Reviews concepts: Pull requests
Repos forking
private repos
Jira linking
PROJECT STRUCTURES
Stash organized by projects that handles multiple repos
permissions are based on projects and repos. Allows for
decentralized admin
users have roles: project creators, admins, system admins,
writer
Soon, anonymous mode
FILE BROWSING
Can browser any file and see source code
Allows to change branches and tags
Can have details of each commit
browse list of commits and log
markdown support to explain source code organization
PULL REQUESTS
Implements best practices with respect to code review
Allows anyone to create a branch, make some code and
ask for its merge
Branch permissions allows to control commits
Pull requests allow to review, make comments, ammend
new commits, see diffs...
Anyone can watch a pull request and be notified
Pull requests can be declined or accepted
FORKING
fork a repo into another repo, but keep history
allows for later merges
PRIVATE REPOS
Develop your own projects
can show or hide repos to others
JIRA LINK
Allows to link Git commit to Jira issues
from a Jira issue, see all related commits
with Git hooks, you can force it
BRANCHING STRATEGIES WITH GIT
Git is small tool that does not come with a whole
environment and rules (think Clearcase...)
Git is versatile and can be used in a dozen of different
ways
Git itself does not enforce how to use branches and how
to work as a team. It keeps this open
But how a serious company is supposed to use git for all its
projects without going messy ?
WENEED BEST PRACTICES !!
GIT FLOW
Standard usages of branches when working with Git
Used in Big projects, with many developers working on it
MAIN CARACTERISTICS
2 long lived branches : Develop and master
3 short lived branches: Feature, Release and HotFix
The master branch is restricted to commit by one or two
people
The Develop branch is restricted to senior developers
New code is added through Feature branches only
STEPS TO FOLLOW
Create a Feature branch, name it to the story/feature you
are working on
once you are happy with your features, you create a pull
request to merge it
STEPS TO FOLLOW (2)
When a release must be done, use a temporary release
branch
The Master branch is used only for "stable" version
commits. Any commits on master should have a Tag
If a bug is found on Master, a "HotFix branch is created to
correct it and merge it back on master

More Related Content

PDF
PPTX
PPTX
Git Pull Requests
PPTX
Git and git workflow best practice
PPT
Open Source Collaboration With Git And Git Hub
PDF
Git and git flow
PDF
Gitting better
PPT
Git workflows
Git Pull Requests
Git and git workflow best practice
Open Source Collaboration With Git And Git Hub
Git and git flow
Gitting better
Git workflows

What's hot (20)

PDF
Git best practices 2016
PPTX
Git flow
PPTX
Git - Simplified For Testers
PPT
Git workflows presentation
PPTX
Git & Github
PPTX
Intro to git and git hub
PDF
Branch to branch by Photis Patriotis
PPTX
Why Aren't You Using Git Flow?
PDF
Git and git hub
PPTX
Git usage (Basics and workflow)
PPTX
Introduction to github slideshare
PDF
Git Ready! Workflows
PDF
Git Workflow With Gitflow
PDF
Git workflows
PDF
Git Tricks — git utilities that make life git easier
PDF
Git for Beginners
PPTX
Gerrit is Getting Native with RPM, Deb and Docker
PDF
Using Github for DSpace development
PDF
Git introduction for Beginners
PPTX
A successful Git branching model
Git best practices 2016
Git flow
Git - Simplified For Testers
Git workflows presentation
Git & Github
Intro to git and git hub
Branch to branch by Photis Patriotis
Why Aren't You Using Git Flow?
Git and git hub
Git usage (Basics and workflow)
Introduction to github slideshare
Git Ready! Workflows
Git Workflow With Gitflow
Git workflows
Git Tricks — git utilities that make life git easier
Git for Beginners
Gerrit is Getting Native with RPM, Deb and Docker
Using Github for DSpace development
Git introduction for Beginners
A successful Git branching model
Ad

Similar to Git basics a starter on git and its ecosystem (20)

PPTX
01 - Git vs SVN
PPTX
Git tips
PDF
Git training
PDF
Intro to Gitflow
PPTX
Mastering git - Workflow
PPTX
Mastering GIT
PPTX
Git more done
PPTX
Git tips and tricks
PDF
Git basics
PPTX
Git and Github
PDF
Git of every day
PDF
Git 101: Force-sensitive to Jedi padawan
PDF
devops-complete-notes-2.pdf
PDF
Git training v10
PPTX
Git and github
PDF
Git and GitHub workflows
PDF
Be a Happier Developer with Git / Productive Team #gettinggitright
PPTX
Git One Day Training Notes
PDF
Collaborative development with git
PPT
01 - Git vs SVN
Git tips
Git training
Intro to Gitflow
Mastering git - Workflow
Mastering GIT
Git more done
Git tips and tricks
Git basics
Git and Github
Git of every day
Git 101: Force-sensitive to Jedi padawan
devops-complete-notes-2.pdf
Git training v10
Git and github
Git and GitHub workflows
Be a Happier Developer with Git / Productive Team #gettinggitright
Git One Day Training Notes
Collaborative development with git
Ad

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PPTX
Spectroscopy.pptx food analysis technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Approach and Philosophy of On baking technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Cloud computing and distributed systems.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
KodekX | Application Modernization Development
Spectroscopy.pptx food analysis technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Approach and Philosophy of On baking technology
Review of recent advances in non-invasive hemoglobin estimation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
sap open course for s4hana steps from ECC to s4
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
Cloud computing and distributed systems.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Advanced methodologies resolving dimensionality complications for autism neur...

Git basics a starter on git and its ecosystem

  • 1. GIT BASICS How to quickly use Git for day to day developments ... AND STASH Leverage Git on the Enterprise Created by / Original presentation on François D'Agostini @DagoFrancesco GitHub
  • 2. WHAT IS GIT ? DISTRIBUTED VERSION CONTROL Speed Simple Non Linear Development Fully Distributed Large Projects
  • 3. SNAPSHOT BASED, NOT DELTAS DIFFERENT FROMCVS,SVN...
  • 4. GIT CONFIGURATION --system: /etc/gitconfig --global: ~/.gitconfig default: repo/.git/config COLORS IN LINUX: gitconfig--globaluser.name"FrançoisD'Agostini" gitconfig--globaluser.email"fdagosti@nds.com" gitconfig--globalcolor.uialways
  • 5. CREATION OF A REPO No need for network Can do a lot locally on PC Best for testing Git init: repo + working directory git init --bare: Repo only git clone:From an existing repo
  • 9. VIEWING COMMITS HISTORY ...lots of other options !! git blame: one files only gitk: for graphic visualization same options as git log gitlog --decorate --graph --stat --oneline -n
  • 10. COMMITS HISTORY (2) git show: details about a commit by default, shows the detail of the most recent commit git show-branch: show details about the current branch HANDY ALIAS gitconfig--globalalias.graph"log--decorate--graph--oneline"
  • 11. VIEWING COMMIT DIFFERENCES git diff: differences between the working directory and the index --cached: between the index and the most recent commit HEAD: between the working directory and the most recent commit
  • 12. CANCELLATION, RESTORATION, CLEANING git reset: cancels changes about to be commited in the index --hard: changes also the working directory git checkout: cancels changes made to working directory compared to index git clean: deletes files not followed by Git. -f option to really delete them -n to simulate .gitignore file: to avoid Git to track useless files
  • 13. BRANCH MANAGEMENT A Branch is just a pointer to a commit nothing fancy very lightweight very similar to "tags" stored in .git/refs/[heads|remotes|tags] Default branch name: master
  • 14. BRANCH MANAGEMENT (2) git branch: list branches -r: remote branches as well -a all branches git branch dev: create branch "dev" Does not change the current branch git checkout dev: move the current branch to the "dev" branch -b: create the branch while switching to it
  • 15. MERGING git merge dev:merges "dev" branch into current branch does not destroy "dev" git branch -d dev:deletes the "dev" branch only deletes the pointer can be deleted only if the branch is accessible from the current branch This is usually what is needed after merging. The old "dev" pointer is no longer useful in case of future branching, a new branch can be created
  • 16. CONFLICT RESOLUTION git merge can lead to conflicts if the same file has been modified in two branches manual conflict resolution is needed on this files conflict markers are added by git and need to be removed by the developper git add on the conflict files git commit to end the merge and of course, delete the merged branch if conflict resolution is too complex: git merge --abort: restores the working directory to the state it was prior to merge
  • 17. REMOTE REPOSITORIES git remote: lists all remote repos linked to the current local repo git remote add name url: adds the specified url as a remote repo to the local repo No need in case repo has been created with git clone git push repo: push local commits on the current branch to the same branch on the remote repo warning: the remote repo must already have the same branch, else use git push repo branch
  • 18. REMOTES: FETCHING AND PULLING There is a separate copy of the remote commits separated from the local branch Can be seen using git branch -a this means that the remote and the local copy can easily be compared git fetch repo: updates the local copy of the remote repo git pull: like git fetch, but also merges the remote commit into this repo's branch this one can lead to conflicts
  • 19. GIT REBASE When fetching commits from remote repos, there are two ways to merge them: regular merge: it will create a new commit every time rebase: it will not create a new commit allows to change the commits that were not published to have new parents very handy when you need to integrate other people changes but continu your work use: git rebase branch git pull --rebase: will use the reboase algorithm in case of conflicts instead of merge
  • 20. STASH: SAVE YOUR WORK WHILE SWITCHING CONTEXT Allows to save your current context and switch work Then, you can restore the exact state back git stash save messages: stores the working director and the index into a stack git stash list: lists all the saved stacks git stash show: shows the details of a stack item git stash pop: pops a state and applies it on the current working directory git stash drop: removes an item on the stack
  • 21. STASH (2) warning, stashing does not store the branch state This means that you can recover your state on any branch popping a state can create conflicts that needs to be merged if a pop failed because of a conflict, it will need to be removed manually
  • 22. GIT HOSTING IN THE COMPANY Different from git stash seen previously !! STASH
  • 23. WHY NEED A GIT HOSTING TOOL? Git alone is not sufficient To improve knowledge sharing to improve code visibility to get the code out of the darkness !!
  • 24. STASH CARACTERISTICS Structuring projects, repos and roles Browse files, branches, commits and tags Code Reviews concepts: Pull requests Repos forking private repos Jira linking
  • 25. PROJECT STRUCTURES Stash organized by projects that handles multiple repos permissions are based on projects and repos. Allows for decentralized admin users have roles: project creators, admins, system admins, writer Soon, anonymous mode
  • 26. FILE BROWSING Can browser any file and see source code Allows to change branches and tags Can have details of each commit browse list of commits and log markdown support to explain source code organization
  • 27. PULL REQUESTS Implements best practices with respect to code review Allows anyone to create a branch, make some code and ask for its merge Branch permissions allows to control commits Pull requests allow to review, make comments, ammend new commits, see diffs... Anyone can watch a pull request and be notified Pull requests can be declined or accepted
  • 28. FORKING fork a repo into another repo, but keep history allows for later merges
  • 29. PRIVATE REPOS Develop your own projects can show or hide repos to others
  • 30. JIRA LINK Allows to link Git commit to Jira issues from a Jira issue, see all related commits with Git hooks, you can force it
  • 31. BRANCHING STRATEGIES WITH GIT Git is small tool that does not come with a whole environment and rules (think Clearcase...) Git is versatile and can be used in a dozen of different ways Git itself does not enforce how to use branches and how to work as a team. It keeps this open But how a serious company is supposed to use git for all its projects without going messy ? WENEED BEST PRACTICES !!
  • 32. GIT FLOW Standard usages of branches when working with Git Used in Big projects, with many developers working on it
  • 33. MAIN CARACTERISTICS 2 long lived branches : Develop and master 3 short lived branches: Feature, Release and HotFix The master branch is restricted to commit by one or two people The Develop branch is restricted to senior developers New code is added through Feature branches only
  • 34. STEPS TO FOLLOW Create a Feature branch, name it to the story/feature you are working on once you are happy with your features, you create a pull request to merge it
  • 35. STEPS TO FOLLOW (2) When a release must be done, use a temporary release branch The Master branch is used only for "stable" version commits. Any commits on master should have a Tag If a bug is found on Master, a "HotFix branch is created to correct it and merge it back on master