ssh#
ssh-keygen -t rsa -b 4096 -C "email@example.com"
拉取远程分支#
在本地创建分支 dev 并切换到该分支
git checkout -b dev(本地分支名称) origin/dev(远程分支名称)
将远程分支拉到本地
git fetch origin dev(远端分支名)
创建空分支#
git checkout --orphan empty-branch-name
合并分支#
Merge commit#
将两个分支的历史记录合并在一起,生成一个新的”合并提交”(merge commit),保留两个分支的完整历史。
# 当前分支为 main, 要合并 dev 分支到 main 分支git switch maingit merge dev
出现冲突: 只需要处理冲突,然后 git add
后重新 commit
。
Rebase and merge#
将当前分支的提交”重新播放”到目标分支的最新提交之上,重写提交历史,使历史记录呈现线性。
# 当前分支为 main, 要合并 dev 分支到 main 分支git switch devgit rebase maingit switch maingit merge dev
出现冲突: 需要一个个节点上处理冲突,在处理完冲突后,使用 git add
提交修改后的文件,然后执行 git rebase --continue
来处理下一个节点,一层层处理。
Squash and merge#
将分支的所有更改压缩为一个单独的提交,并合并到 master,有助于简化复杂的提交历史。
# 当前分支为 main, 要合并 dev 分支到 main 分支git switch maingit merge --squash devgit commit -m ""
撤销#
撤销工作区
git checkout filename
撤销暂存区
git reset HEAD filename
撤销 merge(没 git add
时)
git merge --abort
取消文件或文件夹追踪#
# 删除 readme1.txt 的跟踪,并保留在本地git rm --cached readme1.txt# 删除 dir1 目录,并保留在本地git rm -r --cached dir1# 删除 readme1.txt 的跟踪,并且删除本地文件git rm --f readme1.txt
代理#
推送拉取代码失败时,可能是配置的代理有问题,可以取消代理配置试试。
# 检查当前代理配置git config --global --get http.proxygit config --global --get https.proxy# 取消代理git config --global --unset http.proxygit 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 操作失败。移除令牌,重新登录。
# 移除令牌git config --system --unset credential.helper# 长期存储令牌git config --global credential.helper store# 短期存储令牌,缓存在守护进程的内存git config --global credential.helper cache
fatal: refusing to merge unrelated histories#
git merge master --allow-unrelated-histories# orgit pull --allow-unrelated-histories
git 默认不区分大小写#
因为 git 考虑到兼容,默认不区分文件及文件夹的名称大小写。这个默认配置是分系统的: windows 不区分 macOS 不区分 Linux 区分
通过以下命令试 git 区分大小写
git config core.ignorecase false
https://www.kktoo.com/wiki/gitprofile/Chapter6/ignorecase.html