Skip to content

Git 的使用

· 2 min

ssh#

ssh-keygen -t rsa -b 4096 -C "email@example.com"

拉取远程分支#

在本地创建分支 dev 并切换到该分支

Terminal window
git checkout -b dev(本地分支名称) origin/dev(远程分支名称)

将远程分支拉到本地

Terminal window
git fetch origin dev(远端分支名)

创建空分支#

Terminal window
git checkout --orphan empty-branch-name

撤销#

撤销工作区

Terminal window
git checkout filename

撤销暂存区

Terminal window
git reset HEAD filename

撤销 merge(没 git add 时)

Terminal window
git merge --abort

取消文件或文件夹追踪#

Terminal window
# 删除 readme1.txt 的跟踪,并保留在本地
git rm --cached readme1.txt
# 删除 dir1 目录,并保留在本地
git rm -r --cached dir1
# 删除 readme1.txt 的跟踪,并且删除本地文件
git rm --f readme1.txt

代理#

推送拉取代码失败时,可能是配置的代理有问题,可以取消代理配置试试。

Terminal window
# 检查当前代理配置
git config --global --get http.proxy
git config --global --get https.proxy
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

错误#

remote: HTTP Basic: Access denied.#

If a password was provided for Git authentication, the password was incorrect or you’re required to use a token instead of a password. If a token was provided, it was either incorrect, expired, or improperly scoped.

账号密码验证不通过,密码或者权限不对,导致 Git 操作失败。移除令牌,重新登录。

Terminal window
# 移除令牌
git config --system --unset credential.helper
# 长期存储令牌
git config --global credential.helper store
# 短期存储令牌,缓存在守护进程的内存
git config --global credential.helper cache

fatal: refusing to merge unrelated histories#

Terminal window
git merge master --allow-unrelated-histories
# or
git pull --allow-unrelated-histories

git 默认不区分大小写#

因为 git 考虑到兼容,默认不区分文件及文件夹的名称大小写。这个默认配置是分系统的: windows 不区分 macOS 不区分 Linux 区分

通过以下命令试 git 区分大小写

Terminal window
git config core.ignorecase false

https://www.kktoo.com/wiki/gitprofile/Chapter6/ignorecase.html