GIT -

Introdução

agenda

  • Estruturas
  • Comandos
  • Fluxo
  • Passos
  • WORKING WITH
  • HOOKS

GIT STRUCTURE

Data model

GIT COMMANDS

basics

GIT workflow

basics

Data model

Estrutura inicial 

 

Pastas?

 

Hooks?

$ git init
$ tree .git/
.git/
├── HEAD
├── config
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── prepare-commit-msg.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

DATA Model

40-character checksum (SHA-1)

2 - directory name

38 - file (object) name

$ touch index.php

    <?php
    echo "Hello World";

$ touch README.md

    # Description
    This is my hello world project

$ git add .
$ git commit -m "Initial Commit"
//UPDATED STRUCTURE
├── objects
│   ├── 5d
│   │   └── 92c127156d3d86b70ae41c73973434bf4bf341
│   ├── a6
│   │   └── dbf05551541dc86b7a49212b62cfe1e9bb14f2
│   ├── cf
│   │   └── 59e02c3d2a2413e2da9e535d3c116af1077906
│   ├── f8
│   │   └── 9e64bdfcc08a8b371ee76a74775cfe096655ce
│   ├── info
│   └── pack

data model

$ touch index.php

    <?php
    echo "Hello World";

$ touch README.md

    # Description
    This is my hello world project

$ git add .
$ git commit -m "Initial Commit"

data model

  1. blob object
    1. Snapshot
    2. content
    3. timestamp
  2. tree object
    1. list of files
    2. pointer to the files
  3. commit object
    1. pointer to tree object

CLONE

  • Clonar um repositório:
    • localizado:
      • local
      • remoto
    • tipo:
      • existente / opções
      • vazio
    • Protocolos:
      • HTTP/s
      • SSH 
# Cloning from known source
git clone ssh://ex@example.com/path/my-project.git
cd my-project
# Start working on the project

# Cloning alternatives
git clone <repo> <directory>
git clone --branch <tag> <repo>
git clone -depth=1 <repo>
git clone -branch new_feature git://remrepository.git

init

  • Inicializa um repositório git:
    • Repo:
      • vazio
      • existente
    • Clone vs init
# Initialize a empty repo
git init --bare <directory>
# Initialize a empty repo with tamplate
git init <directory> --template=<template_directory>
# Initilize a remote empity repo
ssh <user>@<host> cd path/repo git init --bare my-project.git

CONFIG

  • Git config:
    • editor
    • ui
    • name
    • email


# Configure commit account
git config --global user.email "your_email@example.com"
git config --global user.name "Your Name"
# Configure git repo account
git config --global github.user myusername
git config --global github.token mytoken
# Git config levels
git config --local <repo>/.gitconfig
git config --global <home>/.gitconfig
git config --system <program>/.gitconfig

add

  • Git add:
    • salva para stage
    • prepara para commit

 

# Git add files to next commit -
# add files that are untracked (git control)
git add <file/directory>
# Git add all files changed that
# are not in .gitignore
git add -a
# Git interative add session
git add -p

commit

  • Git commit:
    • salva grupo de alterações
    • registra uma alteração histórica

 

# Git commit
git commit
# Git with message
git commit -m "message here"
# Git add all files and add message
git commit -a -m "message here"
# Git change last commit
git commit --amend

stash

  • Git stash:
    • salva grupo de alterações
    • registra uma alteração histórica

 

# Git list all stashes and positions
git stash list
# Git add all changed andtracked files 
# to stash and reset branch
git stash 
# Git re-apply last stash
git stash pop 
# Git re-apply a specific stash
git stash pop stash@{2}
# Git re-apply a stash without removing it
git stash apply 
# Git clear all stashes
git stash clear
Command Description
/ search for a hunk by regex
? help
n don't stash this hunk
q quit (any hunks that have already been selected will be stashed)
s split this hunk into smaller hunks
y stash this hunk

fetch

  • Git fetch:
    • Atualização do repo local
    • Não força merge
    • Mais seguro 
# Git update remote connection
git fetch <remote>
# Git update a specific branch
git fetch <remote> <branch>
# Git update entire repo
git fetch --all
# Git update Example
git fetch origin[
# >>>Result 
a1e8fb5..45e66a4 master -> origin/master
a1e8fb5..9e8ab1c develop -> origin/develop
* [new branch] some-feature -> origin/some-feature

pull

  • Git pull:
    • Atualização do repo local
    • força merge
    • menosseguro 
# Git update remote connection
git pull <remote>
# Default behavior
git pull # = git fetch origin HEAD && git merge HEAD
# Git update a specific branch
git pull <remote> <branch>
# Git update without merge commit
git pull --no-commit <remote>
# Git rebase branch
git pull --rebase <remote>
# Git output steps
git pull --verbose
# Git config rebase as default behaviour
git config --global branch.autosetuprebase always

branch

  • Git Branch:
    • nova ramificação
    • visualiza ramificações
# Git list branches
git branch
# Git create branch
git branch <feature>
# Git safe deletion
git branch -d <branch>
# Git unsafe deletion
git branch -D <branch>
# Git rename brnach
git branch -m <branch>
# Git list all branches
git branch -a

checkout

  • Git Checkout:
    • nova ramificação
    • visualiza ramificações
# Git change branch
git checkout <branch>
# Git create and change branch
git checkout -b <branch>
git checkout -b <new-branch> <existing-branch>
# Git reset local branch to last commit
git checkout .
# Git reset a file (remove from "add")
git checkout <path>/<file>

merge

  • Git merge:
    • Combina alterações
    • tipo:
      • 1 merge commit
      • Fast Forward
      • Three way
# Git fast foward | 3 way
git merge <branch>
# Git non fast foward 
git merge --no-ff <branch>

push

Git push:

  • Atualização remota
  • force / all / tags
# Git update remote with commit
git push <remote> <branch>
# Git update remote directly
git push
# Push withou care a local branch
git push <remote> --force
# Push all local branchs
git push <remote> --all
# Push local branch to remote with tag
git push <remote> --tags

rebase

  • Git rebase:
    • troca a base
    • alinha histórico 
    • clean
# Git change base
git rebase <base>
# Git rebase continue solving conflicts
git rebase --continue
# Git abort rebase operation
git rebase --abort
# Git rebase more than one branch
git rebase --onto master featureA featureB

git flow

Um dos fluxos possíveis

Apresenta um plugin para git

benefícios de organização

git workflow

## criação de uma nova branch para desenvolvimento de feature
git fetch
git checkout demanda-sprint3
git pull
git checkout -b "feature-que-vc-vai-desenvolver"
## alterações na branch
git add . # arquivos que serão adicionado e que 
          # (se necessário) já foram excluídos no .gitignore
git commit -a -m "Mensgaem de um comite qua não adicionou novos arquivos"
## alterações mínimas na sua branch
git commit -a -m "Mensgaem de um comite qua não adicionou novos arquivos"
#alterações que não precisam de uma nova branch
git pull --rebase
## alterações na sua branch
git fetch
git checkout "feature-que-vc-esta-desenvolvendo"
git rebase origin/demanda-sprint3
git checkout demanda-sprint3
git merge "feature-que-vc-esta-desenvolvendo" --ff

GIT - Introduction

By Felipe Fonseca Rocha

GIT - Introduction

Git Ingnition presentation for DTI new members. An align of concepts and first steps.

  • 159