Git

O que é Git?


Sistema de controle de versão Distribuído

Centralizado 

Distribuído 

Por que Git?

  • Rápido para comitar 
  • Offline
  • Segurança

Por que Git?
















Source: RebelLabs 2013

Git vs Subversion


SVN salva as diferenças entre arquivos

Git salva o snapshop da situação de cada arquivo

Source: Pro git

Introdução











Source: Greenido

http://greenido.files.wordpress.com/2013/07/git-local-remote.png?w=696&h=570

Introdução

Source: Atlassian

Trabalhando local


Configuração

  • Open a terminal
  • Type 
    git help  
  • Create a new folder "gitTest" and access it
  • Type
    git init 
  • Open .git folder

Working locally


Configure









  • Type
    git config 
  • Type
    git config --global user.name "yourName" git config --global user.email "yourMail" 
  • Type 
    git config --list 

Working locally


git upload steps


 

Working locally



Staging a file

  • On project folder create a file  "yourname.txt"
  • Type
    git status 
  • Type
    git add "yourname.txt" 
  • Type
    git status -v

Working locally

Commiting a file

  • Type
    git commit -m "added initial file" git status  
  • Modify your file
  •  type
    git status  
  • Do another commit
    git commit -a -m "Modified file" 
  • Type
    git log 

Working locally

Unstaging a file

  • Modify your file
  • Type
    git add *.txt
  • Type
    git status 
  • Type
    git reset HEAD 
  • Type
    git status 

Working locally



getting a file back to HEAD







  • Open the file
  • Type
    git status 
    git checkout -- "filename"
  • Type
    git status 
  • Open file again
 

Working locally

- reset HEAD~: Returns the staging area to last commit but does not modify the files.
- reset --soft HEAD~:Undoes the commit but does not modify the files.
- reset --hard HEAD~: Undoes the commit and modifies the files to last commit.

Uncommiting a file

  • Create a new file "secondFile.txt"
  • Commit it (you know how)
  • Type
    git log --oneline --decorate 
  • Type
    git reset --soft HEAD~
    git log --oneline --decorate
    git status

Working locally


Adding to a commit

  • Type
    git commit --amend -m "modified initial and added second" 
  • Type
    git status 
  • Type
    git log --oneline -p

Branching


What it is?


Switching of timelines

Branching


Creating a branch

  • Type
    git branch develop
  • Type
    git branch 
  • Type
    git checkout develop
  • Type
    git branch   

Branching


Working on a branch

  • Create a new file release.txt
  • commit it
  • Type
    git log --decorate
  • Type command ls / dir
  • Type
    git checkout master
    git log  --decorate
  • Type command ls / dir

Branching


Merging a branch

  • Add commit of one branch to the other
  • Two possible scenario:
    • There were no commits on the other branch
    • The other branch also had commits



    Branching


    Fast-forward Merging 

    • Verify you're on master branch
    • Type
       git merge develop
    • Type
       git log --decorate

    Branching

    Recursive Merging

    • Type
      git checkout -b featureX
    • Add a file to it and commit
    • Go to develop branch
    • Modify "release.txt" file and commit
    • Type
      git merge featureX 
    • If a vi screen appears, don't panic. just  :wq!
    • Type
      git log --graph --oneline --decorate --all

     

    Branching

    Merge conflict

    • Occurs in a recursive merge when a file has been modified on two branches 
    • Git will stop the merging and wait until resolution. 
    • Process:
      • Modify file
      • Stage file
      • Commit change

    Branching

    Solving a merge conflict

    • Go to branch featureX
    • Edit and commit file feature.txt
    • Go to branch develop and do the same
    • Type
      git merge featureX
    • Open and modify file
    • Type
      git add -all
      git commit




    Branching


    Deleting a branch

    • Type
      git branch -d featureX 
    • Type
       git branch 
    • Type
      git log --decorate




    Branching  


    Source: Atlassian
    



    Rebase

    • Move all changes on one branch that are not on the other to a temporary stack.
    • Apply all the changes of the other branch on the first branch
    • Apply all the changes of the temporary stack.

    Rebase is another way of merging. What it does is :

     




    Do not merge the upstream develop with your feature branch; rebase your branch on top of the upstream develop.


    Never rebase shared commits. Rebase only the branch where no one have checked in yet


    Rebase  vs. Merging

    Branching

     

    Rebasing

    Branching

    • Create branch featureXX
    • Add and commit a new file on it
    • Checkout to develop, modify and commit a file

    • Type
      git rebase featureXX
    • Type
      git log --graph --oneline --decorate --all
      git branch -d featureXX

    Branching




    Rebase conflict

    • Changes on the same file also generate conflict just like merge
    • To solve the problem:
      • Edit manually the conflict
      • git add <file>
      • git rebase --continue

    Going remote!


    What  is a remote and what I do with it?

    A central repository where different developers can upload and download their commits from their local repository.

    Going remote!


    Creating a remote (on github)

    • Go to github
    • On your profile, new repository
    • Name it git-workshop
    • Copy the  HTTPS url !!!

    Going remote!


    Adding a remote

    • Type
      git remote add origin URL 
    • Type
      git remote -v  


    Going remote!


    Pushing to remote

    • Type
      git push -u origin develop
    • Enter your github name and pass
    • Go to github and watch commit log

    Going remote!


    Cloning a repository

    • Go up on your folders (cd .. )
    • Type 
      git clone https://github.com/bgorriz/git-workshop.git 
    • Go to git-workshop folder
    • Type
      git remote -v 
    • Type
      git remote show origin
    • Type
      git log --oneline --decorate 



    Going remote!


    Pulling from remote

    • Type
      git pull 
    • Type
      git log --decorate

    Going remote!


    What pull does

    • It is just :
      • git fetch : Downloads remote commits and creates a new branch on the local repository with those changes named origin/develop
      • git merge origin/develop: Same as we did at branching

    Going remote!


    Recursive merge on pull 

    • Copy "yourname".txt to this folder
    • Commit your changes
    • Push your changes (you should receive an error)
    • Type
      git pull 
    • Type
      git log  
    • Now really push your changes

    Going remote!


    Merge conflict

    • Modify bgorriz.txt and commit
    • Push your change!
    • If you pull, there is a conflict merging.
    • Solving conflict:
      • Edit file
      •  git commit -a
    • Now a recursive merging occurs

    Remote branching


    Why?

    You may need other people to work on a feature.

    You may want different timelines with certain commits on remote.


    Remote branching


    Creating a remote branch

    • Go back to gitTest
    • Go to master branch
    • Type
      git push -u origin master
    • Modify the file and commit
    • Type
      git push 
    • Go to github and watch branches

    Remote branching


    pulling a remote branch

    • Type
      git branch 
    • Type
      git pull
    • Type
      git checkout  -b master origin/master 
    • Type
      git remote show origin 

    Remote branching


    Removing a remote branch

    • Type
      git push origin:branchName 
    • Type
      git branch -d branchName 

    Tagging


    What is a tag?


    • A reference to a certain commit
    • Points to a important status of the repository
    • Generally, marks a release version


    Git has two ways of tagging:

    • Lightweight: A pointer to a specific commit
    • Annotated: A  full object stored in the Git database

      Tagging


      Creating an annotated tag

      • Go to master branch
      • Type
        git tag -a v0.0.1 -m "version 0.0.1" 
      • Type
        git tag 

      Tagging


      Pushing a tag


      • Type
        git push origin --tags 
      • Type
        git checkout  v0.0.1 

      Tagging


      Signed tagging

      •  GPG  can be used to sign the tag. You need a private key.
      • Use -s instead of -a
        git tag -s v0.0.1 -m "tag message" 
      • To verify the tag type
        git tag -v "tagname" 
        You will need the signer public key.

      Stashing

      What is it ?

      Store some file in a temporary area.

      Stashing


      Stash a file

      •  Modify yourname.txt file
      • Type
        git diff 
        
        git status  
      • Type
        git stash save 
         
      • Type
        git diff
        
        git status  

      Stashing


      Apply a stash

      • Type
        git stash list 
      • Type
        git stash  apply 
      • Type
        git diff
        
        git stash list  
      • Type
        git stash drop
        
         git stash list  
       

      reflog

      What is it ?

      Git keeps track of updates to the tip of branches using a mechanism called reflog. This allows you to go back to changesets even though they are not referenced by any branch or tag.

      Source:  cambrico

      reflog



      Typical use


      • Restore deleted commits
      • Restore deleted branches

      reflog

      Restoring a commit


      • Create a new file and commit it
      • Type
        git reset --hard HEAD~
      • type
        git log 
        git reflog
      • Type
         git reset --hard "sha"

      reflog

      Restoring a branch


      • Create a new branch and commit a file
      • Go back to develop
      • Type
        git branch -D "branchname"
      • type
        git log 
        git reflog
      • Type
         git branch "branchname" "sha"
        git checkout "branchname"
        git log

      Gitignore


      What is it?


      A file tha specifies intentionally untracked files that Git should ignore. 

      Files already tracked by Git are not affected !!

      Gitignore



      Creating the file

      Gitignore

      Untracking a file

      Type
      git rm --chaced filename
       

      Practical case

      Before we continue


      Install git flow

      Scenario


      • International project with a distributed developer team.
      • Bdigital team is in management.
      • Use of git flow to manage development.

      Git flow


      What git flow is


      What git flow is not

      • A different VCS apart from git .
      • A closed software management tool.



      Source: K:\ENGINYERIA SOFTWARE\PUBLIC
      

      git flow

      Permanent branches

      • Master branch containing production-ready code
        • should always reflect a production-ready state.
      • Develop (aka Integration, i.e. nightly builds) containing the main development
        • It reflects the latest delivered development changes for the next release.
        • It should not contain highly experimental code but should rather be considered as an "integration branch".
        • Work happens on the develop branch. Small changes are direct commits here.

      git flow

      Temporary branches

      • Feature branch used to develop new features,fixes or experiments for the upcoming or a distant future
        • Would exist as long as the feature, fix or experiment is in development.
      • Release branch used to prepare a new production release as well as to keep a record of the different releases that took us to a certain production version
      • Hotfix branch used when a critical bug is found in a production release.

      git flow

      naming conventions

      • Tags:
        • develop: dev-major.minor.patch_yyyymmdd (i.e. dev-1.2.07_20140110)
        • master: v-major.minor.patch_yyyymmdd (i.e. v-1.2_20140110)
      • Temporal branches:
        • feature: fix|feature|exp-RedmineOrJiraIssueNumber_yyyymmdd
        • release: rc-major.minor.patch_yyyymmdd
        • hotfix: hotfix-RedmineOrJiraIssueNum_date

      git flow

      Creating a project

      • create a folder gitFlowTest
      • Type
        git flow init 
      • leave everything by default
      • Type
        git branch 

      git flow

      Creating git flow project from existing one

      • create a git project gitFlowTest
      • Add remote repository
        git@github.com:bgorriz/gitflowTest.git
      • Add some commits to master
      • Type
        git config push.default current
        git flow init
        git push -u origin develop
        git remote

      git flow


      Cloning a project

      • Type
        git clone git@github.com:bgorriz/gitflowTest.git
      • Enter the project folder
      • Type
        git flow feature 
      • Type
        git config push.default current
        git flow init -d
        git remote show origin

      git flow

      Developing a feature

      • Team in charge of feature
        • Type
      • git flow feature start "convention"
        • Type
      • git branch
        • Make some commits
        • Type
      • git log --oneline

      git flow

      Publishing a  feature

      • Team in charge of feature:
        • Type
      • git flow feature publish "covention"
        • Go to github and look branches

      git flow

      Pulling a  feature

      • Second team :
        • Type
      • git flow feature track "convention"
        git remote show origin
        git log --oneline
        • Create another file ,commit and push it

      git flow

      Finishing a  feature

      • First team: 
        • Commit final change
        • Type
      • git pull --rebase 
        git flow feature finish -F "convention"
        • Type
          git branch 
          git log
          git push

      git flow

      Finishing a  feature

      • Second team: 
        • Type
      • git checkout develop
        git branch -d feature/"convention"
        git pull --rebase
        • Type
          git branch 
          git log

      git flow

      Starting a  release

      • Type: 
      • git flow release start rc-1.0.0_20140121
        git branch

      git flow

      Fixing a  release

      • Add file to branch and commit
      • Optional: publish the release for team working just as we did with feature

      git flow

      Finishing a  release

      • Type
        git flow release finish rc-1.0.0_20140122
        git push
        git push --tags
      • Go to branch develop
      • Type
        git status
        git pull --rebase
        git push
        git log

      git flow

      hotfixes

      • Type
        git flow hotfix start hotfix-002_20140122
        git branch
      • Modify some files and commit
      • Type
        git flow hotfix finish hotfix-002_20140122
        git branch
        git pull --tags

      Also


      • git revert
      • git alias
      • git clean
      • git blame
      • git bisect
      • git cherrypick
      • git submodules
      • ....

      More info


      Danke schön

      Made with Slides.com