Git使用

一、远程仓库

1、将项目添加到远程库

$ git init
$ git add test.txt
$ git commit -m "first commit"
$ git remote add origin https://github.com/lkhan961221/testRepo.git
$ git push -u origin master
$ git add  test2.txt
$ git commit -m "second commit "
$ git push origin master 

touch+文件名

touch test.txt //产生一个内容为空的test.txt文件

2、克隆远程库项目

(1)普通克隆方式:

//$ git clone <远程仓库地址>
$ git clone https://github.com/lkhan961221/testRepo.git

这种克隆方式默认是克隆master主分支,
而且通过命令 git branch --list能看到克隆后在本地也只有这一个分支,

(2)克隆远程指定分支

  //$ git clone -b <指定分支名> <远程仓库地址>
  $ git clone -b dev_001 https://github.com/lkhan961221/testRepo.git

会自动在克隆dev_001分支到本地,同样克隆后本地只有这一个分支。

二、分支管理

1、查看分支

//查看本地所有分支
git branch
//查看远程所有分支
git branch -r
// 查看全部分支(包含本地和远程) 
git branch -a

2、新建分支

//新建分支不切换
git branch  [branchname]
//新建分支并切换
git branch -b  [branchname]

3、删除本地分支

(1)删除本地分支

git branch -d  [branchname]

(2)删除远程分支

git branch -d -r [branchname] //删除远程分支,其中<branchname>为本地分支名
//删除后,还要推送到服务器上才行
git push origin :<branchname>

4、重命名分支

重命名本地分支git branch (-m | -M) <oldbranch> <newbranch>

git branch -m oldbranchname newbranchname

使用-M则表示强制重命名。

如果你需要重命名远程分支,推荐的做法是:
——删除远程待修改分支
——push本地新分支名到远程

三、git合并远程2个分支

(1)克隆一个项目到本地

git clone

(2)查看远程全部分支

git branch -a

* master
remotes/origin/HEAD -> origin/master
remotes/origin/v1.2
remotes/origin/master
remotes/origin/v1.1
remotes/origin/v1.0

3.切换分支
案例情景:
比如同时有三个人开发,1.2最早是基于1.0,但是由于项目未发布,1.0,1.1,1.2全部都在同时开发,现在想把1.0已经增加的功能先合并到1.2;

此时的步骤:check 1.2和1.0

git checkout v1.0
git checkout v1.2

然后再v1.2的分支基础上执行merge

git merge v1.0

如果没有报错,那就说明合并成功啦。

4,提交代码

git push origin v1.2

参考命令:

Git鼓励大量使用分支:
查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
创建+切换分支:git checkout -b <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>

备注:粘贴按键是insert,不是ctrl+v
posted @ 2022-10-30 23:22  寒小韩  阅读(15)  评论(0编辑  收藏  举报