git更新代码到github仓库注意事项

更新步骤:

1.在电脑项目目录右键选择Open Git Bash here

2.输入命令

git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/你的用户名/你的仓库名.git
git push -u origin main

3.注意事项

3.1.Git的全局配置用户名和邮箱

配置用户名:使用以下命令来设置你的Git用户名。请将your_username替换为你的Git用户名。

git config --global user.name "your_username"

配置邮箱:使用以下命令来设置你的Git邮箱。请将your_email@example.com替换为你的邮箱地址。

git config --global user.email "your_email@example.com"

验证配置:设置完成后,你可以使用以下命令来检查你的全局配置是否正确:

git config --global --list

3.2.报错本地的分支master没有找到对应的远程分支

$ git push -u origin master

error: src refspec master does not match any

error: failed to push some refs to 'github.com:AIreception/Chatbot.git'

检查远程仓库的分支:首先,你需要确认远程仓库中分支的名称。你可以使用以下命令来查看远程分支:

git branch -r

确认远程仓库的URL:确保你推送的远程仓库地址是正确的。可以使用以下命令查看:

git remote -v

推送到正确的远程分支

git push -u origin master

或者,如果远程分支是main,你应该推送到main

git push -u origin main

如果需要,重命名本地分支:如果你的本地分支名称与远程分支名称不一致,你可能需要重命名本地分支。例如,将master重命名为main

git branch -m master main
git push -u origin main

3.3 报错本地分支main落后于远程分支main,也就是说远程分支有一些你的本地分支没有的提交。Git 阻止了这次推送,因为它不是快进合并(fast-forward),这意味着推送会导致远程分支的丢失提交。

$ git push -u origin main

To github.com:AIreception/Chatbot.git

! [rejected] main -> main (non-fast-forward)

error: failed to push some refs to 'github.com:AIreception/Chatbot.git'

hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. If you want to integrate the remote changes,

hint: use 'git pull' before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

强制推送:如果你确定要覆盖远程分支,并且不介意丢失远程分支上的任何提交,你可以使用--force-f选项来强制推送你的本地分支到远程分支。

git push -f origin main

使用--force-with-lease:这是一个更安全的强制推送选项,它不会覆盖远程分支上其他人的工作。

git push --force-with-lease origin main

先拉取再推送:如果你不想使用强制推送,可以先拉取远程分支的更改,解决任何合并冲突,然后再次尝试推送。

git pull origin main # 解决任何合并冲突 git push origin main

 

posted @ 2024-05-28 14:37  繁华博客  阅读(14)  评论(0编辑  收藏  举报