원격 저장소에서 로컬 Git 브랜치를 삭제 한 후 삭제
로컬 및 원격 리포지토리를 항상 브랜치 측면에서 동기화하고 싶습니다.
GitHub에 대한 풀 요청 검토 후 분기를 병합하고 제거합니다 (원격). 로컬 리포지토리에서이 정보를 가져 와서 Git이 로컬 버전의 브랜치를 제거하도록하려면 어떻게해야합니까?
빠른 길
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
주의 :에 있지 않으면 master
분기를 삭제할 가능성이 있습니다. "더 나은 방법"을 계속 읽으십시오.
우리는 주인을 유지해야합니다
당신을 확인 할 수 있습니다 master
, 또는 그 문제에 대한 다른 지점에 의해 제거되지 않는 grep
이상 보내고. 이 경우 당신은 갈 것입니다 :
git branch --merged | grep -v "\*" | grep -v "YOUR_BRANCH_TO_KEEP" | xargs -n 1 git branch -d
그래서 만약 우리가 유지하고 싶어 master
, develop
그리고 staging
예를 들어, 우리는 갈 것입니다 :
git branch --merged | grep -v "\*" | grep -Ev "(\*|master|develop|staging)" | xargs -n 1 git branch -d
이것을 별명으로 만드십시오
약간 길기 때문에 .zshrc
또는에 별칭을 추가 할 수 있습니다 .bashrc
. 내 이름은 gbpurge
( git branches purge
)입니다.
alias gbpurge='git branch --merged | grep -Ev "(\*|master|develop|staging)" | xargs -n 1 git branch -d'
그런 다음 다시로드하여 .bashrc
나 .zshrc
:
. ~/.bashrc
또는
. ~/.zshrc
나는 GitHub와 동일한 흐름을 사용하고 git branch --merged
병합 된 분기 목록으로 저를 만족시키는 이전 답변을 찾지 못했습니다 .하지만 모든 경우 원격으로 제거되지 않았습니다. 그래서 이것은 나를 위해 일했습니다 :
git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print $1 }' | xargs -n 1 git branch -d
어디:
git fetch --all -p
: 로컬 지점 상태 업데이트git branch -vv
: 현지 지사 상태 표시grep ": gone]"
: 삭제 된 필터awk '{ print $1 }'
: 그들의 이름을 추출xargs -n 1 git branch -d
: 이름을 삭제 명령에 전달
참고 : 원하는 경우 -d 대신 -D를 사용하면 삭제가 적용됩니다.
예를 들면 다음과 같습니다.
someUsr@someHost:~/repo$ git branch -a
basic-testing
integration-for-tests
* master
origin
playground-for-tests
test-services
remotes/origin/HEAD -> origin/master
remotes/origin/basic-testing
remotes/origin/master
remotes/origin/test-services
someUsr@someHost:~/repo$ git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print $1 }' | xargs -n 1 git branch -d
Fetching origin
Deleted branch integration-for-tests (was fbc609a).
Deleted branch playground-for-tests (was 584b900).
someUsr@someHost:~/repo$ git branch -a
basic-testing
* master
origin
test-services
remotes/origin/HEAD -> origin/master
remotes/origin/basic-testing
remotes/origin/master
remotes/origin/test-services
참고:
http://git-scm.com/book/en/v2/Git-Branching-Remote-Branches
시험:
자식 풀-정리
해당 원격 지점이 삭제되면 로컬 지점을 삭제합니다.
업데이트 :
The statement above is not that correct.
In fact, running git pull --prune
will only REMOVE the remote-tracking branches such like
remotes/origin/fff remotes/origin/dev remotes/origin/master
Then, you can run git branch -r
to check the remote-tracking branches left on your machine. Suppose the left branches are:
origin/dev origin/master
which means the branch origin/fff
is deleted.
So, after running git pull --prune
, just run:
git branch --merged | grep -vFf <(git branch -r | cut -d'/' -f2-)
you can find out all the local branches which:
- have no correspoding remote branches any more;
- can be removed safely.
then, <the command above> | xargs git branch -d
can delete all of them.
This should work to avoid deleting the master and development branches with the accepted solution:
git branch --merged | egrep -v "^\*|master|development" | xargs -n 1 git branch -d
For people using powershell, this is the equivalent to the answer above:
git branch -vv | Select-String -Pattern ': gone]' | ForEach-Object{($_ -split "\s+")[1]} | %{ git branch -D $_ }
- Filter all the branches that are marked as gone
- Call
git branch -D
on each of the found branches
Very simple solution: remove your local repo and clone the remote one anew. May not seem very elegant, but it is simple and you'll understand exactly what you're doing without reading man pages :-).
None of this was working for me. You can see my other answer here: https://stackoverflow.com/a/34969726/550454
But essentially, I now have this in my ~/.gitconfig
:
[alias]
prune-branches = !git remote prune origin && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -d
I just do that to remove merged local branches:
git branch -d $(git branch --merged)
and in case you want to remove inexistent trackings too:
git pull --prune
I've written this one-liner to list all local branches which do not have corresponding remote branch:
diff -u <(git branch|sed 's/..//') <(git branch -r|sed 's/..origin\///')|tail -n +4|sed -n "s/^-//p" -
After this done, deleting these local branches is easy with xargs
:
diff -u <(git branch|sed 's/..//') <(git branch -r|sed 's/..origin\///')|tail -n +4|sed -n "s/^-//p" -|xargs -r git branch -d
In the event that you've just pushed and merged your branch to master, then do the following in git bash:
git branch -d branch_name_to_delete
If you're currently in that branch it will push you back to master. At this point do a pull with
git pull
The voted answer does have the potential to delete master. Consdier the below practical example.
I had two feature branches hemen_README and hemen_BASEBOX which were merged into develop, and then develop was merged into master. The feature branches hemen_README and hemen_BASEBOX were deleted remotely but were still showing up locally. Also i am not on master locally, but on develop.
In that case
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git branch -v -a
* develop 671ad6c Merged in hemen_README (pull request #1)
hemen_BASEBOX a535c0f added global exec paths to puppet manifest
hemen_README ba87489 Updated Readme with considerable details
master 8980894 [behind 7] Initial Vagrantfile, works for vagrant up. Also initial .gitignore
remotes/origin/develop 671ad6c Merged in hemen_README (pull request #1)
remotes/origin/hemen_BASEBOX a535c0f added global exec paths to puppet manifest
remotes/origin/hemen_README ba87489 Updated Readme with considerable details
remotes/origin/master 2f093ce Merged in develop (pull request #3)
So if i run the above partial command
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git branch --merged | grep -v "\*"
hemen_BASEBOX
hemen_README
master
Notice that it shows master too, which will eventually be deleted.
In any case I was able to do it. I am sharing my session log with you on how I achieved that.
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git remote prune origin --dry-run
Pruning origin
URL: git@bitbucket.org:hemenkapadiapublic/vagrant-webdev.git
* [would prune] origin/hemen_BASEBOX
* [would prune] origin/hemen_README
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git remote prune origin
Pruning origin
URL: git@bitbucket.org:hemenkapadiapublic/vagrant-webdev.git
* [pruned] origin/hemen_BASEBOX
* [pruned] origin/hemen_README
I just checked whay will be pruned and then pruned it. looking at branch command below we have taken care of remotes
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git branch -v -a
* develop 671ad6c Merged in hemen_README (pull request #1)
hemen_BASEBOX a535c0f added global exec paths to puppet manifest
hemen_README ba87489 Updated Readme with considerable details
master 8980894 [behind 7] Initial Vagrantfile, works for vagrant up. Also initial .gitignore
remotes/origin/develop 671ad6c Merged in hemen_README (pull request #1)
remotes/origin/master 2f093ce Merged in develop (pull request #3)
Now go ahead and delete local branches
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git branch -d hemen_BASEBOX
Deleted branch hemen_BASEBOX (was a535c0f).
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git branch -d hemen_README
Deleted branch hemen_README (was ba87489).
Good now the branches are as desired.
hemen@hemen-MXC061:~/data/projects/vagrant-webdev$ git branch -v -a
* develop 671ad6c Merged in hemen_README (pull request #1)
master 8980894 [behind 7] Initial Vagrantfile, works for vagrant up. Also initial .gitignore
remotes/origin/develop 671ad6c Merged in hemen_README (pull request #1)
remotes/origin/master 2f093ce Merged in develop (pull request #3)
'IT' 카테고리의 다른 글
데이터 프레임에서 숫자 열만 선택 (0) | 2020.06.04 |
---|---|
Oracle에서 시퀀스를 어떻게 재설정합니까? (0) | 2020.06.04 |
부트 스트랩 모달 오픈에서 함수 호출 (0) | 2020.06.04 |
Passport.js-오류 : 사용자를 세션으로 직렬화하지 못했습니다. (0) | 2020.06.04 |
Rails 3 데이터 타입? (0) | 2020.06.04 |