likes
comments
collection
share

github如何和已经开发好的项目关联

作者站长头像
站长
· 阅读数 9

之前本地写了一个小项目,现在打算放到github上,但是如何使这两者关联起来呢?我的步骤是这样的:

1.先进入项目里面,执行git init

$ git init 
Initialized empty Git repository in D:/workspace/go/src/mybeego/.git/ 

这样会在项目里面生成.git文件夹

github如何和已经开发好的项目关联

2.通过git remote进行关联【在这之前确保github上已经建了分支】

git remote add origin git@github.com:yif/mybeego.git

3.通过git remote show查看某个远程仓库的详细信息

10964@DESKTOP-HE0SBPH MINGW64 /d/workspace/go/src/mybeego (master)
$ git remote show
origin 

4.查看远程主机的网址

10964@DESKTOP-HE0SBPH MINGW64 /d/workspace/go/src/mybeego (master)
$ git remote -v
origin  git@github.com:yif/mybeego.git (fetch)
origin  git@github.com:yif/mybeego.git (push) 

5.通过git status查看本地状态

github如何和已经开发好的项目关联

6.因为github现在没有master分支,只有main分支,所以本地先建一个main分支

git branch main

7.切换分支到main分支

git checkout main

8.接下来就是git add . git commit -m '第一次提交'

9.然后push上去

github如何和已经开发好的项目关联

按照提示使用git push --set-upstream origin main

10964@DESKTOP-HE0SBPH MINGW64 /d/workspace/go/src/mybeego (main)
$ git  push --set-upstream origin main
To github.com:yif/mybeego.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'git@github.com:yif/mybeego.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. 

发现报错,直接通过-f强推

git push -f --set-upstream origin main

 10964@DESKTOP-HE0SBPH MINGW64 /d/workspace/go/src/mybeego (main)
$ git push -f --set-upstream origin main
Counting objects: 378, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (364/364), done.
Writing objects: 100% (378/378), 10.60 MiB | 1.43 MiB/s, done.
Total 378 (delta 26), reused 0 (delta 0)
remote: Resolving deltas: 100% (26/26), done.
To github.com:yif/mybeego.git
 + 27b9d3f...3f8724a main -> main (forced update)
Branch main set up to track remote branch main from origin. 

成功了!之后直接git push就可以了

有些时候发现强推也是不行,大概是设置了master或main分支是受保护的,不允许这么操作。那就新建一个分支

git branch develop

git push -u origin develop

-u表示把本地分支和远程分支进行关联,远程仓库的别名一般是origin

这样远程也会有develop分支了,并且把本地的更改的文件提交上去了。

第一次提交需要加 -u参数后,后面的提交就直接可以 git push