Git

Workshop

Git...

  • 世界上最好的文件管理系统【没有之一】

分布式存储

多分支-多人合作

理解 Git 

空间

Remote

Local

Remote

Local

Upstream

Repository

Local

Repository

...

Remote

Local

Upstream

Repository

Local

Repository

Workspace

...

...

Remote

Local

Upstream

Repository

Local

Repository

Index

(Staging)

Workspace

...

Remote

Local

Upstream

Repository

Local

Repository

Index

(Staging)

Workspace

Stash

分支

git init

本地初始化仓库

git add

添加文件到  staging 区

git commit

 提交文件 到本地 仓库

git status

查看本地文件的状态

$ git status
No ramo feature/nova_feature
Changes not staged for commit:
  (utilize "git add <arquivo>..." para atualizar o que será submetido)
  (utilize "git checkout -- <arquivo>..." para descartar mudanças no diretório de trabalho)

	modificado: teste2.txt

Arquivos não monitorados:
  (utilize "git add <arquivo>..." para incluir o que será submetido)

	.project
	novo_arquivo.txt

nenhuma modificação adicionada à submissão (utilize "git add" e/ou "git commit -a")
$ cat my-project/.gitignore
# Ignore modeulos, demas e vendor
/core
/modules
/themes
/vendor

# Ignora arquivos de configurações
sites/*/settings*.php
sites/*/services*.yml

# Ignora arquivos privados
sites/*/files
sites/*/private

# Ignora todos diretórios, exceto um
/some-dir/*
!/some-dir/dont-ignore-me.txt

.gitignore

排除文件不使用git管理

$ git log
commit 5e31a9080c7f0a9b0390cd7dd2aa8e59a28a7328
Author: Yves Laroche <yves.laroche@logicnow.com>
Date:   Tue Feb 2 20:02:08 2016 +0000

    My first commit

# Short
$ git log --oneline
5e31a90 My first commit

# Limit number of commits
$ git log -10
$ git log --since=1.week
$ git log --after='2016-01-10'

git log

查看Git提交历史

删除与移动

$ git rm my-first-file
rm 'my-first-file'

$ git status
On branch my-new-feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    my-first-file

$ git status
On branch my-new-feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	renamed:    my-first-file -> other-name

Let's Git !!!

> mkdir repo1
> cd repo1
> ls -la
total 8
drwxrwxr-x 2 asp asp 4096 jun  5 13:28 .
drwxrwxr-x 3 asp asp 4096 jun  5 13:28 ..
> git init
Initialized empty Git repository in /home/asp/repositories/repo1/.git/
> ls -la
total 12
drwxrwxr-x 3 asp asp 4096 jun  5 13:28 .
drwxrwxr-x 3 asp asp 4096 jun  5 13:28 ..
drwxrwxr-x 7 asp asp 4096 jun  5 13:28 .git

... e criar um arquivo:

> cat > file.txt
this is a file
^C
> ls -la
total 16
drwxrwxr-x 3 asp asp 4096 jun  5 13:30 .
drwxrwxr-x 3 asp asp 4096 jun  5 13:28 ..
-rw-rw-r-- 1 asp asp   15 jun  5 13:30 file.txt
drwxrwxr-x 7 asp asp 4096 jun  5 13:28 .git
> git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	file.txt

nothing added to commit but untracked files present (use "git add" to track)
> 
> git add file.txt 
> git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   file.txt

> 
> git config user.name "Your name here"
> git config user.email "your@email.here"
> git commit -m "New file file.txt"
[master (root-commit) d87946f] New file file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
> git status
On branch master
nothing to commit, working directory clean
> 
> git log
commit d87946fa2d170113dcbda46d4b095a1a30e852ca
Author: acanimal <fabio.nowaki@bcash.com.br>
Date:   Mon Feb 20 13:38:27 2016 +0200

    New file file.txt
> git log --decorate=full --name-status
commit d87946fa2d170113dcbda46d4b095a1a30e852ca (HEAD, refs/heads/master)
Author: fabio.nowaki <fabio.nowaki@bcash.com.br>
Date:   Mon Feb 20 13:38:27 2016 +0200

    New file file.txt

A       file.txt

git status VS git log

> git status
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:   file.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	other.txt

no changes added to commit (use "git add" and/or "git commit -a")
> git add other.txt 
> git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   other.txt

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:   file.txt
> git commit -am "Added new file other.txt"
[master a709c60] Added new file other.txt
 2 files changed, 2 insertions(+)
 create mode 100644 other.txt
> git status
On branch master
nothing to commit, working directory clean
> git log --decorate=full --name-status
commit a709c60d5c885beeeaf2a7d6fd6f129dbeead216 (HEAD, refs/heads/master)
Author: fabio.nowaki <fabio.nowaki@bcash.com.br>
Date:   Fri Jun 5 15:59:05 2015 +0200

    Added new file other.txt

M       file.txt
A       other.txt

commit d87946fa2d170113dcbda46d4b095a1a30e852ca
Author: fabio.nowaki <fabio.nowaki@bcash.com.br>
Date:   Fri Jun 5 13:38:27 2015 +0200

    New file file.txt

A       file.txt

分支管理

git branch

$ git branch
  develop
* feature/nova_feature
  master
$ git branch 'projeto1'

git checkout

$ git checkout 'projeto1'

 

  > git branch projeto1

  > git checkout projeto1

 

 

 

  > git checkout -b projeto1

 

 

git merge

$ git branch master
$ git merge --no-ff 'projeto1'

Let's Git !!!

> git branch
* master
> git branch bug01
> git branch
  bug01
* master
> git checkout bug01
Switched to branch 'bug01'
> git branch
* bug01
  master
> cat > file_on_bug01.txt
fix the bug
^C
> git status
On branch bug01
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	file_on_bug01.txt

nothing added to commit but untracked files present (use "git add" to track)
> git add file_on_bug01.txt 
> git status
On branch bug01
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   file_on_bug01.txt

> git commit -am "Bug fixed"
[bug01 776d7ee] Bug fixed
 1 file changed, 1 insertion(+)
 create mode 100644 file_on_bug01.txt
> git status
On branch bug01
nothing to commit, working directory clean
> git status
On branch bug01
nothing to commit, working directory clean
> ls -l
total 12
-rw-rw-r-- 1 asp asp 12 jun 11 12:08 file_on_bug01.txt
-rw-rw-r-- 1 asp asp 17 jun  5 15:55 file.txt
-rw-rw-r-- 1 asp asp 21 jun  5 15:54 other.txt
> git checkout master
Switched to branch 'master'
> ls -l
total 8
-rw-rw-r-- 1 asp asp 17 jun  5 15:55 file.txt
-rw-rw-r-- 1 asp asp 21 jun  5 15:54 other.txt
> git merge bug01
Updating a709c60..776d7ee
Fast-forward
 file_on_bug01.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file_on_bug01.txt
> ls -l
total 12
-rw-rw-r-- 1 asp asp 12 jun 11 15:16 file_on_bug01.txt
-rw-rw-r-- 1 asp asp 35 jun 11 13:48 file.txt
-rw-rw-r-- 1 asp asp 21 jun  5 15:54 other.txt

E deletar a branch bug01 

> git branch -d bug01
Deleted branch bug01 (was 776d7ee).
> git checkout master
Switched to branch 'master'
> vi file.txt 
> git commit -am "Added new line on file.txt at master"
[master 3c07391] Added new line on file.txt at master
 1 file changed, 1 insertion(+)
> git checkout -b bug02
Switched to a new branch 'bug02'
> git branch
* bug02
  master
>
> vi file.txt
>
> git commit -am "Modified file.txt on bug02"
[bug02 fa2a1b4] Modified file.txt on bug02
 1 file changed, 1 insertion(+)
> git branch
  bug02
* master
> git merge bug02
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

> more file.txt 
<<<<<<< HEAD
New line at 'master'
=======
New line here at 'bug02'.
>>>>>>> bug02
this is a file
.
> git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:      file.txt

no changes added to commit (use "git add" and/or "git commit -a")
> vi file.txt 
> more file.txt 
New line at 'master'
New line here at 'bug02'.
this is a file

> git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:      file.txt
no changes added to commit (use "git add" and/or "git commit -a")
> git add file.txt 
> git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

nothing to commit, working directory clean
> git commit -am "Merge done"
[master cb2da7c] Merge done
> git status
On branch master
nothing to commit, working directory clean
# Mostrar todas as tags
> git tag

# Criar um tag
> git tag -a 0.1.0 -m 'Release 0.1.0 do projeto'

# Checkout de uma tag específica
> git checkout -b 0.1.0

# Criar branch da tag
> git checkout -b hotfix-0.1.1 0.1.0
Switched to a new branch 'hotfix-0.1.1'

Resumo

Repositórios Remotos

remote

Local

Remote (origin)

Remote

Local

Upstream

Repository

Local

Repository

Index

(Staging)

Workspace

Stash

git clone

$ git clone git@git.bcash.com.br/bcash/api.git

git push

$ git push origin master

git fetch

$ git fetch origin

git pull

$ git pull
$ git fetch origin
$ git merge

git push

$ git push origin master

git remote

$ git remote add origin https://git.bcash.com.br/fabio.nowaki/teste.git

Let's Git !!!

> git remote add origin https://git.bcash.com.br/fabio.nowaki/teste.git
> git remote
origin
> git remote -v
origin	https://git.bcash.com.br/fabio.nowaki/teste.git (fetch)
origin	https://git.bcash.com.br/fabio.nowaki/teste.git (push)
>
> git push origin master
Username for 'https://git.bcash.com.br': fabio.nowaki
Password for 'https://fabio.nowaki@git.bcash.com.br':
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (17/17), 1.45 KiB | 0 bytes/s, done.
Total 17 (delta 3), reused 0 (delta 0)
To https://git.bcash.com.br/fabio.nowaki/teste.git
 * [new branch]      master -> master
> 
> git clone https://git.bcash.com.br/fabio.nowaki/teste.git test
Cloning into 'test'...
Username for 'https://git.bcash.com.br': fabio.nowaki
Password for 'https://fabio.nowaki@git.bcash.com.br': 
remote: Counting objects: 23, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 23 (delta 5), reused 0 (delta 0)
Unpacking objects: 100% (23/23), done.
Checking connectivity... done.
> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/fix01
  remotes/origin/master

> git remote -v
origin	https://git.bcash.com.br/fabio.nowaki/teste.git (fetch)
origin	https://git.bcash.com.br/fabio.nowaki/teste.git (push)

git clone...

> vi file02.txt
> git add file02.txt 
> git commit -m "New file 02"
[master d78e486] New file 02
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file02.txt
> git push
Username for 'https://git.bcash.com.br': fabio.nowaki
Password for 'https://fabio.nowaki@git.bcash.com.br':
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 337 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://git.bcash.com.br/fabio.nowaki/teste.git
   720262a..d78e486  master -> master
> git push origin master
Password for 'https://git.bcash.com.br/': 
To https://git.bcash.com.br/fabio.nowaki/teste.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://git.bcash.com.br/fabio.nowaki/teste.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
> git pull origin master
Password for 'https://git.bcash.com.br': 
From https://git.bcash.com.br/fabio.nowaki/teste.git
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 file02.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file02.txt
> git push origin master
Password for 'https://git.bcash.com.br': 
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 556 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
To https://git.bcash.com.br/fabio.nowaki/teste.git
   d78e486..4c15a4e  master -> master
# Mostrar todas as tags
$ git tag

# Criar um tag
$ git tag -a 0.1.0 -m 'Release 0.1.0 do projeto'

# Tags não são 'pushed' por padrão
$ git push origin 0.1.0

# Push todas tags
$ git push origin --tags

# Checkout de uma tag específica
$ git checkout -b 0.1.0
# Criar branch da tag
> git checkout -b hotfix-0.1.1 0.1.0
Switched to a new branch 'hotfix-0.1.1'

Gitflow

O gitflow workflow...

  • Define um modelo de branch 
  • Atribui regras especificas para diferentes branches e define como e quando ela deve ser interagida.

Gitflow branches

  • master
  • develop
  • feature
  • release
  • hotfix

Branch: master

Branch de release e estável, então nós devemos gerar tags  através da master.

Branch: develop

Atua como branch de integração das novas features.

Branch: feature

  • Cada nova feature deve ter uma própria branch
  • Quando o desenvolvimento da feature finalizar, deve ser integrada de volta para a develop
  • Features nunca devem ser desenvolvidas direto na master

Branch: release

Branch: hotfix

Workflow

Let's Git !!!

master

# Cria novo repositório
$ git init

# Clonar repositório
$ git clone git@git.bcash.com.br:bcash/api.git

git

develop

# Iniciar desenvolvimento na branch
$ git checkout -b develop master

git

# Inicializar flow
$ git flow init

git flow

feature

# Cria a branch nova-feature com base na develop
$ git checkout -b feature/nova-feature

# Faz o merge da nova-feature para a develop
$ git checkout develop
$ git merge --no-ff feature/nova-feature

# Deleta a branch nova-feature
$ git branch -d feature/nova-feature

# Envia as alterações da branch develop para o repositório remoto
$ git push origin develop

git

# Cria a branch e faz o checkout dela
$ git flow feature start nova-feature

# Faz o merge para a master/develop, deleta a branch 
# e faz push para o remoto
$ git flow feature finish nova-feature

git flow

release

# Cria a release branch 2.3.0 com base na develop
$ git checkout -b release/2.3.0 develop

# Faz o merge da branch release 2.3.0 para a master
$ git checkout master
$ git merge --no-ff release/2.3.0

# Gera a tag
$ git tag -a v2.3.0

# Faz o merge da branch release 2.3.0 para a develop
$ git checkout develop
$ git merge --no-ff release/2.3.0

# Deleta a branch 2.3.0
$ git branch -d release/2.3.0

git

# Cria a branch e faz o checkout dela
$ git flow release start 2.3.0

# Faz o merge para a master/develop, deleta a branch 
# e faz push para o remoto
$ git flow release finish 2.3.0

git flow

hotfix

# Cria branch para resolver bug emergencial baseado na master
$ git checkout -b hotfix/2.3.7 master

# Após correção, faz merge da branch hotfix para a master
$ git checkout master
$ git merge --no-ff hotfix/2.3.7

# Gera tag baseado na master
$ git tag -a 2.3.7

# Faz o merge da branch hotfix para a develop
$ git checkout develop
$ git merge --no-ff hotfix/2.3.7

# Deleta a branch hotfix
$ git branch -d hotfix/2.3.7

git

# Cria a branch e faz o checkout dela
$ git flow hotfix start 2.3.7

# Faz o merge para a master/develop, deleta a branch, 
# faz push para o remoto, e gera a tag
$ git flow hotfix finish 2.3.7

git flow

Git-教程

By xfyuan

Git-教程

A short introduction to Git.

  • 634