Git使用指北
前言:注意!使用git时,开始工作前先pull,结束工作后要push!!!
前言:注意!使用git时,开始工作前先pull,结束工作后要push!!!
前言:注意!使用git时,开始工作前先pull,结束工作后要push!!!
先选中文件夹,右键,选择 Git Bash Here
第一次链接上传
新建仓库
git init
git config --global user.name "名字"
git config --global user.email "邮箱"
准备提交
// 单文件提交:git add 文件名
// 全部文件提交:git add .
git add .
git commit -m "提交信息"
链接仓库
法一:使用HTTPS连接
git remote add origin https://github.com/仓库.git
法二:使用SSH连接
git remote set-url origin git@github.com:仓库.git
如果 HTTPS 连接不稳定,考虑切换到 SSH 方式来进行推送
上传仓库
// 注意远程仓库新建时不要手动添加readme文件
git push -u origin main
后续上传
准备提交
// 单文件提交:git add 文件名
// 全部文件提交:git add .
git add .
git commit -m "提交信息"
上传仓库
git push origin main
修改commit
git commit --amend
- 按 i 进入编辑模式
- 按 esc 退出编辑模式
- 输入
:wq
退出log状态
参考:
https://zhuanlan.zhihu.com/p/100243017
一些常用命令
查看日志
git log
退出按q
下拉代码
git pull
查看分支状态
git status
良好的分支习惯:
- 每次提交到一个新的本地分支
git checkout -b newbranch
<修改代码>
- 获取远程分支
git add .
git commit -m "提交信息"
- 将拉取到的远程分支与之前提交的本地分支合并
git checkout main
git pull
git merge newbranch
- 提交
git push
Github上传代码及解决main主分支问题
https://blog.youkuaiyun.com/Arcofcosmos/article/details/112862038
git 分支命名规范
https://blog.youkuaiyun.com/a6864657/article/details/107236221
参考:
https://www.runoob.com/git/git-remote-repo.html
https://blog.youkuaiyun.com/BUCTOJ/article/details/89879619
一些报错
* branch main -> FETCH_HEAD
fatal: refusing to merge unrelated histories
原因:
本地仓库与远程仓库的历史记录不相关,通常是因为你在本地初始化了一个新仓库,而远程仓库已经有了一些提交
解决方法:
- 拉取远程更改并允许不相关历史合并:
git pull origin main --allow-unrelated-histories
- 推送本地更改
git push origin main
注意:使用--allow-unrelated-histories
选项时,请确保你确实想合并这两个不同历史的仓库,特别是在团队协作的情况下,以免丢失重要的提交记录。