汇总
git
常用命令,git
使用技巧和使用优化。
配置 sshkey
参考 SSH 常用命令
创建 sshkey
ssh-keygen -t rsa -C "邮箱"
在 Github 中添加公钥
复制 ~\.ssh\id_rsa.pub
文件中的内容,登录 Github
–> Account Setting
–> SSH-KEY
–> Add SSH-KEY
–> 粘贴id_rsa.pub
中的内容
验证
ssh -T git@github.com
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
说明配置成功,可以连接上 Github
git 命令
git config
global 变量:
git config --global user.name "xiexianbin" -- 配置用户名,上传本地 repository 到服务器上的时候,在 Github 上会显示这里配置的上传者信息
git config --global user.email "me@xiexianbin.cn" -- 配置邮箱
git config core.filemode true -- 配置文件mode敏感,在git管理的目录下执行,cat .git/config 查看
git config --list -- 查看配置列表
local 变量:
git config user.email "me@xiexianbin.cn" -- 配置邮箱,可以在 .git/config 中查看对应配置
git init
git init here -- 创建本地仓库
git remote add origin git@github.com:用户名/仓库名.git
-- 把本地仓库和远程仓库关联起来, 如果不执行这个命令的话,每次 push 的时候都需要指定远程服务器的地址
git clone
- 用户名密码
https://<username>@<git_url>
- SSH key
ssh://<username>@<git_url>
- personal access tokens
获取 github
personal access tokens
settings > developer settings > personal access tokens > generate new token
git clone http://<username>:<access tokens>@<git_url>
git clone https://<token>@github.com/owner/repo.git
git pull
git pull origin master
git push
git add
git add -A -- 将改动添加到本地仓库中
git rm xxx -- 从本地仓库中删除指定文件
git rm -r xxx -- 从本地仓库中删除指定文件夹
git commit -m "注释" -- 把本机缓存中的内容提交到本机的 HEAD 里面
git push origin master -- 把本地的 commit push 到远程仓库中
或
git push origin HEAD:master
指定分支push:
git push origin HEAD:stable/jdk8
git log
git show <commit-id> # 查看某次提交的内容
git log --pretty=oneline <file-path> # 查看某文件的修改历史
git log --stat <commit-id> # 仅显示修改行数
git log -- <file> # 只显示某文件的提交日志
.gitignore 文件
github 官方维护了一个仓库,专门管理 gitignore 配置:
https://github.com/github/gitignore
作用:忽略指定的内容
使用:
- 在本地仓库根目录创建
.gitignore
文件 - 过滤文件和文件夹:
[Tt]emp/
过滤Temp\temp
文件夹;*.suo
过滤.suo
文件; - 不过滤文件和文件夹:
!*.c
永久删除文件
要删除的文件是id_rsa
,运行如下命令:
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch id_rsa' \
--prune-empty --tag-name-filter cat -- --all
参数说明:
filter-branch
是让git
重写每一个分支--force
假如遇到冲突也让git
强制执行--index-filter
选项指定重写的时候应该执行什么命令,要执行的命令紧跟在它的后面,在这里就是git rm --cached --ignore-unmatch id_rsa
,让git删除掉缓存的文件,如果有匹配的--prune-empty
如果因为重写导致某些commit变成了空(比如修改的文件全部被删除),忽略掉这个commit--tag-name-filter
表示对每一个tag如何重命名,重命名的命令紧跟在后面,当前的tag名会从标注输入送给后面的命令,用cat就表示保持tag名不变。--
表示分割符--all
表示对所有的文件都考虑在内。
已经commit
到git
仓库,再运行如下命令永久删除远端上的文件:
git push --all --force
git 管理
分支管理
- master:长期分支
- feature / buggix:2钟短期分支
- release:发布版本分支
提交日志
每次代码提交必须有备注说明,注明本次提交做了哪些修改,commit 分类:
- bugfix: 线上功能 bug
- sprintfix: 未上线代码修改 (功能模块未上线部分bug)
- minor: 不重要的修改(换行,拼写错误等)
- feature: 新功能说明
- optimization:功能优化
- refactor:功能重构
- test:测试代码
- docs:文档
git 代理
已知代理(如V2ray
)默认端口:
- socks5 1081
- http 8001
http_proxy 方式
https_proxy=http://127.0.0.1:8001 git clone https://<git_url>
gitproxy 方式
Git 目前支持的三种协议 git://
、ssh://
和 http://
,其代理配置各不相同:
-
core.gitproxy
用于git://
协议 -
http.proxy
用于http://
协议 -
ssh://
协议的代理需要配置ssh
的ProxyCommand
参数 -
GIT 协议的配置
建立 /path/to/socks5proxywrapper
文件,使用 https://bitbucket.org/gotoh/connect
工具进行代理的转换,各发行版一般打包为 proxy-connect
或者 connect-proxy
。
#!/bin/sh
nc -x 127.0.0.1:1081 "$@"
配置 git
[core]
gitproxy = /path/to/socks5proxywrapper
或者
export GIT_PROXY_COMMAND="/path/to/socks5proxywrapper"
- SSH 协议的配置
建立 /path/to/soks5proxyssh
文件
#!/bin/sh
ssh -o ProxyCommand="/path/to/socks5proxywrapper %h %p" "$@"
配置 git 使用该 wrapper
export GIT_SSH="/path/to/soks5proxyssh“
当然也可以直接配置 ~/.ssh/config
的 ProxyCommand
全局代理
# socks5 协议
git config --global http.proxy socks5://127.0.0.1:1081
git config --global https.proxy socks5://127.0.0.1:1081
# http 协议
git config --global https.proxy http://127.0.0.1:8001
git config --global https.proxy https://127.0.0.1:8001
取消代理:
git config -l
git config --global --unset http.proxy
git config --global --unset https.proxy
github 代理
# socks5 协议
git config --global http.https://github.com.proxy socks5://127.0.0.1:1081
git config --global https.https://github.com.proxy socks5://127.0.0.1:1081
# http 协议
git config --global http.https://github.com.proxy https://127.0.0.1:8001
git config --global https.https://github.com.proxy https://127.0.0.1:8001
取消代理:
git config -l
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
Git 避免用户名和密码方法
方法一
- 创建文件存储GIT用户名和密码
在%HOME%目录中,一般为C:\users\Administrator,也可以是你自己创建的系统用户名目录,反正都在C:\users\中。文件名为.git-credentials,由于在Window中不允许直接创建以".“开头的文件,所以需要借助git bash进行,打开git bash客户端,进行%HOME%目录,然后用touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式:
Administrator@scloud-PC MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator@scloud-PC MINGW64 ~
$ touch .git-credentials
Administrator@scloud-PC MINGW64 ~
$ vim .git-credentials
https://{username}:{password}@github.com
- 添加Git Config 内容
进入git bash终端, 输入如下命令:
Administrator@scloud-PC MINGW64 ~
$ git config --global credential.helper store
执行完后查看%HOME%目录下的.gitconfig文件,会多了一项:
Administrator@scloud-PC MINGW64 ~
$ cat .gitconfig
[user]
name = xiexianin
email = me@xiexianbin.cn
[credential]
helper = store
重新开启git bash会发现git push时不用再输入用户名和密码
git branch --set-upstream-to=origin/<branch> master
git branch --set-upstream-to=origin/master master
方法二
- 添加环境变量
在windows中添加一个HOME环境变量,变量名:HOME,变量值:%USERPROFILE%
- 创建git用户名和密码存储文件
进入%HOME%目录,新建一个名为”_netrc"的文件,文件中内容格式如下:
machine {git account name}.github.com
login your-usernmae
password your-password
重新打开git bash即可,无需再输入用户名和密码
表情
git 提交可以使用如下命令填加表情:
git commit -m ":smile: your message"
git commit -m ":books: documentation is completed"
https://www.webfx.com/tools/emoji-cheat-sheet/
问题
多邮箱问题
remote pull push的时候会有问题,因为设置了 pull 的时候识别的是邮箱,2个 github 账号,2个邮箱,我们自然不能使用global的user.email,修复方式:
首先,取消global:
git config --global --unset user.name
git config --global --unset user.email
然后,设置每个项目repo的自己的user.email
cd project
git config user.email "me@xiexianbin.cn"
git config user.name "xiexianbin"
可以在.git/config
中查看对应的设置。
Git 中文乱码
在 terminal
中执行:
git config --global core.quotepath false
如果解决不了,若采用 oh-my-zsh
可以进行如下配置:
- 打开
oh-my-zsh
配置文件~/.zshrc
- 在文件最后面添加如下代码:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
- 重启下终端或输入
source ~/.zshrc
git remote: HTTP Basic: Access denied 错误
git 操作报错误:git remote: HTTP Basic: Access denied 错误
解决方法:
- 密码修改导致问题,重新输入账号密码即可,命令:
git config --system --unset credential.helper
- 清空密码认证信息,重新输入,命令:
git config --global http.emptyAuth true
SSL: no alternative certificate subject name matches target host name
git config --global http.sslVerify false
git pull index-pack failed
用git pull的时候,经常会遇到这样的错误:
fatal: early EOF
fatal: index-pack failed
有几处设置可以尝试着去解决这个问题。首先找到gitconfig文件。它的路径在:
*[git installed path]*/etc/gitconfig
以管理员模式打开这个文件,在[core]部分加上这一句:
compression = 0
这代表不压缩,会引起一定的性能问题。如果之后不再出现以上的错误,可以再去掉这一行。如果还不行,有可能是因为存在Git上的工程太大了,这时候需要把本地的限制调高一些。还是在[core]部分,加上这两行:
packedGitLimit = 512m
packedGitWindowSize = 512m
在[pack]部分加上这三行:
deltaCacheSize = 2048m
packSizeLimit = 2048m
windowMemory = 2048m
大小根据你的实际情况来填写
换行符问题
- 回车换行
- CR回车
- LF换行
- Windows/Dos CRLF \r\n
- Linux/Unix LF \n
- MacOS CR \r
- Git 配置
推荐设置:
git config --global core.autocrlf input
git config --global core.safecrlf warn
- CRLF设置
提交时转换为LF,检出时转换为CRLF:
git config --global core.autocrlf true
提交时转换为LF,检出时不转换:
git config --global core.autocrlf input
提交检出均不转换:
git config --global core.autocrlf false
- SafeCRLF 设置
拒绝提交包含混合换行符的文件:
git config --global core.safecrlf true
允许提交包含混合换行符的文件:
git config --global core.safecrlf false
提交包含混合换行符的文件时给出警告:
git config --global core.safecrlf warn
Git Push: Error writing request body to server
- 问题描述
- Git Push错误"Error writing request body to server"
默认Git设置http post的缓存为1M
- 解决方法
增加http post缓存, 如果使用git命令,设置参数命令如下:
#增加为 500MB
git config http.postBuffer 524288000
如果使用的是Eclipse Git插件,则需要在 Window -> Preferences 中找到 Team -> Git -> Configuration 配置界面,在System Settings Tab页中 点击 “Add Entry…” 按钮来添加一个配置项:
key为: http.postBuffer value为:524288000
ssh 链接慢
修改:/etc/ssh/sshd_config
UseDNS no
# GSSAPI options
GSSAPIAuthentication no
GSSAPICleanupCredentials no