将本地项目推送到远程git新仓库

在git bash下

1. mkdir project
2. cd project
3. git init (初始化仓库)
4.复制项目文件到刚创建的新仓库目录
git config --global user.name [username]git config --global user.password [email]
git config --global user.email [email]

准备工作做好后,接着就是把本地仓库项目推送到远程仓库:

1.git add . (将当前目录下的文件加入到仓库)
2.git commit -m "the first commit project" (第一次提交项目文件)
3.git remote add origin https://git.coding.net/solent/xingzuo.git
4.git push -u origin master (推送远程仓库)

执行第四步的时候,报错如下:

To https://git.coding.net/solent/xingzuo.git
 ! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://git.coding.net/solent/xingzuo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

报错因为远程仓库与本地仓库文件不一致,使用git pull拉下远程代码到本地

5.git pull

接着又报如下错误:

warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From https://git.coding.net/solent/xingzuo
 * [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

  git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

  git branch --set-upstream-to=origin/<branch> master

报错原因是没有指定本地master和远程origin/master的连接,执行git branch --set-upstream master origin/master,设置链接

6.git branch --set-upstream master origin/master

The --set-upstream flag is deprecated and will be removed. Consider using --trac k or --set-upstream-to

Branch master set up to track remote branch master from origin.

然后再执行下git pull

7.git pull
fatal: refusing to merge unrelated histories

这回报更严重的致命错误,根据提示然后如下执行:

8.git pull origin master --allow-unrelated-histories 

From https://git.coding.net/solent/xingzuo
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
.gitignore | 46 +++++++++++++++
LICENSE | 191     
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
README.md | 1 +
3 files changed, 238 insertions(+)
create mode 100644 .gitignore
create mode 100644 LICENSE
create mode 100644 README.md

最后再执行推送到远程仓库的命令,就不会再报错了

9.git push -u origin master 

Counting objects: 2279, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2227/2227), done.
Writing objects: 100% (2279/2279), 46.29 MiB | 543.00 KiB/s, done.
Total 2279 (delta 349), reused 0 (delta 0)
To https://git.coding.net/solent/xingzuo.git
29b55c0..7c92f17 master -> master
Branch master set up to track remote branch master from origin.
在 Visual Studio Code (VSCode) 中使用 Git 将本地项目推送远程仓库通常分为以下几个步骤: 1. **安装Git**: 如果还未安装 Git,你需要先从官方网站下载并安装 Git(https://git-scm.com/downloads)。 2. **初始化本地仓库**: 打开 VSCode,右键点击项目文件夹,选择“Git” > “Initialize Repository”,创建一个的 .git 文件夹。 3. **配置用户信息**: 在终端或命令提示符中输入 `git config --global user.name` 和 `git config --global user.email` 来设置你的用户名和邮箱。 4. **添加文件到暂存区**: 添加所有要提交的更改到暂存区(staging area),例如: ``` git add . ``` 5. **查看状态**: 查看当前哪些文件被添加了或修改了,用 `git status`。 6. **提交更改**: 输入提交信息描述这次提交的内容,然后执行: ``` git commit -m "your commit message" ``` 7. **连接远程仓库**: 首次连接时,需要生成一个的 SSH 密钥对,将其添加到 GitHub 或其他服务的SSH密钥管理(如 `ssh-agent`)。然后,在命令行中,复制远程仓库的 URL 并运行: ``` git remote add origin <remote-repo-url> ``` 8. **推送代码**: 最后,将本地分支推送远程仓库: ``` git push -u origin main ``` `-u` 标志是首次推送时自动跟踪远程主分支。 如果你还没有创建过远程仓库,第一次推送会提示你是否要创建一个远程分支。根据提示操作即可。 **相关问题--:** 1. 如何检查我的 Git 是否已经正确设置? 2. 除了 `main` 分支,还有哪些常见的远程分支名称? 3. 如何解决推送失败,因为默认分支与远程不符的问题?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值