Git
基础教程
Git 安装(Windows)
Git 常用命令
初始化仓库(生成一个
.git的隐藏文件)1
git init
添加远程仓库地址
1
2
3
4git remote add origin "远程仓库地址"
# eg:
git remote add origin https://github.com/zxiaosi/Vue3-FastAPI.git全局创建用户 (在
"your.name""your.email"内填入自己的姓名与邮箱)1
2git config --global user.name "your.name"
git config --global user.email "your.email"
检查信息是否写入成功
1
2git config user.name
git config user.email拉取远程仓库指定分支代码
1
2
3
4git pull origin "分支名"
# eg:
git pull origin master查看已更改的文件
1
git status
添加当前目录下的所有文件到暂存区
1
2
3
4
5# 添加当前目录下所有文件
git add .
# 添加单个文件
git add README.md提交暂存区到本地仓库中
1
2
3
4git commit -m "备注信息"
# eg:
git commit -m "添加了README.md文件"将本地仓库代码上传到远程仓库
1
2
3
4git push origin "分支名"
# eg:
git push origin master创建分支
1
2
3
4git branch "分支名"
# eg:
git branch test切换所有分支
1
2
3
4git checkout "分支名"
# eg:
git checkout test查看分支名
1
2
3
4
5# 查看本地分支
git branch
# 查看所有分支(本地+远程仓库)
git branch -a合并分支
1
2
3
4git merge "分支名"
# eg:
git merge test删除分支
1
2
3
4
5
6
7# 删除本地已合并的分支
git branch -d "分支名"
# 或者
git branch -D "分支名"
# 删除远程分支
git push origin --delete "分支名"克隆远程代码
1
2
3
4
5git clone -b "分支名 或 tag名" "远程仓库地址"
# eg: 拉取默认分支可不带 -b 参数
git clone https://github.com/zxiaosi/Vue3-FastAPI.git
git clone -b master https://github.com/zxiaosi/Vue3-FastAPI.git查看提交日志(按
q键 退出)1
git log
强制推送(不推荐,慎用!)
1
git push origin master -f
Git 配置密钥
添加远程仓库地址(
SSH地址)
进阶教程
查看 Git 配置
显示隐藏文件
找到文件所在的文件夹,点击左上角的
文件,然后选择更改文件夹和搜索选项点击
查看,然后选中显示隐藏的文件、文件夹和驱动器
找到文件夹里面的
.git文件进入
.git文件夹,找到config右键以记事本打开,可以看到相应的信息
Git 暂存
前提条件
- 同一分支多人协作情况
- 小心动了他人分支,忘记切换分支情况
命令
执行存储时,添加备注,方便查找,只有 git stash 也要可以的,但查找时不方便识别。(例如:
git stash save "当前时间")1
git stash save "save message
查看 stash 了哪些存储
1
git stash list
显示做了哪些改动,默认查看第一个存储,如果要显示其他存贮,后面加
git stash show num,比如第二个git stash show stash 11
git stash show
应用某个存储,但不会把存储从存储列表中删除,默认使用第一个存储,即 stash 0,如果要使用其他
stash,git stash apply stash num, 比如第二个:git stash apply 11
git stash apply "索引"
删除所有缓存的
stash1
git stash clear
实际操作
当本地仓库与远程仓库不一致,
git stash save "save message",暂存更改,回到上次拉取代码的状态(默认会暂存到 list 中第一个)查看暂存,
git stash listgit pull origin 分支名,拉取代码合并代码,
git stash apply 0,注意看记录是否有冲突(下方表示有冲突)解决冲突,以 Vscode 为例,全局搜索
>>>>,找到冲突的地方,保留本地修改或者远程修改(视情况而定)git status,查看一下是否解决完,解决完之后就可以提交代码了

























