Git初始化提交项目代码并与远端建立连接

如题

闲来无事,炒个冷饭。。。

步骤

1. 先本地建仓库

  • 方法一:
    执行命令(使用git bash或者类似工具,或者IDEA下terminal命令行):
git init

会在当前目录下创建一个新的空Git库。

  • 方法二:
    在IDEA中,执行如下操作也可:
    在这里插入图片描述
    此时当前项目下就生成了.git存储库。

2. 将文件添加到 .git库中

执行命令,将当前目录下所有修改添加到git库里:

git add .

3. 提交修改

提交修改,使用以下命令:

git commit -m "提交信息提示"

4. 在Github/Gitlab/Gitee/Gitea等等创建仓库

5. 使用以下命令关联远程仓库

git remote add origin 远程仓库地址    # 命名为了origin
或者
git remote add project名 远程仓库地址

6. 推送到远端

git push -u origin(本地仓库) master(远端仓库分支名)

这里注意:对于github和gitlab创建远程仓库的默认分支名为 main, 不是master!!!

报错:

error: src refspec main does not match any

原因就是我们git默认创建的分支是出于master下,但是github以及gitlab上的分支为main分支,官方的解释是:

GitHub is working on replacing the term “master” on its service with a neutral term like “main” to avoid any unnecessary references to slavery

解决:将本地分支改为main分支

git branch -m master main

然后再次提交。

git push -u project main

最后

放俩传送门吧:

  1. 如何用git创建本地仓库并且push到远程仓库
  2. 关于git的问题:error: src refspec main does not match any

猜你喜欢

转载自blog.csdn.net/qq_36256590/article/details/131112712
今日推荐