-
1. Começando
- 1.1 Sobre Controle de Versão
- 1.2 Uma Breve História do Git
- 1.3 O Básico do Git
- 1.4 A Linha de Comando
- 1.5 Instalando o Git
- 1.6 Configuração Inicial do Git
- 1.7 Pedindo Ajuda
- 1.8 Sumário
-
2. Fundamentos de Git
-
3. Git Branching
- 3.1 Branches in a Nutshell
- 3.2 Basic Branching and Merging
- 3.3 Branch Management
- 3.4 Branching Workflows
- 3.5 Remote Branches
- 3.6 Rebasing
- 3.7 Summary
-
4. Git on the Server
- 4.1 The Protocols
- 4.2 Getting Git on a Server
- 4.3 Generating Your SSH Public Key
- 4.4 Setting Up the Server
- 4.5 Git Daemon
- 4.6 Smart HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 Third Party Hosted Options
- 4.10 Summary
-
5. Distributed Git
- 5.1 Distributed Workflows
- 5.2 Contributing to a Project
- 5.3 Maintaining a Project
- 5.4 Summary
-
6. GitHub
- 6.1 Configurando uma conta
- 6.2 Contribuindo em um projeto
- 6.3 Maintaining a Project
- 6.4 Managing an organization
- 6.5 Scripting GitHub
- 6.6 Summary
-
7. Git Tools
- 7.1 Revision Selection
- 7.2 Interactive Staging
- 7.3 Stashing and Cleaning
- 7.4 Signing Your Work
- 7.5 Searching
- 7.6 Rewriting History
- 7.7 Reset Demystified
- 7.8 Advanced Merging
- 7.9 Rerere
- 7.10 Debugging with Git
- 7.11 Submodules
- 7.12 Bundling
- 7.13 Replace
- 7.14 Credential Storage
- 7.15 Summary
-
8. Customizing Git
- 8.1 Git Configuration
- 8.2 Git Attributes
- 8.3 Git Hooks
- 8.4 An Example Git-Enforced Policy
- 8.5 Summary
-
9. Git and Other Systems
- 9.1 Git as a Client
- 9.2 Migrating to Git
- 9.3 Summary
-
10. Git Internals
- 10.1 Plumbing and Porcelain
- 10.2 Git Objects
- 10.3 Git References
- 10.4 Packfiles
- 10.5 The Refspec
- 10.6 Transfer Protocols
- 10.7 Maintenance and Data Recovery
- 10.8 Environment Variables
- 10.9 Summary
-
A1. Appendix A: Git in Other Environments
- A1.1 Graphical Interfaces
- A1.2 Git in Visual Studio
- A1.3 Git in Eclipse
- A1.4 Git in Bash
- A1.5 Git in Zsh
- A1.6 Git in Powershell
- A1.7 Summary
-
A2. Appendix B: Embedding Git in your Applications
- A2.1 Command-line Git
- A2.2 Libgit2
- A2.3 JGit
-
A3. Appendix C: Git Commands
- A3.1 Setup and Config
- A3.2 Getting and Creating Projects
- A3.3 Basic Snapshotting
- A3.4 Branching and Merging
- A3.5 Sharing and Updating Projects
- A3.6 Inspection and Comparison
- A3.7 Debugging
- A3.8 Patching
- A3.9 Email
- A3.10 External Systems
- A3.11 Administration
- A3.12 Plumbing Commands
2.5 Fundamentos de Git - Trabalhando de Forma Remota
Trabalhando de Forma Remota
Para colaborar com qualquer projeto Git, você precisará saber como gerenciar seus repositórios remotos. Repositórios remotos são versões de seu repositório hospedado na Internet ou em uma rede qualquer. Você pode ter vários deles, cada um dos quais geralmente é ou somente leitura ou leitura/escrita. Colaborar com outras pessoas envolve o gerenciamento destes repositórios remotos, fazer pushing(atualizar) e pulling(obter) de dados para e deles quando você precisar compartilhar seu trabalho. Gerenciar repositórios remotos inclui saber como adicioná-los remotamente, remover aqueles que não são mais válidos, gerenciar vários branches(ramos) e definí-los como rastreados ou não e muito mais. Nesta seção, abordaremos algumas destas habilidades de gereciamento remoto.
Exibindo seus repositórios remotos
Para ver quais servidores remotos você configurou, você pode executar o comando git remote
. Ele lista os nomes abreviados de cada repositório remoto manejado que você especificou. Se você clonou seu repositório, você deve pelo menos ver origin(origem) – que é o nome padrão dado pelo Git ao servidor que você clonou:
$ git clone https://github.com/schacon/ticgit
Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done.
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done.
Resolving deltas: 100% (772/772), done.
Checking connectivity... done.
$ cd ticgit
$ git remote
origin
Você também pode especificar -v
, que mostra as URLs que o Git tem armazenado pelo nome abreviado a ser usado para ler ou gravar naquele repositório remoto:
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
Se você tem mais de um repositório remoto, o comando lista todos eles. Por exemplo, um repositório com diversos repositórios remotos para trabalhar com vários colaboradores pode ser algo parecido com isto:
$ cd grit
$ git remote -v
bakkdoor https://github.com/bakkdoor/grit (fetch)
bakkdoor https://github.com/bakkdoor/grit (push)
cho45 https://github.com/cho45/grit (fetch)
cho45 https://github.com/cho45/grit (push)
defunkt https://github.com/defunkt/grit (fetch)
defunkt https://github.com/defunkt/grit (push)
koke git://github.com/koke/grit.git (fetch)
koke git://github.com/koke/grit.git (push)
origin git@github.com:mojombo/grit.git (fetch)
origin git@github.com:mojombo/grit.git (push)
Isto significa que nós podemos obter(pull) contribuições de qualquer um desses usuários muito facilmente. Nós podemos, adicionalmente, ter a permissão de atualizar(push) um ou mais destes, embora não possamos dizer isso nesse caso.
Note que estes repositórios remotos usam uma variedade de protocólos e nós falaremos mais sobre isso em Getting Git on a Server.
Adicionando Repositórios Remotos
Nós mencionamos e demos algumas demonstrações de como o comando clone
implicitamente adiciona a origem(origin
) remota para você. Aqui está como adicionar um novo repositório remoto explicitamente. Para adicionar um novo repositório Git remoto como um nome curto que você pode referenciar facilmente, execute git remote add <shortname> <url>
:
$ git remote
origin
$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb https://github.com/paulboone/ticgit (fetch)
pb https://github.com/paulboone/ticgit (push)
Agora você pode usar a string pb
na linha de comando no lugar de uma URL completa. Por exemplo, se você quiser buscar toda a informação que Paul tem, mas você ainda não tem em seu repositório, você pode executar git fetch pb
:
$ git fetch pb
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
* [new branch] master -> pb/master
* [new branch] ticgit -> pb/ticgit
O master branch(ramo mestre) do Paul agora está acessível localmente como pb/master
– você pode fundí-lo(merge) dentro de uma de suas ramificações(branches) ou você pode checar fora da ramificação local se você quiser inspecioná-lo. (Nós abordaremos o que são ramificações(branches) e como usá-las mais detalhadamente em [ch03-git-branching]. )
Buscando e Obtendo de seus Repositórios Remotos
Como você viu, para obter dados de seus projetos remotos, você pode executar:
$ git fetch [remote-name]
O comando vai até aquele projeto remoto e extrai todos os dados daquele projeto que você ainda não tem. Depois que você faz isso, você deve ter como referência todos as ramificações(branches) daquele repositório remoto, que você pode mesclar(merge) com o atual ou inspecionar a qualquer momento.
Se você clonar um repositório, o comando automaticamente adiciona àquele repositório remoto com o nome origin
. Então, git fetch origin
busca qualquer novo trabalho que tenha sido enviado para aquele servidor desde que você o clonou ou fez a última busca(fetch). É importante notar que o comando git fetch
só baixa os dados para o seu repositório local - ele não é automaticamente mesclado(merge) com nenhum trabalho seu ou modificação que você esteja trabalhando atualmente. Você deve mesclá-los manualmente dentro de seu trabalho quando você estiver pronto.
Se o branch
atual é configurando para rastrear um branch
remoto (veja a próxima seção e [ch03-git-branching] para mais informação), você pode usar o comando git pull
para buscar(fetch) e então mesclar(merge) automaticamente aquele branch
remoto dentro do seu branch
atual. Este pode ser um fluxo de trabalho mais fácil e mais confortável para você, e por padrão, o comando git clone
automaticamente configura a sua master branch
local para rastrear a master branch
remota ou qualquer que seja o nome do branch
padrão no servidor de onde você o clonou. Executar git pull
comumente busca os dados do servidor de onde você originalmente clonou e automaticamente tenta mesclá-lo dentro do código que você está atualmente trabalhando.
Pushing to Your Remotes
When you have your project at a point that you want to share, you have to push it upstream.
The command for this is simple: git push [remote-name] [branch-name]
.
If you want to push your master branch to your origin
server (again, cloning generally sets up both of those names for you automatically), then you can run this to push any commits you’ve done back up to the server:
$ git push origin master
This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to fetch their work first and incorporate it into yours before you’ll be allowed to push. See [ch03-git-branching] for more detailed information on how to push to remote servers.
Inspecting a Remote
If you want to see more information about a particular remote, you can use the git remote show [remote-name]
command.
If you run this command with a particular shortname, such as origin
, you get something like this:
$ git remote show origin
* remote origin
Fetch URL: https://github.com/schacon/ticgit
Push URL: https://github.com/schacon/ticgit
HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
It lists the URL for the remote repository as well as the tracking branch information.
The command helpfully tells you that if you’re on the master branch and you run git pull
, it will automatically merge in the master branch on the remote after it fetches all the remote references.
It also lists all the remote references it has pulled down.
That is a simple example you’re likely to encounter.
When you’re using Git more heavily, however, you may see much more information from git remote show
:
$ git remote show origin
* remote origin
URL: https://github.com/my-org/complex-project
Fetch URL: https://github.com/my-org/complex-project
Push URL: https://github.com/my-org/complex-project
HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
markdown-strip tracked
issue-43 new (next fetch will store in remotes/origin)
issue-45 new (next fetch will store in remotes/origin)
refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev-branch merges with remote dev-branch
master merges with remote master
Local refs configured for 'git push':
dev-branch pushes to dev-branch (up to date)
markdown-strip pushes to markdown-strip (up to date)
master pushes to master (up to date)
This command shows which branch is automatically pushed to when you run git push
while on certain branches.
It also shows you which remote branches on the server you don’t yet have, which remote branches you have that have been removed from the server, and multiple local branches that are able to merge automatically with their remote-tracking branch when you run git pull
.
Removing and Renaming Remotes
You can run git remote rename
to change a remote’s shortname.
For instance, if you want to rename pb
to paul
, you can do so with git remote rename
:
$ git remote rename pb paul
$ git remote
origin
paul
It’s worth mentioning that this changes all your remote-tracking branch names, too.
What used to be referenced at pb/master
is now at paul/master
.
If you want to remove a remote for some reason – you’ve moved the server or are no longer using a particular mirror, or perhaps a contributor isn’t contributing anymore – you can either use git remote remove
or git remote rm
:
$ git remote remove paul
$ git remote
origin