如何新建git仓库并连接以及git branch相关命令

Quick setup — if you’ve done this kind of thing before
Set up in Desktop
or
Get started by creating a new file or uploading an existing file. We recommend every repository include a **README, LICENSE, and .gitignore**.

create a new repository on the command line

echo "# XXX" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
//例如https://github.com/xxx/xx.git
git remote add origin github地址
git push -u origin main

 push an existing repository from the command line


git remote add origin github地址
git branch -M main
git push -u origin main
…or import code from another repository
You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

--Import code

 git branch命令小结

1、git branch

查看本地当前所在分支,并且在当前分支前面加“*”号标记

2、git branch -r

查看远程分支,r是remote的简写
3、git checkout -b mybranch origin/mybranch

取远程分支并分化一个新的分支到本地;然后此刻,本地已经切换到了该新分支,执行 git pull ,将代码拉下来,本地才有了完整的对应分支;
前提***:先查看远程分支情况,执行 git branch -r

例子:

git checkout -b hotfix_v2003 origin/hotfix_v2003
4、git branch -a

列出本地分支和远程分支

5、git branch _分支名

创建一个新的本地分支,需要注意,**此处只是创建分支,不进行分支切换**;git checkout -b _分支名 创建一个新的本地分支,同时切换到刚新建的分支上。

6、git branch -m | -M oldbranch newbranch

重命名分支,如果newbranch名字已经存在,则需要使用-M强制重命名,否则,使用-m进行重命名。

7、git branch -d | -D branchname

删除branchname分支,D表示强制删除

8、git branch -d -r branchname

删除远程branchname分支,通知还需要执行push命令,才能真正删除:git push origin : branchname

补充:如果不执行push命令,虽然通过git branch -r已经看不到branchname了,但在GitHub的网页上依然能看到branchname,而且执行git fetch命令后,再git branch -r,由可以看到branchname,说明如果不push没有真正删除远程分支。

9、git branch -v

查看各个分支最后一次提交

10、git branch -merged

查看哪些分支合并入当前分支

11、git branch -no-merged

查看哪些分支未合并入当前分支

12、git fetch origin

更新远程库到本地

13、git push origin mybranch

推送分支

14、git merged origin/mybranch

去远程分支合并到本地

 

git新建项目关联远程仓库实战

git init
git remote add origin https://gitee.com/zhangpenga/rili.git
git pull origin master
git add .
git commit -m 'init'
git push origin master

至此就全部关联上了

posted @ 2021-02-25 09:46  卡卡罗特琪琪  阅读(322)  评论(0编辑  收藏  举报