Git常用命令

121 阅读2分钟

仓库管理

添加或指定远程仓库地址

方式一:

git remote set-url origin "http://XXX/test.git"
git config remote.origin.url "http://XXX/test.git"

方式二:修改配置.git/config

[remote "origin"]
	url = http://XXX/test.git
	fetch = +refs/heads/*:refs/remotes/origin/*

删除远程仓库

git remote rm origin

分支操作

新建分支推送远程仓库

git push -u origin feature_test
git push --set-upstream origin feature_test

删除本地分支

git branch -d feature_test1 feature_test2

git branch -D feature_test1 feature_test2

一次删除多个分支时,空格分隔

删除远程仓库分支

git push -d origin feature_test1 feature_test2

一次删除多个分支时,空格分隔

清理本地缓存远程仓库无效分支

# 查看是否存在无效分支
git remote prune origin

# 清理无效分支
git remote prune origin -n

分支名重命名

git branch -m feature_test1 feature_test_rename

IDEA使用Git bash

windows中IDEA Terminal配置shell为git的bash

settings -> Tools -> Terminal

Shell path 选择Git安装目录下/bin/bash.exe

IDEA git bash中文乱码问题

Git 安装目录/etc/bash.bashrc添加

# 解决IDEA下的terminal中文Unicode编码问题
export LANG="zh_CN.UTF-8"
export LC_ALL="zh_CN.UTF-8"

统计类

git提交代码统计

git log --since=2023-01-09 --until=2023-01-19 --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 + $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'