Git
使用
- Learn Git Branching: 可视化学习 Git 操作
基本命令(Basics)
git init:创建新的本地仓库(生成 .git 目录)git status:显示当前仓库状态git add <文件名>:将文件添加到暂存区git commit:提交暂存区内容为新提交
Tips: Write good commit messages!git checkout <版本号>:切换到指定提交或分支(如切换分支时,同时切换 HEAD)
信息查看
git help <command>:查看某个命令的帮助文档git log:显示提交历史git log --all --graph --decorate:以 D.A.G 图的形式展示提交历史git diff <文件名>:比较工作区与暂存区的差异git diff <版本号> <文件名>:比较两个快照间文件的差异git show查看某个提交的详细信息git blame:逐行显示最后一次修改的提交git bisect:二分查找 bug 所在的提交
分支与合并
git branch:查看本地分支git branch <分支名>:新建分支git switch <分支名>:切换分支git checkout -b <分支名>:新建并切换到该分支(等价于上面两条的组合)git merge <分支名>:将指定分支合并到当前分支git mergetool:使用可视化工具解决合并冲突Ctrl-w w移动窗口:only关闭其他窗口,只留 MERGED:diffg LO用当前分支:diffg RE用对方分支git merge --continue
git rebase:变基(将一个分支的提交移到新基底上)git rebase -i:交互式变基操作
远程仓库管理(Remotes)
git remote:列出所有远程仓库git remote add <名称> <url>:添加远程仓库git push <远程> <本地分支>:<远程分支>:推送本地分支到远程并更新引用git branch --set-upstream-to=<远程>/<远程分支>:设置本地分支与远程分支的跟踪关系git fetch:从远程获取对象和引用git pull:等同于 fetch + merge(拉取并合并)git clone <url>:克隆远程仓库git clone --depth=1:浅克隆,仅拉取最新历史
文件恢复与撤销(Undo)
git commit --amend:修改最近一次提交的信息或内容git reset <文件>:将文件从暂存区移除git restore:还原工作区文件git stash:暂存当前修改,保持工作区整洁git add -p:交互式暂存文件部分内容git revert:新建一个提交用于撤销某次历史提交git clean -n:查看将要删除的文件git clean -f:删除所有未跟踪的文件
git stash 本质上是把当前工作区和暂存区的修改打包成一个临时 commit 存到 refs/stash 里,并把工作区恢复干净。
bash
# 保存当前修改
git stash
git stash list
# 应用但不删除
git stash apply stash@{0}
# 应用并删除(常用)
git stash pop
# 删除某个 stash
git stash drop stash@{0}
# 清空所有 stash
git stash clear1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
进阶
git config:配置和个性化 Gitgit worktree:支持多工作区,便于同时检出多个分支.gitignore:设置忽略跟踪的文件或目录
Pre-Commit
submodule
shell
git submodule add <repo-url> <path>1
这会在你的 repo 中生成:
.gitmodules文件<path>目录(但内部是一个独立 Git 仓库)
shell
git submodule update --init --recursive1
shell
git pull origin main1
shell
cd ..
git add .
git commit -m "update yyy submodule"1
2
3
2
3
shell
git submodule deinit -f third_party/yyy
git rm -f <path>
rm -rf .git/modules/<path>1
2
3
2
3
配置
git 的安装
创建个人令牌
txt
Setting
-> Developer settings
-> Personal access tokens
-> Generate new token 保存密码到自己可以看到的位置1
2
3
4
2
3
4
免密登陆
shell
# 记住密码
git config --global credential.helper store
# 删除密码
git config --global --unset credential.helper1
2
3
4
2
3
4
github 配置 ssh
shell
cd ~
ssh-keygen -t rsa -C "xxx@xxx.com" # 这里输入你的邮箱
cd .ssh
cat id_rsa.pub # 复制到 github 的 ssh 设置中1
2
3
4
2
3
4
点击右上角的 settings 
将刚才复制的内容粘贴到这里
验证是否成功
shell
ssh -T git@github.com1
显示如下信息表明设置成功 
SSH
- 连接虚拟机
shell
ifconfig #记录 ip 地址
ssh user.name@ip1
2
2
设置 ssh 免密登录
在 win 主机上ssh-keygen生成一对公私钥,将公钥发送到服务器的~/.ssh/authorized_keys文件下
在 win 主机上的 ssh 配置中加入IdentityFile文件,即可实现免密登录
git 代理
shell
#http 代理
git config --global http.proxy 'http://127.0.0.1:7890'
#https 代理
git config --global https.proxy 'http://127.0.0.1:7890'
#http 代理
git config --global http.proxy 'socks5://127.0.0.1:7890'
#https 代理
git config --global https.proxy 'socks5://127.0.0.1:7890'
#取消 http 代理
git config --global --unset http.proxy
#取消 https 代理
git config --global --unset https.proxy1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
.gitignore 的配置
有时候不想要.git,.DS_Store 等文件,那么就需要在.gitignore中写清楚
shell
git rm -r --cached .DS_Store
git rm -r --cached **/.DS_Store1
2
2
原理
missing semester
版本控制 (Git) · the missing semester of your cs education
6. Lecture 6 - 版本控制 git_哔哩哔哩_bilibili
问题与解决
连接不上 port 443 Couldn‘t connect to server
- 方案一:关闭 VPN
- 方案二:取消代理
shell
git config --global --unset http.proxy
git config --global --unset https.proxy1
2
2
拒绝连接 connect to host github.com port 22: Connection refused
- 使用
github 443端口
给~/.ssh/config文件里添加如下内容,这样 ssh 连接 GitHub 的时候就会使用 443 端口。
txt
Host github.com
Hostname ssh.github.com
Port 4431
2
3
2
3
https和git链接换着试试
txt
url = https://github.com/username/repo.git
url = git@github.com:username/repo.git1
2
2
- 换梯子节点,检查 DNS 污染
推送失败 src refspec master does not match any
按照下面的顺序执行
shell
git commit -m "init"
git remote add origin xxxxxxxx.git
git push -u origin master1
2
3
2
3
远端链接失败 fatal: Couldn‘t find remote ref master
shell
# 检查本地配置
git config user.name/git config --global user.name
git config user.email/git config --gloabl user.email
# 检查仓库配置
git remote -v
git remote rm origin
git remote add origin XXXX1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
文件过大 RPC failed;curl 56 Recv failure: Connection was reset
shell
git config --global http.postBuffer 5242880001
如果设置之后提交还是报错的话,可能是因为某几个文件过大造成的;
这时就需要用到 git-lfs 具体用法见官网
shell
git lfs install
git lfs track "*.so"
git add .gitattributes1
2
3
2
3
Host key verification failed
重新配置一下 ssh,删除~/.ssh文件夹,重新生成 ssh key,然后再次连接。
具体操作看配置/github 配置ssh一节