Skip to content

Instantly share code, notes, and snippets.

@kelvinst
Last active February 22, 2022 01:02
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelvinst/0b90e7df870ed9ec9226 to your computer and use it in GitHub Desktop.
Save kelvinst/0b90e7df870ed9ec9226 to your computer and use it in GitHub Desktop.
Limpando os branches que você já fez merge

Limpando os branches que você já fez merge

Olá!

Bem, muitas vezes no dia-a-dia com o git, você abre um branch para a feature, termina, faz merge na master e pronto! Está entregue. O problema é que com isso, seu git fica cheio de branches que são, digamos: INÚTEIS. E claro que com o tempo esses branches se reproduzem como coelhos.

Como fazer para apagar todos esses branches? Você deve ter percebido que apenas um git branch -D meu-branch só apaga o branch no seu computador, e em nenhum remote.

Fácil, é só fazer o código git push origin :meu-branch, que além de ser nem um pouco claro (WTF?), te obriga a fazer isso um a um. Se forem muitos branches, babau!

Mas agora seus problemas acabaram! Chegou o git-sweep! Olha lá o repositório... É um projeto para fazer o delete de todos os branches que já estão merged, ou seja, que não tem nenhum commit ahead.

É um projeto em Python, portanto terá que ter ele instalado na máquina e rodar:

$ pip install git-sweep || easy_install git-sweep

Depois disso é só rodar:

$ git-sweep preview
Fetching from the remote
These branches have been merged into master:

  branch1
  branch2
  branch3
  branch4
  branch5

To delete them, run again with `git-sweep cleanup`

De dentro do seu repositório (claro). Com esse comando ele apenas te deu a lista dos branches merged. Despois disso para limpar é só fazer um:

$ git-sweep cleanup
Fetching from the remote
These branches have been merged into master:

  branch1
  branch2
  branch3
  branch4
  branch5

Delete these branches? (y/n) y
  deleting branch1 (done)
  deleting branch2 (done)
  deleting branch3 (done)
  deleting branch4 (done)
  deleting branch5 (done)

All done!

Tell everyone to run `git fetch --prune` to sync with this remote.
(you don't have to, yours is synced)

Pronto, seu remote está limpinho. 😁

Copy link

ghost commented Nov 29, 2018

pip install git-sweep || easy_install git-sweep
Collecting git-sweep
Downloading https://files.pythonhosted.org/packages/68/71/a4b06ba2cd275a2cc1c1eef46ae655517c6b8b05326e8bf60b89e01f2a59/git-sweep-0.1.1.tar.gz
Collecting GitPython>=0.3.2RC1 (from git-sweep)
Downloading https://files.pythonhosted.org/packages/fe/e5/fafe827507644c32d6dc553a1c435cdf882e0c28918a5bab29f7fbebfb70/GitPython-2.1.11-py2.py3-none-any.whl (448kB)
100% |████████████████████████████████| 450kB 415kB/s
Collecting gitdb2>=2.0.0 (from GitPython>=0.3.2RC1->git-sweep)
Downloading https://files.pythonhosted.org/packages/da/30/a407568aa8d8f25db817cf50121a958722f3fc5f87e3a6fba1f40c0633e3/gitdb2-2.0.5-py2.py3-none-any.whl (62kB)
100% |████████████████████████████████| 71kB 698kB/s
Collecting smmap2>=2.0.0 (from gitdb2>=2.0.0->GitPython>=0.3.2RC1->git-sweep)
Downloading https://files.pythonhosted.org/packages/55/d2/866d45e3a121ee15a1dc013824d58072fd5c7799c9c34d01378eb262ca8f/smmap2-2.0.5-py2.py3-none-any.whl
Building wheels for collected packages: git-sweep
Running setup.py bdist_wheel for git-sweep ... done
Stored in directory: /home/phantom/.cache/pip/wheels/80/7c/88/bdaf52b019ca1d198dddfdb50872a5b6b2d63b86947d8e362d
Successfully built git-sweep
Installing collected packages: smmap2, gitdb2, GitPython, git-sweep
Successfully installed GitPython-2.1.11 git-sweep-0.1.1 gitdb2-2.0.5 smmap2-2.0.5

Instalou tudo certo, mas na hora de rodar o git-sweep preview ele buga.

git-sweep preview
git-sweep: command not found

@iagocsousa
Copy link

Muito bom! Eu estava com algumas branchs muito antigas aqui e queria fazer um limpa nelas, o programa funcionou excelente. Após instalar o git-sweep ele não adiciona automaticamente o caminho dele no Path nas variáveis de ambiente, foi necessário fazer isso manualmente.

@henriquerezende
Copy link

pip install git-sweep || easy_install git-sweep
Collecting git-sweep
Downloading https://files.pythonhosted.org/packages/68/71/a4b06ba2cd275a2cc1c1eef46ae655517c6b8b05326e8bf60b89e01f2a59/git-sweep-0.1.1.tar.gz
Collecting GitPython>=0.3.2RC1 (from git-sweep)
Downloading https://files.pythonhosted.org/packages/fe/e5/fafe827507644c32d6dc553a1c435cdf882e0c28918a5bab29f7fbebfb70/GitPython-2.1.11-py2.py3-none-any.whl (448kB)
100% |████████████████████████████████| 450kB 415kB/s
Collecting gitdb2>=2.0.0 (from GitPython>=0.3.2RC1->git-sweep)
Downloading https://files.pythonhosted.org/packages/da/30/a407568aa8d8f25db817cf50121a958722f3fc5f87e3a6fba1f40c0633e3/gitdb2-2.0.5-py2.py3-none-any.whl (62kB)
100% |████████████████████████████████| 71kB 698kB/s
Collecting smmap2>=2.0.0 (from gitdb2>=2.0.0->GitPython>=0.3.2RC1->git-sweep)
Downloading https://files.pythonhosted.org/packages/55/d2/866d45e3a121ee15a1dc013824d58072fd5c7799c9c34d01378eb262ca8f/smmap2-2.0.5-py2.py3-none-any.whl
Building wheels for collected packages: git-sweep
Running setup.py bdist_wheel for git-sweep ... done
Stored in directory: /home/phantom/.cache/pip/wheels/80/7c/88/bdaf52b019ca1d198dddfdb50872a5b6b2d63b86947d8e362d
Successfully built git-sweep
Installing collected packages: smmap2, gitdb2, GitPython, git-sweep
Successfully installed GitPython-2.1.11 git-sweep-0.1.1 gitdb2-2.0.5 smmap2-2.0.5

Instalou tudo certo, mas na hora de rodar o git-sweep preview ele buga.

git-sweep preview
git-sweep: command not found

Tive o mesmo problema

@iagocsousa
Copy link

pip install git-sweep || easy_install git-sweep
Collecting git-sweep
Downloading https://files.pythonhosted.org/packages/68/71/a4b06ba2cd275a2cc1c1eef46ae655517c6b8b05326e8bf60b89e01f2a59/git-sweep-0.1.1.tar.gz
Collecting GitPython>=0.3.2RC1 (from git-sweep)
Downloading https://files.pythonhosted.org/packages/fe/e5/fafe827507644c32d6dc553a1c435cdf882e0c28918a5bab29f7fbebfb70/GitPython-2.1.11-py2.py3-none-any.whl (448kB)
100% |████████████████████████████████| 450kB 415kB/s
Collecting gitdb2>=2.0.0 (from GitPython>=0.3.2RC1->git-sweep)
Downloading https://files.pythonhosted.org/packages/da/30/a407568aa8d8f25db817cf50121a958722f3fc5f87e3a6fba1f40c0633e3/gitdb2-2.0.5-py2.py3-none-any.whl (62kB)
100% |████████████████████████████████| 71kB 698kB/s
Collecting smmap2>=2.0.0 (from gitdb2>=2.0.0->GitPython>=0.3.2RC1->git-sweep)
Downloading https://files.pythonhosted.org/packages/55/d2/866d45e3a121ee15a1dc013824d58072fd5c7799c9c34d01378eb262ca8f/smmap2-2.0.5-py2.py3-none-any.whl
Building wheels for collected packages: git-sweep
Running setup.py bdist_wheel for git-sweep ... done
Stored in directory: /home/phantom/.cache/pip/wheels/80/7c/88/bdaf52b019ca1d198dddfdb50872a5b6b2d63b86947d8e362d
Successfully built git-sweep
Installing collected packages: smmap2, gitdb2, GitPython, git-sweep
Successfully installed GitPython-2.1.11 git-sweep-0.1.1 gitdb2-2.0.5 smmap2-2.0.5
Instalou tudo certo, mas na hora de rodar o git-sweep preview ele buga.
git-sweep preview
git-sweep: command not found

Tive o mesmo problema

Então, tem que adicionar o caminho do git sweep nas variáveis de ambiente. Encontre a pasta aonde está o git sweep e adicione o caminho dela na variável Path no seu sistema (Windows)

@henriquerezende
Copy link

Consegui. Obrigado!

@diniz-victor
Copy link

Consegui. Obrigado!

@henriquerezende pode colocar o comando?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment