前言

记录Git杂项命令合集

删除远程分支

1
$ git push origin --delete [branchname]

删除本地已合并的分支

1
2
$ git branch -d [branchname]
$ git branch -D [branchName]

删除所有本地TAG

1
$ git tag -l | xargs git tag -d

删除远程本地TAG

注意Windows平台可能需要用git-bash执行

1
$ git show-ref --tag | awk '{print ":" $2}' | xargs git push origin

新建空白分支

1
2
3
$ git checkout --orphan newbranch
$ git rm -rf .
$ git commit --allow-empty -m "[empty|rm] initial commit"

强制拉取并覆盖本地代码

1
2
3
$ git fetch --all
$ git reset --hard origin/master
$ git pull

加速克隆及下载镜像

1
2
3
4
5
6
# https://doc.fastgit.org/
# https://gitclone.com/
# https://github.com.cnpmjs.org/
# https://ghproxy.com/
# 免替换
$ git config --global url."https://hub.fastgit.org".insteadOf https://github.com

设置代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 全局项目
$ git config --global http.proxy 127.0.0.1:1080
$ git config --global https.proxy 127.0.0.1:1080
# 单独项目
$ git config --local http.proxy 127.0.0.1:1080
$ git config --local https.proxy 127.0.0.1:1080
# 清除
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy
# 针对 github.com 的单独配置
$ git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
$ git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
$ git config --global --unset http.https://github.com.proxy
$ git config --global --unset https.https://github.com.proxy

查看配置

1
2
3
4
5
6
7
8
# 查看所有配置
$ git config -l
# 查看用户配置
$ git config --global -l
# 查看系统配置
$ git config --system -l
# 查看仓库配置
$ git config --local -l

存储用户名和密码

1
2
3
4
5
# 缓存时间 默认15分钟  --timeout 自定义时间
$ git config --global credential.helper cache
$ git config --global credential.helper 'cache --timeout 3600'
# 永久存储
$ git config --global credential.helper store

提交Commit关联Issue

1
2
3
# 操作 fixes fixed fix closes close closed
$ git commit -m "... {action} #{issue_id}"
$ git commit -m "... fixed #1024"

清空commit记录

1
$ git rebase --root -i

提交大型文件(lfs)

Git LFS 是 Github 开发的一个 Git 的扩展,用于实现 Git 对大文件的支持

安装LFS

Linux
1
2
3
$ curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
$ sudo apt-get install git-lfs
$ git lfs install
Mac(HomeBrew)
1
2
3
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install git-lfs
$ git lfs install
Windows

Windows Installer

1
2
# 下载安装上述链接的文件后执行命令
$ git lfs install

使用

使用前需要git lfs install开启功能

1
2
3
4
5
6
7
8
9
10
11
12
# 大文件追踪
$ git lfs track "your_large_file"
# 会生成大文件夹追踪记录,并添加提交
$ git add .gitattributes
$ git add your_large_file
$ git commit -m "Add large file"
$ git push origin master

# 追踪记录
$ git lfs ls-files
# 可选clone方式
$ git lfs clone

END.