一、基础操作,本地上传GitHub
1、在GitHub上新建仓库 此时只有一个分支master分支
2、在本地下载这个仓库
$ git clone https://github.com/fanghejun/crawler.git
3、在本地仓库放入新建的文件和文件夹,上传到GitHub
$ cd git_first/ # 进入到工程文件
$ git add . # 将修改的文件夹和文件加载到本地待上传区
$ git commit -m "first commit" # 本次上传的注释
$ git push origin master
二、分支操作
1、创建分支并上传数据
(master)$ git branch test_branch # 创建另一个分支
(master)$ git checkout test_branch # 切换分支
(test_branch)$ git remote add origin https://github.com/zjq7/stoke-DeepRL.git
# 与远程分支关联, 但是这里已经在GitHub上建立, 无需二次建立 ,因此这里可能会报错
(test_branch)$ git add .
(test_branch)$ git commit -m "branch test first"
(test_branch)$ git push -u origin test_branch
三、合并分支到主分支
(test_branch)$ git checkout master # 从test_branch分支切换到主分支
(master)$ git merge test_branch # 合并分支到主分支
(master) $ git push # 将分支数据合并到到master
To https://github.com/FrancisWangH/NISL_EUROSYS2021.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/FrancisWangH/NISL_EUROSYS2021.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 origin master # 以为我们在GitHub上修改文件后, 与本地就不同步了, 因此会报错
$ git push # 这里重新push就好了
Everything up-to-date
四、问题总结
1、GitHub上修改了文件,本地也修改了,直接可以下载GitHub的文件。
2、本地GitHub项目上传到gitee
先在gitee上创建一个新的项目, 如test.然后得到test的git链接
$ https://gitee.com/fhj/test.git
然后在本地下载GitHub的项目
$ git clone --recursive https://github.com/fhj/crawler
在删除本地GitHub项目与远程GitHub的联系
$ git remote rm origin
本地项目与远程gitee进行链接
$ git remote add origin https://gitee.com/fhj/test.git
在将本地项目上传到gitee
$ git push --set-upstream origin main
3、删除当前项目的远程连接,建立新的连接
$ git remote rm origin // 移除本地关联
$ git remote add origin git@**.git // 添加线上仓库
$ git push -u origin master // 注意:更改后,第一次上传需要指定 origin
4、一直显示GitHub连接失败
$ git config --global --unset https.https://github.com.proxy
$ git config --global --unset http.https://github.com.proxy
5、更换版本
$ git tag # 查看都有那些版本
$ git checkout v1.6.0 # 根据上面提示, 更换版本
6、
# 具体错误
git clone https://github.com/jqzhao7/scan.git
Cloning into 'scan'...
fatal: unable to access 'https://github.com/jqzhao7/scan.git/': Failed to connect to github.com port 443: Connection refused
# 下面两个命令执行后就能解决上述问题
$ git config --global http.proxy http://127.0.0.1:1080
$ git config --global https.proxy http://127.0.0.1:1080