git
常用命令介绍,git
使用技巧和使用优化。
Git 常用命令
git init
git init here -- 创建本地仓库
git remote add origin git@github.com:用户名/仓库名.git
-- 把本地仓库和远程仓库关联起来, 如果不执行这个命令的话,每次 push 的时候都需要指定远程服务器的地址
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 clone
https://<username>@<git_url>
ssh://<username>@<git_url>
获取 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 clone 子目录
仅 clone 一个 git 项目的子目录
mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>
# 配置要 clone 子目录
git config core.sparseCheckout true // 等价于 git sparse-checkout init
echo "some/dir/" >> .git/info/sparse-checkout // 等价于 git sparse-checkout set "some/dir/"
# 查看要 clone 子目录,等价于 cat .git/info/sparse-checkout
git sparse-checkout list
# clone
git pull origin <target-branch> [--depth=1]
git commit
git commit -m "注释" -- 把本机缓存中的内容提交到本机的 HEAD 里面
git commit -sv -m "xxx" --date="2018-01-01T00:00:00+0800" -- 指定提交的时间
git revert
回滚一个提交
git revert <commit-id>
git pull
git pull origin master
git push
git add
git add -A -- 将改动添加到本地仓库中
git rm xxx -- 从本地仓库中删除指定文件
git rm -r xxx -- 从本地仓库中删除指定文件夹
git push origin master -- 把本地的 commit push 到远程仓库中
或
git push origin HEAD:master
指定分支push:
git push origin HEAD:stable/jdk8
git log/show
git show <commit-id> # 查看某次提交的内容
git log --pretty=oneline <file-path> # 查看某文件的修改历史
git log --stat <commit-id> # 仅显示修改行数
git log -- <file> # 只显示某文件的提交日志
git tag
git tag -l
# 显示离当前提交最近的标签
git describe --tags
为文件添加执行权限
# 新文件
git add --chmod=+x -- entrypoint.sh
# 已有问题
git update-index --chmod +x entrypoint.sh
# 若不生效,直接使用 chmod a+x entrypoint.sh; git add .
# 查看文件权限
git ls-files --stage
git describe
git describe 命令显示离当前提交最近的标签
git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>…]
git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]
$ git describe --tags
v2.2.12-8-g040935c
说明:
v2.2.12
tag 名称
8
表示自 v2.2.12
后有 8 次提交(commit)
g040935c
g
为 git 的缩写
040935c
为当前提交的 commit id
git rev-list
按时间顺序倒序列出提交对象
# 按时间倒序列出所有 commit id
git rev-list --all
# 按时间倒序列出最后一次提交的 commit id
git rev-list --all -n 1
# 按时间倒序列出最后一次提交的 commit id 缩写
git rev-list HEAD --abbrev-commit --max-count=1
# 和 submodule 结合使用
git submodule foreach git rev-list HEAD --abbrev-commit --max-count=1
git ls-remote
列出远程存储库中的引用
git merge
将两个或多个分支历史合并在一起,以从 dev
向 main
分支合并为例:
# 首先切换到dev
git checkout dev
# 获取最新 dev 代码
git pull
# 切换到main分支
git checkout main
# 把 dev 分支的代码merge到 main 分支
git merge dev
# 推送到远程 main 分支
git push
.gitignore 文件
github 官方维护了一个仓库,专门管理 gitignore 配置:
https://github.com/github/gitignore
作用:忽略指定的内容
使用:
- 在本地仓库根目录创建
.gitignore
文件
- 过滤文件和文件夹:
[Tt]emp/
过滤 Temp\temp
文件夹; *.suo
过滤 .suo
文件;
- 不过滤文件和文件夹:
!*.c
表情
git 提交可以使用如下命令填加表情:
git commit -m ":smile: your message"
git commit -m ":books: documentation is completed"
https://www.webfx.com/tools/emoji-cheat-sheet/
加密
git 加密有很多方式
- 与 keybase 结合
- Git 文件加密
- Sealed-secret
- vault 等
问题
多邮箱问题
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
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 config --global core.autocrlf input
git config --global core.safecrlf warn
提交时转换为LF,检出时转换为CRLF:
git config --global core.autocrlf true
提交时转换为LF,检出时不转换:
git config --global core.autocrlf input
提交检出均不转换:
git config --global core.autocrlf false
拒绝提交包含混合换行符的文件:
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
删除 git 密码
$ git credential-manager uninstall
git: 'credential-manager' is not a git command. See 'git --help'.
The most similar command is
credential-manager-core
F&Q
failed to push some refs
$ git push origin master
Username for 'https://github.com': REDACTED
Password for 'https://REDACTED@github.com':
To https://github.com/REDACTED.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/REDACTED.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
- 解决方法,使用如下命令可以将仓库已经提交的合并到本地
git pull --rebase origin master
skip SSL verify
- 解决CA引起的问题
fatal: unable to access 'https://xxx.git/': server certificate verification failed. CAfile: none CRLfile: none
export GIT_SSL_NO_VERIFY=1