先给最佳实践:准备工作。请先切换到项目的根目录。如果之前已经为Git配置了全局用户信息,并且本项目将沿用这些信息,那么可以直接跳过步骤2,否则请按照后续步骤依次执行命令。
# 1. 初始化仓库(如果尚未初始化)
git init
# 2. 配置用户信息(局部)
git config user.name 'allInAi'
git config user.email '793059909@ai-as.cn'
# 3. 添加远程仓库
git remote add origin https://gitee.com/baidumap/wanju.git
# 4. 关键步骤:创建初始提交(解决"master不存在"的核心问题)
git add .
git commit -m "Initial commit - project scaffolding"
# 5. 重命名主分支为 master(Git 默认可能是 main)
git branch -M master
# 6. 强制推送到远程仓库(覆盖远程初始状态)
git push -u origin master --force
的数字化之路
格物致知 知行合一 记录开悟时的小欢喜 也希望能通过这种方式正向反馈社区
你认为的提交,是不是下面这样这样的:
点“创建”,然后:
$ git clone https://gitee.com/用户个性地址/HelloGitee.git #将远程仓库克隆到本地
在克隆过程中,如果仓库是一个私有仓库,将会要求用户输入 Gitee 的账号和密码。按照提示输入即可。
当然,用户也可以通过配置本地的git配置信息,执行git config
命令预先配置好相关的用户信息,配置执行如下:
$ git config --global user.name "你的名字或昵称"
$ git config --global user.email "你的邮箱"
在 Gitee 平台中,强烈建议您在【设置-多邮箱管理】中的“提交邮箱”与上面配置信息中的邮箱地址保持一致,这样平台就能及时地统计您在平台中提交代码的贡献了。
修改代码后,在仓库目录下执行下面命令
$ git add .#将当前目录所有文件添加到git暂存区
$ git commit -m"my first commit"#提交并备注提交信息
$ git push origin master #将本地提交推送到远程仓库
和方法1的差别,在于先创建仓库。
$ git init
$ git remote add origin https://gitee.com/用户个性地址/wanju.git
这样就完成了版本的一次初始化。 接下去,进入你已经初始化好的或者克隆仓库的目录,然后执行:
$ git pull origin master
修改/添加文件,否则与原文件相比就没有变动。
$ git add .
$ git commit -m"第一次提交"
$ git push origin master
然后如果需要账号密码的话就输入账号密码,这样就完成了一次提交。
实际不是这样的,总有一些“故事”发生。
不管是提交到gitee或github或Bitbucket等代码托管平台,但这些平台都没有提供创建项目的脚手架,同时我们会使用这些托管平台提供的Readme.md文件等特性
这时就没有办法使用方法一的git clone,只能使用上面的方法二
git clone https://gitee.com/baidumap/wanju.git
那么在提交用IDE生成的脚手架代码时,是不是按下面
cd 代码所在根目录
git config user.name 'allInAi'
git config user.email '793059909@ai-as.cn'
git init
git remote add origin https://gitee.com/baidumap/wanju.git
git pull origin master
git add .
git commit -m "第一次提交"
git push origin master
这样这样一步一步操作,就可以顺序完成“第一次提交”?
我只能说:
% git push origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://gitee.com/baidumap/wanju.git'
怎么办?
难道有问题? 万事不决问AI。 唐成,公众号:的数字化之路被这个老代码骗了,还需要再重新发次版,烦
复盘一下,得到最新的最佳实践:
# 1. 初始化仓库(如果尚未初始化)
git init
# 2. 配置用户信息(局部)
git config user.name 'allInAi'
git config user.email '793059909@ai-as.cn'
# 3. 添加远程仓库
git remote add origin https://gitee.com/baidumap/wanju.git
# 4. 关键步骤:创建初始提交(解决"master不存在"的核心问题)
git add .
git commit -m "Initial commit - project scaffolding"
# 5. 重命名主分支为 master(Git 默认可能是 main)
git branch -M master
# 6. 强制推送到远程仓库(覆盖远程初始状态)
git push -u origin master --force
git init
后必须至少有一次 commit 才会真正创建分支main
分支,-M
参数确保重命名为 master
--force
参数覆盖远程可能存在的空提交(平台自动生成的 README)提交第一行代码
https://gitee.com/help/articles/4122
为commit自定义Author 场景一:全局级别。设置全局用户名和电子邮件地址。 git config --global user.name "YourName" git config --global user.email "your.email@ai-as.cn" 场景二:项目级别。不同的仓库的user.name和user.email是不同的 2.1 cd 到Git仓库所在文件夹目录【很关键】 2.2 执行以下git config命令 git config user.name "YourName" git config user.email "yourEmail@ai-as.net" 唐成,公众号:的数字化之路分享Git的两个小技巧
如何使用IDEA上的Git来管理代码? 唐成,公众号:的数字化之路AI|Mac换机+ChatGPT=!!!
Git中的其它报错如何解决:
1、You have divergent branches and need to specify how to reconcile them
% git pull origin master
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
2、fatal: refusing to merge unrelated histories
% git pull origin master
From https://gitee.com/baidumap/wanju
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
root@root todolist % git pull origin master --allow-unrelated-histories
From https://gitee.com/baidumap/wanju
* branch master -> FETCH_HEAD
Merge made by the 'ort' strategy.
.gitee/ISSUE_TEMPLATE.zh-CN.md | 13 +++++
.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md | 12 +++++
LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
README.en.md | 36 +++++++++++++
README.md | 37 +++++++++++++
5 files changed, 299 insertions(+)
create mode 100644 .gitee/ISSUE_TEMPLATE.zh-CN.md
create mode 100644 .gitee/PULL_REQUEST_TEMPLATE.zh-CN.md
create mode 100644 LICENSE
create mode 100644 README.en.md
create mode 100644 README.md
root@root wanju %
%git pull origin master --allow-unrelated-histories