Remove local git tags that are no longer on the remote repository
We use tags in git as part of our deployment process. From time to time, we want to clean up these tags by removing them from our remote repository.
This is pretty straightforward. One user deletes the local tag and the remote tag in one set of commands. We have a little shell script that combines both steps.
The 2nd (3rd, 4th,...) user now has local tags that are no longer reflected on the remote.
I am looking for a command similar to git remote prune origin
which cleans up locally tracking branches for which the remote branch has been deleted.
Alternatively, a simple command to list remote tags could be used to compare to the local tags returned via git tag -l
.
Good question. :) I don't have a complete answer...
That said, you can get a list of remote tags via git ls-remote
. To list the tags in the repository referenced by origin
, you'd run:
git ls-remote --tags origin
That returns a list of hashes and friendly tag names, like:
94bf6de8315d9a7b22385e86e1f5add9183bcb3c refs/tags/v0.1.3
cc047da6604bdd9a0e5ecbba3375ba6f09eed09d refs/tags/v0.1.4
...
2f2e45bedf67dedb8d1dc0d02612345ee5c893f2 refs/tags/v0.5.4
You could certainly put together a bash script to compare the tags generated by this list with the tags you have locally. Take a look at git show-ref --tags
, which generates the tag names in the same form as git ls-remote
).
As an aside, git show-ref
has an option that does the opposite of what you'd like. The following command would list all the tags on the remote branch that you don't have locally:
git ls-remote --tags origin | git show-ref --tags --exclude-existing
This is great question, I'd been wondering the same thing.
I didn't want to write a script so sought a different solution. The key is discovering that you can delete a tag locally, then use git fetch to "get it back" from the remote server. If the tag doesn't exist on the remote, then it will remain deleted.
Thus you need to type two lines in order:
git tag -l | xargs git tag -d
git fetch --tags
These:
Delete all tags from the local repo. FWIW, xargs places each tag output by "tag -l" onto the command line for "tag -d". Without this, git won't delete anything because it doesn't read stdin (silly git).
Fetch all active tags from the remote repo.
This even works a treat on Windows.
From Git v1.7.8 to v1.8.5.6, you can use this:
git fetch <remote> --prune --tags
Update
This doesn't work on newer versions of git (starting with v1.9.0) because of commit e66ef7ae6f31f2. I don't really want to delete it though since it did work for some people.
As suggested by "Chad Juliano", with all Git version since v1.7.8, you can use the following command:
git fetch --prune <remote> +refs/tags/*:refs/tags/*
You may need to enclose the tags part with quotes (on Windows for example) to avoid wildcard expansion:
git fetch --prune <remote> "+refs/tags/*:refs/tags/*"
链接地址: http://www.djcxy.com/p/92400.html
上一篇: 子模块内部的Git子模块(嵌套子模块)
下一篇: 删除远程存储库上不再存在的本地git标签