深色模式
Git 的配置与使用
Git配置
Git配置的3个级别
使用git config
命令时,可以指定配置的级别,不同级别有不同的作用范围。
项目级
配置文件:
.git/config
,选项是--local
,这是默认选项。此配置仅作用于当前项目,优先级最高。
用户级
配置文件:
~/.gitconfig
或~/.config/git/config
,选项是--global
。可作用于所有项目,全局配置。
系统级
配置文件:
/etc/gitconfig
,选项是--system
。一般不在系统级修改。
查看配置
bash
# 查看所有配置
git config --list
# -l是--list的简写
git config -l
# 显示配置的文件位置
git config --list --show-origin
# 仅查看 local 级
git config --list --local
# 仅查看 global 级
git config --list --global
# 仅查看 system 级
git config --list --system
写入配置
直接指定<key> <value>
:
bash
git config --global user.name "John"
删除配置
使用--unset
选项:
sh
git config --global --unset user.name
Git常用配置
sh
# 用户名
git config --global user.name "John Doe"
# 邮箱
git config --global user.email johndoe@example.com
# git acm => 将所有修改添加到索引,并提交
git config --global alias.acm "! git add . && git commit -m"
# git s => git status
git config --global alias.s "git status"
Git代理
全局设置代理:
sh
# 使用http代理
git config --global http.proxy http://127.0.0.1:58591
git config --global https.proxy https://127.0.0.1:58591
# 使用socks5代理
git config --global http.proxy socks5://127.0.0.1:51837
git config --global https.proxy socks5://127.0.0.1:51837
仅访问GitHub设置代理:
sh
# 使用socks5代理(推荐)
git config --global http.https://github.com.proxy socks5://127.0.0.1:51837
# 使用http代理(不推荐)
git config --global http.https://github.com.proxy http://127.0.0.1:58591
临时使用代理:
sh
git -c https.proxy=https://127.0.0.1:58591 pull
git -c https.proxy=socks5://127.0.0.1:51837 pull
参考:https://zhuanlan.zhihu.com/p/481574024
git 删除分支
- 本地分支,安全删除
sh
git branch -d dev
- 本地分支,强制删除
sh
git branch -D dev
- 远程分支
sh
git push origin --delete dev
- 本地分支,清理
sh
git fetch -p
# or
# git fetch --prune
git push 到非空远程仓库
当git push
到远程仓库时,如果远程仓库非空,比如远程仓库已经有一个README.md
文件了,那么就会push
失败。
解决办法是,先pull
远程仓库内容,再push
。
在pull
的时候,会提示冲突,可使用--allow-unrelated-histories
选项解决冲突:
bash
git pull origin master --allow-unrelated-histories
然后push
:
bash
git push -u origin master
git stash
常用stash操作:
git stash list
:查看列表。git stash push -m
:创建一条stash。git stash pop <stash>
:应用一条stash,并删除它。git stash apply <stash>
:应用一条stash,不删除它。git stash drop <stash>
:删除一条stash。git stash clear
:清除所有stash。git stash show <stash>
:查看一条stash。git stash branch <stash>
:从创建该条stash的commit处,新建分支,并切换到这个分支,然后pop这个stash。
git commit 追加提交
修改提交信息
shgit commit -m "Fix login bug" # 发现提交信息有误 git commit --amend -m "Fix login UI crash"
追加内容,不改提交信息
使用选项
--no-edit
shgit commit -m "Add feature A" # 发现漏了文件 feature_a_test.py git add feature_a_test.py git commit --amend --no-edit
追加内容并修改提交信息
shgit commit -m "Update README" # 发现 README.md 还有需要补充的内容 vim README.md git add README.md git commit --amend -m "Update README with installation steps"
git commit 指定日期
格式:
git commit -m 'test' --date=format:<format>:<date>
其中,<format>
可以是以下几种之一(参考:commit-96b2d4f Oct 3, 2007):
rfc2822
iso8601
default
relative
local
short
raw
举🌰:
sh
git commit -m 'test' --date=format:rfc2822: Mon, 3 Jul 2006 17:18:43 +0200
git commit -m 'test' --date=format:iso8601: 2006-07-03 17:18:43 +0200
git commit -m 'test' --date=format:default: Mon Jul 3 17:18:43 2006 +0200
git commit -m 'test' --date=format:relative:2.years.3.months.ago
git commit -m 'test' --date=format:relative:5.seconds.ago
git commit -m 'test' --date=format:local: Mon Jul 3 15:18:43 2006
git commit -m 'test' --date=format:short: 2006-07-03
git commit -m 'test' --date=format:raw:
git 合并最近3个提交
交互式变基:
sh
git rebase -i HEAD~3
可以看到交互式内容:
pick a1b2c3d Commit 1
pick d4e5f6g Commit 2
pick h7i8j9k Commit 3
编辑:
pick a1b2c3d Commit 1
f d4e5f6g Commit 2
f h7i8j9k Commit 3
保存退出。