DEV Community

ダニエリ for WoMakersCode

Posted on • Edited on

26 1 1

[Tutorial Git] git branch: O que são branches (ramos) no Git?

De maneira simplificada, os ramos (branches) no Git são semelhantes a um ramo de uma árvore, onde o tronco seria a base do código. Desse modo é possível criar diversos ramos e fazer alterações, enquanto a base permanece intacta. Por padrão o ramo principal é denominado de main (master, na versão antiga).

ramos no git

Criando branch

Para criar um branch, digite



$ git branch <nome_do_ramo>


Enter fullscreen mode Exit fullscreen mode
  • $ indica que você deve usar o usuário comum para fazer essa operação.
  • escolha um nome para o seu ramo sem os sinais < e >.

Para ir até o branch criado



$ git checkout <nome_do_ramo>


Enter fullscreen mode Exit fullscreen mode

Para usar um atalho para esses dois comandos acima (criar o novo ramo e ser imediatamente transferida para ele)



$ git checkout -b <nome_do_ramo>


Enter fullscreen mode Exit fullscreen mode

Desse modo o branch será criado e em seguida irá transferir você para lá.

Listar branch

Para listar todos os branches, usa-se o comando:



$ git branch -a


Enter fullscreen mode Exit fullscreen mode

Para saber em qual branch você está, digite:



$ git branch


Enter fullscreen mode Exit fullscreen mode

Excluindo branch

Para excluir um branch, digite



$ git branch -d <nome_do_ramo>


Enter fullscreen mode Exit fullscreen mode

Excluindo branch remoto



$ git push origin --delete <nome_do_ramo>


Enter fullscreen mode Exit fullscreen mode

Unindo branch

Para unir todas as modificações que foram feitas em diferentes branches, ao ramo principal do projeto, digite



$ git checkout main


Enter fullscreen mode Exit fullscreen mode

para ir até o branch principal ou



$ git checkout <nome_do_ramo>


Enter fullscreen mode Exit fullscreen mode

para ir até o branch de destino das alterações.

Então digite



$ git merge <nome_do_ramo_onde_as_alterações_foram_feitas>


Enter fullscreen mode Exit fullscreen mode

O fluxo dos branches será algo como a figura abaixo (unindo branch 1 a main).

merge

Para visualizar os commits de merge:



 $ git log --merges 


Enter fullscreen mode Exit fullscreen mode

Resolvendo conflitos ao unir os ramos no git

Supondo que temos um arquivo na branch main com o seguinte código



def soma(x,y):
    z = x+y
    print(x,"+",y,"=",z)

print('SOMA DOIS NÚMEROS')
num1 = float(input("Digite o primeiro número: "))
num2 = float(input("Digite o segundo número: "))
soma(num1,num2)


Enter fullscreen mode Exit fullscreen mode

E o mesmo arquivo na branch teste, com o código:



def somar(a,b):
    c = a+b
    return c

print('SOMA DOIS NÚMEROS')
numero1 = float(input("Digite o primeiro número: "))
numero2 = float(input("Digite o segundo número: "))
resultado = somar(numero1,numero2)
print(f'Soma: {resultado}')


Enter fullscreen mode Exit fullscreen mode

Ao tentar fazer o merge o git anunciará o conflito:



Mesclagem automática de <arquivo>
CONFLITO (conteúdo): conflito de mesclagem em <arquivo>
Automatic merge failed; fix conflicts and then commit the result.


Enter fullscreen mode Exit fullscreen mode

Desse modo, é preciso primeiro resolver a situação conflitante (escolhendo as partes do código que deseja manter) e em seguida fazer o commit. Só após isso a união dos branches (ramos) será concluída.

Caso deseje desfazer esse processo, digite:



$ git merge --abort


Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (1)

Collapse
 
broona-dantas profile image
broona-dantas

Muito bem explicado e ilustrado.
Obrigada!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay