Git links local projects to GitHub

  1. First operate on the local project: create a local warehouse and push the code to the local warehouse

git init  初始化本地仓库
git checkout -b <本地分支名>  创建本地分支并切换分支
git add . 添加到暂存区
git commit -m <备注信息>  代码推到本地仓库
git remote add origin <远程仓库地址> 链接到远程仓库
git push --set-upstream origin <远程分支名> / git push -u origin <远程分支名>
本地分支代码推送到远程分支,并关联到远程分支
  1. Possible pitfalls when local code is pushed to remote

  • Question 1: Prompt that the remote warehouse cannot be read

Solution: There is a high probability that GITHUB does not set a key. The specific steps are as follows

查看本地是否存在 SSH密钥 打开node命令行 输入ls -al ~/.ssh,看是否有id_rsa和id_rsa.pub,如果有跳过第二步;
生成SSH密钥:ssh-keygen -t rsa -C “自己的Email地址” 注意:执行完成后会有一些列提示输入密码的指令,直接回车即可;
查看SSH公钥:cat /Users/电脑用户名/.ssh/id_rsa.pub
公钥复制粘贴到github,地址:https://github.com/settings/ssh/new
  • Problem 2: The remote warehouse can be read, but the push is rejected

Solution 1: There is a high probability that the push of the local branch code will overwrite the remote branch code. Execute the following command to force the overwrite, and the push can be successful

git push -f --set-upstream origin <分支名>

Solution 2:

git pull --rebase origin master 进行代码合并 
git push --set-upstream origin <远程分支名> / git push -u origin <远程分支名> 代码推送到远程
  1. Pitfalls that may be encountered when code is merged with the main branch

  • Problem: The branch is not pulled from the main, and the following problems will occur when two independent branches are merged

Solution: Execute the following command on the current branch

git config pull.rebase false  修复分支偏离
git pull origin main --allow-unrelated-histories 拉取远程main分支代码并合并

Guess you like

Origin blog.csdn.net/lfq1996/article/details/129361544