1

[Git教程系列]基础操作2

 2 years ago
source link: https://lihui.net/git%e6%95%99%e7%a8%8b%e7%b3%bb%e5%88%97%e5%9f%ba%e7%a1%80%e6%93%8d%e4%bd%9c2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Git教程系列]基础操作2

9月 11, 2020 654点热度 0人点赞 0条评论

上一节,学习了Git和Github/或者其他平台连接的比较基础的使用方法,本节开始学习Git的分支的基础操作,基础操作包括,创建Git仓库git init,克隆远程分支git clone,添加到暂存(git add),提交记录(git commit),推送(push), 拉取更新(pull) ,创建新分支(git checkout),从远程创建新分支,推送新分支到远端。

本文的代码地址: https://github.com/lihuiocean/hello-git)

创建Git仓库

git init {{projectName}} # 后面的项目名称可以忽略 (如果不添加的话)直接使用当前的文件夹的名称作为项目名(当然可以修改)

克隆远程分支 git clone

命令行的规范

有两种规范 一种是采用https的规范,一种是采用git的规范,两种方式各有利弊,且对代码无任何差异,可以选择使用。

https要求每次进行远程交互的时候采用账号和密码进行认证,而ssh则要求使用密钥进行认证(需要事先绑定密钥,如何绑定密钥,可以查看前一节的内容)。

$ git clone https://*****.domain/{{username}}/{{repname}}
git clone https://github.com/lihuiocean/hello-git
$ git clone git@***.domain:{{username}}/{{repName}}
$ git clone [email protected]:lihuiocean/hello-git

CM4ZKd

经过上述的两种方式的一种方式,我们可以将远程的目录clone到本地,我比较推荐下面一种写法,可以快速的将代码进行提交到远端,不用每次都输入密码。

添加到暂存 git add

使用场景: 当我们修改文件,想将其放入版本管理系统的时候,首先使用git add {{filename}} 将文件放到暂存区域,而后对文件进行提交记录,正式加入版本管理系统

git add {{filename}}
git add hello.txt

此时文件已经放到暂存区域(虽然没有返回信息),使用下面的命令可以看到暂存区的文件

git status

P172Cu

git add . # 加入当前目录的所有的文件

提交记录 git commit

使用场景:当我们将代码放到暂存区后想要提交到git记录中,就要使用到commit命令

git commit -m "提示信息"
git commit -m "add hello.txt"
[master d7b6dfc] add hello.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

0UuM1o

推送分支 git push

使用场景:将本地的分支推送到远端,可以让其他的人看到

git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:lihuiocean/hello-git
   a8658dc..d7b6dfc  master -> master

git-push

拉取更新 git pull

使用场景: 当远程仓库有更新的时候(有人提交了新的代码的时候),我们可以使用git pull 拉取到最新的代码到本地

git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:lihuiocean/hello-git
   d7b6dfc..913accb  master     -> origin/master
Updating d7b6dfc..913accb
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

git-pull

创建新分支 git checkout

使用场景:想创建新的功能或者多人协作的时候,或者三套环境的时候(开发dev,预生产pre,生产环境prod)分开的时候,不影响其他人的使用。

创建新分支的方式有两种,基于本地的分支创建和基于远程的分支创建,其实这两者没有什么太大的区别。

基于本地的分支创建

在当前的分支拉取新分支

git checkout -b {{branchName}}
git checkout -b dev

git-checkout

将本地分支推送到远程

git push {{RmoteHostName}} {{localBranchName}}:{{remoteNewBranchName}} # 后面的是远程的新的代码分支的名字,可以自定义
git push origin dev:dev

git-push

基于远端的新分支

git checkout -b {{localBranchName}} origin/{{remoteBranchName}} # 前面的是本地的新分支的名称 后面的是远程的已经存在的新分支的名称
git checkout -b lihui-dev origin/dev

git checkout

显示所有的分支

git branch -a

6WmRIS

删除本地分支

git branch -d <brancheName>

删除远程分支

git push origin --delete <BranchName>
本作品采用 知识共享署名 4.0 国际许可协议 进行许可

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK