git 删除 本地分支和远程分支 出现的问题

885 阅读3分钟

将分支合并到 master 后,我们需要删除无用分支,本地删除比较简单:

git branch -d/-D xxx

但是,有些分支,我们可能也是多人开发,推送到了远程测试服务器上,我们也需要删除远程的废弃分支:

1.先在本地删除分支
	git branch -d xxx

2.推送给远程仓库
	git push test :xxx		// 切记这个语法。
/*
    如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放稳定代码的地方),可以用这个非常无厘头的语法来删除它:
    	git push  [远程名] :[分支名]
	如果想在服务器上删除 serverfix 分支,运行下面的命令:
		git push origin :serverfix
	会输出:
		To git@github.com:schacon/simplegit.git
		- [deleted] serverfix
    咚!服务器上的分支没了。你最好特别留心这一页,因为你一定会用到那个命令,而且你很可能会忘掉它的语法。有种方便记忆这条命令的方法:
    	记住我们不久前见过的 git push [远程名] [本地分支]:[远程分支] 语法,如果省略 [本地分支],那就等于是在说“在这里提取空白然后把它变成[远程分支]”。
 */

/*
	我执行删除的时候,报了如下错误:
		remote: error: By default, deleting the current branch is denied, because the next
		remote: error: 'git clone' won't result in any file checked out, causing confusion.
		remote: error:
		remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
		remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
		remote: error: current branch, with or without a warning message.
		remote: error:
		remote: error: To squelch this message, you can set it to 'refuse'.
		remote: error: refusing to delete the current branch: refs/heads/LFF
		To xxx.xxx.xxx.xxx:yyy.git
		 ! [remote rejected] LFF (deletion of the current branch prohibited)

	看着错误提示,应该是需要我们在远程 git 上设置 'receive.denyDeleteCurrent' 为 'warn' 或 'ignore' 来开启删除

	// git 进行全局设置
	git config --global receive.denyDeleteCurrent warn	

	// 再次删除远程分支
	git push test :xxx

	删除成功!!!

	又出现了一个问题,我这样操作没有问题了,本地和远程的分支都被删除了,但是不知道为啥,同事的 sourcetree 里,一直会展示一个 'refs/remotes/origin/xxx',看着像是本地对远程的引用:
		参考文章:
			https://www.jianshu.com/p/884ff6252be5

		执行下面命令:
			git remote prune origin

	对 git 不熟悉,好多东西不了解!
 */


/*
	这里再记录下,git 添加配置的一个笔记,只能说明对 git 太不熟悉了:
		上面想添加 git 全局配置,想着查找下 git 的配置文件,完全不知道哪里有...不知道 linux 下 git 的配置目录在哪里,通过命令:
			find / -name '[A-Za-z]*git[A-Za-z]*'		// 全局搜索,也没找到

		// 1.find 命令应该都是使用的 glob 通配符(这里也忘记的差不多了...上面的 glob 写的不太对)
		// 2.find 也支持正则,可能是 perl 正则
			find / -regex '.*git.*'

		而且 git config -l 查看所有的 git 配置,居然啥也没,导致我一度怀疑是不是用错了~~

		在网上搜索了下 linux 的配置文件路径:
			1./etc/gitconfig
			2.~/.gitconfig
			3.不同项目下的 .git/config

		git 官方文档也有说明,我居然没有去了解...
			https://git-scm.com/book/zh/v1/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-%E9%85%8D%E7%BD%AE-Git

		我是自己测出来的,输入 git config,会输出 git config 的用法:

			配置文件位置
			    --global              使用全局配置文件
			    --system              使用系统级配置文件
			    --local               使用版本库级配置文件
			    -f, --file <文件>     使用指定的配置文件

		我们一般使用的全局配置时,会添加 '--global' 参数,然后就依次尝试了下,设置 user.name 这个配置项:
			git config --global user.name dongxuemin		// 在 ~/ 下创建了 .gitconfig
			git config --system user.name dongxuemin		// 在 /etc/ 下创建了 gitconfig

			git config --local 我没测试


		还是得好好熟悉 git 啊,不过慢慢发现问题,寻找问题,就熟悉了
 */