Upload
mauro-george
View
641
Download
0
Embed Size (px)
DESCRIPTION
Apresentada em 30/06/12 no IV FSLDC. Na palestra você verá uma introdução aos sistemas de controle de versão, passando pelos modelos centralizado e distribuído. Um pouco da história do git, a instalação e GUIs para Mac, Linux e Windows. As configurações e o fluxo básico para o dia a dia. Além de alguns comandos e dicas que ajudarão no dia a dia com o git, inclusive para quem já usa o git no seu dia a dia
Citation preview
Git para iniciantes
Git para iniciantes1.1.0
Mauro... quem?
Mauro George
http://maurogeorge.github.comhttp://www.aprenderphp.com.br@maurogeorge
Sistemas de controle de versãoVCS (Version control systems)
Pra que?
Pra que?
Gerenciar diferentes versões de um arquivo
Pra que?
Guardar histórico de alterações de um arquivo
Pra que?
Saber quem fez e o porquê de cada alteração
Vantagens
Vantagens
Backup automático
Vantagens
Controle do histórico
Vantagens
Trabalho em equipe
Vantagens
Marcação e resgate de versões estáveis
Vantagens
Ramificação do projeto
Diferentes modelos
Centralizado
Centralizado
Necessita de um servidor central
Centralizado
O checkout é uma cópia da estrutura dos arquivos do repositório
Centralizado
Somente usuários autorizados podem fazer commits
Centralizado
Distribuído
Distribuído
Cada cópia é um repositório com o histórico completo
Distribuído
Não necessita de acesso a rede ou um servidor central
Distribuído
Commits podem ser feitos offline
Distribuído
Commits podem ser facilmente transferidos entre repositórios
Distribuído
Distribuído
Pesquisa
Utiliza:NenhumSubversionMercurialGitOutro
Pesquisa
Git
Git
Open source
Git
Desenvolvido originalmente por Linus Torvalds, com o objetivo de...
Git
.. manter o Kernel do Linux
Git
“focado em velocidade, efetividade e usabilidade do mundo real em grandes projetos”
Instalação
Instalação Mac
DMGhttp://code.google.com/p/git-osx-installer/downloads/list?can=3
Homebrew (mxcl.github.com/homebrew)$ brew install git
Instalação Linux
apt-get$ sudo apt-get install git-core git-gui git-doc
Instalação Windows
msysgithttp://code.google.com/p/msysgit/downloads/list
* Use Git Bash Only** Checkout Windows-style, commit Unix-style line endings
GUIs
GUIs Gitk para todas as plataformas
GUIs GitX (L) para Mac
http://gitx.laullon.com/ *GitX -> Enable Terminal Usage
Gerenciador de pacotes
GUIs Gitg para Linux
http://code.google.com/p/tortoisegit/
GUIs tortoisegit para Windows
Primeiros passos
Primeiros passos
Configuração$ git config --global user.name "Mauro George"$ git config --global user.email "[email protected]"
Primeiros passos
Criando nosso projeto
$ mkdir project$ cd project$ touch hello_world.php
Primeiros passos
Criando um repositório
$ git init
Primeiros passos
Criando um repositório
$ git init
Output:Initialized empty Git repository in /path/to/my/project/.git/
Primeiros passos
Primeiro commit
$ git add .$ git commit -m "first commit"
Primeiros passos
Primeiro commit
$ git add .$ git commit -m "first commit"
Output:[master (root-commit) bf3bd62] first commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 hello_world.php
Primeiros passos
Primeiro commit
$ git status
Primeiros passos
Primeiro commit
$ git status
Output:# On branch masternothing to commit (working directory clean)
Fluxo básico
Fluxo básico
Estado dos arquivosUntrackedModifiedStagedCommited
Fluxo básico
Estado dos arquivos UntrackedArquivos que o git não conhece
Fluxo básico
Estado dos arquivos Untracked$ touch Readme$ git status
Fluxo básico
Estado dos arquivos Untracked$ touch Readme$ git status
Output:# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## Readmenothing added to commit but untracked files present (use "git add" to track)
Fluxo básico
Estado dos arquivos ModifiedArquivos que o git já conhece e que foram alterados
Fluxo básico
Estado dos arquivos Modified$ vim hello_world.php
<?php echo 'Hello world';?>
Fluxo básico
Estado dos arquivos Modified$ git status
Fluxo básico
Estado dos arquivos Modified
Output:
# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: hello_world.php## Untracked files:# (use "git add <file>..." to include in what will be committed)## Readmeno changes added to commit (use "git add" and/or "git commit -a")
Fluxo básico
Estado dos arquivos StagedArquivos que serão enviados para o repositório
Fluxo básico
Estado dos arquivos Staged$ git add hello_world.php$ git status
Fluxo básico
Estado dos arquivos Staged$ git add hello_world.php$ git status
Output:# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: hello_world.php## Untracked files:# (use "git add <file>..." to include in what will be committed)## Readme
Fluxo básico
Estado dos arquivos CommitedArquivos que já estão no repositório git
Fluxo básico
Estado dos arquivos Commited$ git commit -m "showing hello world"
Fluxo básico
Estado dos arquivos Commited$ git commit -m "showing hello world"
Output:[master 83d672c] showing hello world 1 files changed, 3 insertions(+), 0 deletions(-)
Fluxo básico
Estado dos arquivos Commited$ git status
Fluxo básico
Estado dos arquivos Commited$ git status
Output:# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## Readmenothing added to commit but untracked files present (use "git add" to track)
Dica
Colorindo o output$ git config --global color.ui true
Fluxo básico
Resumo
Editar os arquivos Vim / emacs / textmate / etc
Colocar os arquivos em staged git add nome do arquivo
Verificar as mudanças git status
Fazer o commit das mudançasgit commit -m “mensagem do commit”
Dica
Sempre leia as mensagens do Git
Output:
# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: hello_world.php## Untracked files:# (use "git add <file>..." to include in what will be committed)## Readme
Comandos
Comandos
git diffVisualiza o que foi modificado
Comandos
git diff$ vim hello_world.php
<?php echo 'Hello world, no IV FSLDC';?>
Comandos
git diff$ git diff
Comandos
git diff$ git diff
Output:diff --git a/hello_world.php b/hello_world.phpindex fe71dc6..04beafa 100644--- a/hello_world.php+++ b/hello_world.php@@ -1,3 +1,3 @@ <?php- echo 'Hello world';+ echo 'Hello world, no IV FSLDC'; ?>\ No newline at end of file
Comandos
git diff --word-diffVisualiza o que foi modificado, diferenciando por palavras
Comandos
git diff --word-diff$ git diff --word-diff
Comandos
git diff --word-diff$ git diff --word-diff
Output:diff --git a/hello_world.php b/hello_world.phpindex fe71dc6..04beafa 100644--- a/hello_world.php+++ b/hello_world.php@@ -1,3 +1,3 @@<?php echo 'Hello [-world';-]{+world, no IV FSLDC';+}?> No newline at end of file
Comandos
git diff --cachedVisualiza o que foi modificado e está staged
Comandos
git diff --cached$ git add hello_world.php$ git status
Comandos
git diff --cached$ git add hello_world.php$ git status
Output:# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: hello_world.php## Untracked files:# (use "git add <file>..." to include in what will be committed)## Readme
Comandos
git diff --cached$ git diff
Comandos
git diff --cached$ git diff
Output:
Comandos
git diff --cached$ git diff --cached
Comandos
git diff --cached$ git diff --cached
Output:diff --git a/hello_world.php b/hello_world.phpindex fe71dc6..04beafa 100644--- a/hello_world.php+++ b/hello_world.php@@ -1,3 +1,3 @@ <?php- echo 'Hello world';+ echo 'Hello world, no IV FSLDC'; ?>\ No newline at end of file
Comandos
Commitando nossas alterações para continuarmos...$ git commit -m "change hello world message"
Comandos
git commit -am Envia de modified para commited diretamente
Comandos
git commit -am $ vim hello_world.php
<?php echo 'Hello world, no IV FSLDC em 30/06/2012';?>
Comandos
git commit -am $ git status
Comandos
git commit -am $ git status
Output:# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: hello_world.php## Untracked files:# (use "git add <file>..." to include in what will be committed)## Readmeno changes added to commit (use "git add" and/or "git commit -a")
Comandos
git commit -am $ git commit -am "added date on hello world"
Comandos
git commit -am $ git commit -am "added date on hello world"
Output:[master 518bad1] added date on hello world 1 files changed, 1 insertions(+), 1 deletions(-)
Comandos
git commit -am $ git status
Comandos
git commit -am $ git status
Output:# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## Readmenothing added to commit but untracked files present (use "git add" to track)
Dica
Se o git não conhece ele não mexe
Comandos
Editando e commitando o Readme para continuarmos...$ vim Readme$ git add Readme$ git commit -m "added readme"
Comandos
git logExibe o histórico com infomações dos commits
Comandos
git log$ git log
Comandos
git log$ git log
Output:commit a6aa0cb4d832a3a7c3a26001d89d8698e73790bbAuthor: Mauro George <[email protected]>Date: Fri Jan 20 15:59:33 2012 -0200
added readme
commit 518bad17f1bc064f4d542683925ae9f1003609bdAuthor: Mauro George <[email protected]>Date: Fri Jan 20 15:27:07 2012 -0200
added date on hello world
Comandos
git log --onelineExibe o log em apenas uma linha
Comandos
git log --oneline$ git log --oneline
Comandos
git log --oneline$ git log --oneline
Output:53bc92d Revert "added date on hello world"a6aa0cb added readme518bad1 added date on hello world0e1b665 change hello world message83d672c showing hello worldbf3bd62 first commit
Voltando no tempo
Voltando no tempo
git checkout <hash>Volta para o commit informado em <hash>
Voltando no tempo
git checkout <hash>$ git checkout bf3bd62
$ git checkout bf3bd62
Voltando no tempo
git checkout <hash> Hash do primeiro commit
$ git checkout bf3bd62
Voltando no tempo
git checkout <hash> Hash do primeiro commit
Output:Note: checking out 'bf3bd62'.
You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at bf3bd62... first commit
Voltando no tempo
git checkout <hash>$ lshello_world.php$ cat hello_world.php
Voltando no tempo
git checkout <hash> De volta para o futuro $ git checkout master
Voltando no tempo
git checkout <hash> De volta para o futuro $ git checkout master
Output:Previous HEAD position was bf3bd62... first commitSwitched to branch 'master'
Voltando no tempo
git checkout <hash> De volta para o futuro$ lsReadme hello_world.php$ cat hello_world.php<?php echo 'Hello world, no IV FSLDC em 30/06/2012';?>
Voltando no tempo
git revert <hash>Reverte um commit criando um novo
$ git revert 518bad1
Voltando no tempo
git revert <hash>
Voltando no tempo
git revert <hash>$ git revert 518bad1
Hash do quarto commit.
Em que foi adicionada a data
Voltando no tempo
git revert <hash>$ git revert 518bad1
Hash do quarto commit.
Em que foi adicionada a data
Output:[master 53bc92d] Revert "added date on hello world" 1 files changed, 1 insertions(+), 1 deletions(-)
Voltando no tempo
git revert <hash>$ git revert 518bad1
Hash do quarto commit.
Em que foi adicionada a data
Output:[master 53bc92d] Revert "added date on hello world" 1 files changed, 1 insertions(+), 1 deletions(-)
* Será solicitado para digitar uma mensagem do commit, mantenha a padrão por convenção
$ cat hello_world.php<?php echo 'Hello world, no IV FSLDC';?>
Voltando no tempo
git revert <hash>
Dica
Commits pequenos e constantes
Created model of User
Boa mensagem de commit
Created model of User, Post and Comments and added template files
Má mensagem de commit
Voltando no tempo
git checkout <hash> <arquivo>Recupera o arquivo do commit informado
Voltando no tempo
git checkout <hash> <arquivo>$ git checkout 83d672c hello_world.php
Voltando no tempo
git checkout <hash> <arquivo>$ git checkout 83d672c hello_world.php
Hash do segundo commit.Em que foi criada a mensagem
Voltando no tempo
git checkout <hash> <arquivo>$ git status
Voltando no tempo
git checkout <hash> <arquivo>$ git status
Output:# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: hello_world.php#
Voltando no tempo
git checkout <hash> <arquivo>$ git diff --cached
Voltando no tempo
git checkout <hash> <arquivo>$ git diff --cached
Output:diff --git a/hello_world.php b/hello_world.phpindex 04beafa..fe71dc6 100644--- a/hello_world.php+++ b/hello_world.php@@ -1,3 +1,3 @@ <?php- echo 'Hello world, no IV FSLDC';+ echo 'Hello world'; ?>\ No newline at end of file
Voltando no tempo
git checkout .Recupera todos os arquivos sobreescrevendo as mudanças locais
Voltando no tempo
git checkout .$ vim hello_world.php$ vim Readme
Voltando no tempo
git checkout .$ vim hello_world.php$ vim Readme
Voltando no tempo
git checkout .$ git status
Voltando no tempo
git checkout .$ git status
Output:# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: Readme# modified: hello_world.php#no changes added to commit (use "git add" and/or "git commit -a")
Voltando no tempo
git checkout .$ git checkout .
Voltando no tempo
git checkout .$ git status
Voltando no tempo
git checkout .$ git status
Output:# On branch masternothing to commit (working directory clean)
Perguntas?
Obrigado!@maurogeorge
maurogeorge.github.com