配置 Git Bash:
git config --global user.name <name>
git config --global user.email <email>
# 如果开了 Github 的 `Keep my email addresses private` 功能,
# 设置 email 时 要填 Github 生成的 email, 而不是注册 email.
# 还有一些其他可选设置
以下是我的一些配置:
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
credential.helper=manager
core.editor="C:\\Program Files\\Microsoft VS Code\\Code.exe" --wait
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
user.name=XieNaoban
user.email=[email protected]
core.quotepath=true
配置 ssh:
# 查看版本 (我的版本比较新, 老版本的指令略有不同)
$ ssh -V
OpenSSH_8.0p1, OpenSSL 1.1.1c 28 May 2019
# 新开一个 ssh 代理
$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-hoSfpFGbN64j/agent.1678; export SSH_AUTH_SOCK;
SSH_AGENT_PID=1679; export SSH_AGENT_PID;
echo Agent pid 1679;
# 确定一下这个 ssh 还没有密钥
$ ssh-add -l -E md5
The agent has no identities.
# 整一个新的密钥, 一路回车确定 (默认配置)
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
# 新密钥的私钥和公钥储存位置 (id_rsa 和 id_rsa.pub)
$ ls ~/.ssh/
id_rsa id_rsa.pub known_hosts
# 添加到 ssh-agent
$ ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/xie/.ssh/id_rsa ([email protected])
# 再次查看 ssh-agent 的密钥, 已经成功添加
$ ssh-add -l -E md5
4096 MD5:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx [email protected] (RSA)
然后登陆 Github, 复制公钥:
clip < ~/.ssh/id_rsa.pub
登陆后 https://github.com/settings/keys
-> New SSH Key
, ‘Title’ 随意, 在 ‘Key’ 粘贴.
然后测试一下连接 (初次使用以下指令会多一段对话(如下), 输入 “yes”):
$ ssh -T [email protected]
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi XieNaoban! You've successfully authenticated, but GitHub does not provide shell access.
直接偷懒把当前时间作为本次提交版本的 “提交说明”, 每次都要思考 “提交说明” 怎么写真是太累了.
# 获取当前时间作为 "提交说明"
readonly date=$(date +%Y-%m-%d\ %H:%M:%S)
git add . # 添加当前目录所有文件到暂存区
git commit -m "${date}" # 提交到本地仓库, 以时间作为 "提交说明"
git push origin master # origin 表示远程主机, master 为分支名称
git branch # 查看分支
git branch <branch> # 创建分支
git checkout <branch> # 切换分支
git checkout -b <branch> # 创建并切换分支
# 推送到远程分支: 远程还没有该分支
git push origin <local_branch>:<remote_branch>
git status # 找出不同的文件、文件夹
git status -s # 找不同, 显示很精简规整, 就很舒服
git diff # 找出不同的每一行
git checkout <branch_1> # 转到分支1
git merge <branch_2> # 合并分支2
git push origin <branch_1> # 推送到远程分支
git pull <host> <remote_branch>:<local_branch>
git pull <host> <remote_branch> # 与当前本地分支合并
git pull # 简写, 合并本分支
# git pull origin tmp 相当于:
git fetch origin
git merge origin/tmp
# git pull 更快, 但 fetch 再 merge 可以方便查看 diff
git fetch origin master:tmp
git diff tmp
git merge tmp
例如对于 git status
指令, 中文文件 “新建文本文档.txt” 会被显示为
\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt
# 解决 Windows Git Bash、Linux 下的中文转码问题
git config --global core.quotepath false
例如对于 git log
指令, 中文的 Commit Message 会被显示为:
<87><8A><9F><83><9F><9C><8C><88><90>
解决方法如下, 在 Powershell 输入:
git config --global core.quotepath false
git config --global gui.encoding utf-8
git config --global i18n.commit.encoding utf-8
git config --global i18n.logoutputencoding utf-8
$env:LESSCHARSET='utf-8' #该行可以不输入, 但可能之后要重启 Powershell
然后设置系统环境变量, 设置 LESSCHARSET
为 utf-8
.
$
总是另起一行命令前的那一串叫 “系统终端命令提示符 (Prompt Sign)” (就是那一串包含用户名, 工作目录和 $
符号的东西). Windows 下的 Git Bash 的命令提示符的 $
总是另起一行.
这玩意由 $PS1
控制. 输入 echo "$PS1"
, 可得:
# 我的PS1被 conda 修改过, 所以前面有个 "(base)"
(base)
xie@DESKTOP-BAD47V8 MINGW64 ~
$ echo "$PS1"
(base) \[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$
修改它就能修改提示符样式.
而 Ubuntu 下的 $PS1
为:
xie@xie-vm:~$ echo "$PS1"
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
那么依葫芦画瓢改一改. 用 vim 或 code (vscode) 命令打开 ~/.bash_profile
, 添加一行:
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[32m\]\u@\h\[\033[00m\]:\[\033[33m\]\w\[\033[00m\]\$ "
保存重启 Git Bash即可.
git clone
报错$ git clone [email protected]:xxx/xxx.git
Cloning into 'xxx'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
此时如果测试连接, 却显示正常:
$ ssh -T [email protected]
Hi XieNaoban! You've successfully authenticated, but GitHub does not provide shell access.
网上的解决方案大多是说 ssh 配置出了问题. 但我这里不是这个原因. 原因是勾选了 https://github.com/settings/emails
-> Keep my email addresses private
, 好像是我的隐私邮箱被系统更换了, 所以就挂了. 解决方案同下面的 Push 时报错 push declined due to email privacy restrictions
push declined due to email privacy restrictions
从某天开始突然不能 push 了, 错误信息:
! [remote rejected] master -> master (push declined due to email privacy restrictions)
原因是勾选了 https://github.com/settings/emails
-> Keep my email addresses private
, 可能是因为它重置了隐私邮箱吧.
复制 Keep my email addresses private
下给的邮箱 (数字+名字@users.noreply.github.com),
git config --global user.email [email protected]
git commit --amend --reset-author # 会打开文本编辑器, 直接关闭即可
就好了.
设置:
git config --global core.autocrlf input # 提交时转换为 LF, 签出时不转换
# 或
git config --global core.autocrlf false # 提交签出均不转换
以下载 LF 的版本. 折腾完了改回去 (毕竟 Win 下还是默认开着好):
git config --global core.autocrlf true # 提交时转换为 LF, 签出时转换为 CRLF
解决方案: 直接干脆把文件统一成 LF, 不要转来转去了, 反正在 Win 下用 LF 大多数软件也支持.
warning: LF will be replaced by CRLF
Windows 以 CRLF 为换行符, Linux 以 LF 为换行符, Git 仓库以 LF 为换行符. 从 Win 上 git clone
时, Git 会自动把仓库里所有的文件从 LF 转为 CRLF. 当提交时, 又会把 CRLF 转回 LF. 如果提交文件是纯 LF 文件, 会显示一个警告: warning: LF will be replaced by CRLF in xxx.
.
直接关了这个 warning:
git config --global core.safecrlf false # 默认是 'warn'
git reset
与 git revert
区别git reset
回滚到某 commit, 后面的 commmit 都抛弃了. 而 git revert
创建一个新 commit, 中和之前的提交.
用 git reset
后 git push
会报错, 提示你当前本地版本落后于远程版本. 需要强制推送 (git push -f
).