ssh配置
ssh-keygen -t rsa -C "selector@gmail.com",按3个回车,密码为空- 打开C:/用户/用户名/.ssh/.pub,添加到https://github.com/settings/keys
拉取
初始化 git 仓库
git init
clone 仓库
# --depth=1 只拉取近一次的提交记录
git clone 项目远程地址 --depth=1 项目名
配置
git config user.name '用户名'
git config user.email '邮箱'
git config core.autocrlf
git config core.eol
# 查看全部配置信息
git config --list
git config -l
# 查看指定配置
git config --get key
git config key
# 修改指定配置
git config key value
加上 --global 参数可查看或修改全局配置
暂存
git add <file>
git add .
# 查看状态(添加,提交完之后可以查看文件是否修改,提交等)
git status
# 撤销
git reset HEAD <file>
git reset HEAD
# 取消暂存
git restore --staged <file>
git restore --staged .
# 从索引中移除已追踪文件
git rm --cached <file>
# 从索引中移除已追踪文件,多个文件
git rm -r --cached <dir>
提交
# 会打开 vi 界面
git commit
# 直接提交
git commit -m "commit msg"
# 提交记录
git log
git log --oneline
# 修改最后一次提交信息
git commit --amend
# 撤销未推送提交,但保留改动在工作区
git reset HEAD~1
# 或 git reset --mixed HEAD~1
# 撤销未推送提交,但保留改动在暂存区
git reset --soft HEAD~1
# 完全撤销提交
git reset --hard HEAD~1
# 撤销已推送提交,不会重写历史,历史记录保留原始提交和撤销提交
git revert <commit-hash>
git push
推送
git push
# 强制推送
git push --force
分支
# 查看分支
git branch
git branch -a
# 新建分支
git branch new-branch-name
# 或者新建分支,并切换到该分支
git checkout -b new-branch-name
# 切换分支
git checkout branch-name
# 或者更现代的
git switch branch-name
# 推送新的分支
git push -u origin new-branch-name
# 删除分支
git branch -d old-branch-name
# 强制删除,比如即使该分支还没有被合并
git branch -D old-branch-name
# 切换分支之前有未提交更改,应该先提交或stash
git stash
# 切换回来后,来恢复之前的工作状态
git stash pop
# 合并分支
git merge branch-name
# 取消合并
git merge --abort
常见异常
CRLF/LF警告
症状
- Windows上提示:
LF will be replaced by CRLF - macOS/Linux上提示:
CRLF will be replaced by LF原因是不同平台换行符不一致
仓库级配置
# Windows
git config core.autocrlf true
# macOS/Linux
git config core.autocrlf input
更稳妥的做法
用 .gitattributes 固定文本行为
# 统一文本文件为 LF
* text=auto eol=lf
# 保持二进制原文件
*.png binary
*.jpg binary
*.jpeg binary
# more...
已有文件按规则重新规范化
git add --renormalize .
git commit -m "fix: normalize line endings to LF"
git push
