git 第一次提交本地代码到远程仓库操作方法

in git with 0 comment

配置git用户和邮箱

git config --global user.name "username"
git config --global user.email "email ID"

初始化推送代码到远程仓库

git init
git remote add origin "远程仓库地址"
git add .
git commit -m "init commit"
\#由于我们的gitlab默认主分支名为main,则设置origin为main
git checkout -b main
git push -u origin main

推送现有的git仓库

cd "代码目录"
git remote rename origin old-origin
git remote add origin "新的git远程仓库地址"
git push -u origin --all
git push -u origin --tags

报错解决

git pull --rebase origin main

执行完上述命令之后,再重新push

git push -u origin main

==报错信息部分如下:==

"remote: Gitlab: You are not allowed to push code to protected branches on this project."
解决方法:

在gitlab找到对应的项目的左侧菜单选择,"Settings----->Repository";
右侧“Protected branches” 中;
将push的权限改成,由默认的“maintainer”改成“develop+maintainer”,即允许开发者push代码到仓库中。

git 分支常用命令

1.列出分支,-a参数是列出所有分支,包括远程分支
git branch [-a]

2.创建一个本地分支
git branch branchname

3.创建一个分支,并切换到该分支
git checkout -b branchname

4.删除一个本地分支
git branch -d branchname

5.删除一个远程分支
git push origin --delete branchname

6.删除一个远程分支,通过push一个空的分支来覆盖原来的分支,以达到删除远程分支的目的
git push origin :branchname

参考文档:

https://www.cnblogs.com/nianzhilian/articles/14168260.html
https://blog.csdn.net/zzh920625/article/details/78087669

Responses