查看git分支目录的工具_轻量级办公平台项目记录(1):git的使用

为什么在项目的开篇要介绍下git的使用呢?俗话说:“工欲善其事,必先利其器”,git工具就是项目开发的必备利器,尤其是在多人协作开发环境中。

使用git工具可实现分布式的版本控制,可在服务端和本地创建一个版本库。

参考资料:http://www.runoob.com/git/git-tutorial.html

1、在windows上安装git工具

安装包下载地址:https://gitforwindows.org/

使用安装包完成安装后,git工具会自动安装git bash 工具(打开开始菜单,找到"Git"->"Git Bash"),会弹出git命令窗口,在命令窗口下可以进行git操作,同时鼠标右键也可以快速打开git bash

2、基本配置

配置个人用户名称和电子邮件

$ git config --global user.name "RobbieHan"
$ git confit --global user.email robbie_han@outlook.com

查看配置信息

$ git config --list

3、Git创建仓库

使用当前目录作为仓库

$ git init 

使用指定目录作为Git仓库

$ git init myproject

4、Git基本操作

git clone:
使用git clone拷贝一个Git仓库到本地,例如从github上克隆git-demo项目

$ git clone https://github.com/RobbieHan/git-demo.git
# ssh方式 git clone git@github.com:RobbieHan/git-demo.git

克隆完成后,会在当前目录下生成一个git-demo的项目目录,接下来我们就可以查看git-demo项目文件,修改代码了

git add :
使用git add 命令将文件添加到缓存,我们在git-demo项目中创建一个test.py文件,然后使用git add 将文件添加到缓存

$ touch test.py
$ ls
README.md  test.py
$ git status -s
?? test.py

其中git status命令是用于查看项目当前状态,接下来使用git add命令来添加文件:

$ git add test.py
$ git status -s
A  test.py

这时候在使用git status命令可以看到 test.py文件已经成功添加到缓存。

git diff:

查看尚未缓存的改动(已经修改变更尚未执行git add的文件):git diff

$ echo "# This is test.py" > test.py
$ git diff
diff --git a/test.py b/test.py
index e69de29..0c029d2 100644
--- a/test.py
+++ b/test.py
@@ -0,0 +1 @@

从上面输出结果可以看出git diff 会一行行显示还未提交到缓存的具体变动内容。

查看已缓存成功的改变:git diff --cached

$ git add .
$ git diff --cached
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..0c029d2
--- /dev/null
+++ b/test.py
@@ -0,0 +1 @@
+# This is test.py

上面使用了 git add . 是提交所有项目文件到缓存,执行git diff --cached 可以看到已经缓存成功的改变,包括新建 test.py文件 ,将内容写入 test.py

git commit:

使用git commit 将存入缓存的快照添加到仓库中

$ git commit -m '这是一条描述信息:将缓存快照添加到仓库'
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 test.py
 $ git status
 nothing to commit, working directory clean

将缓存中的快照提交到仓库后,再来执行git status 可以看到已经没有需要提交的改动信息了。

git checkout -- file:

使用git checkout -- file 用来撤销修改,将文件恢复到上一个版本,比如将一些无用或错误的数据写到项目文件了,这时可以使用git checkout来撤销修改

$ echo "这是一条错误的数据" > test.py
cat test.py
$ cat test.py
这是一条错误的数据
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout -- test.py
$ cat test.py
# This is test.py

可以看到使用 git checkout -- file 命令已经将 test.py恢复到之前版本

上面的操作是我们用来恢复工作区的错误操作,如果已经将工作区的错误操作通过 git add 添加到了缓存区怎么办呢?可以使用 git reset HEAD file 来撤销缓存区的修改。

5、git分支管理

使用git分支可以让你从开发主线上分离出来,然后在不影响主线的同时进行开发工作。

创建分支:

$ git checkout -b dev # -b 表示创建并切换到该分支
Switched to a new branch 'dev'

查看分支:

$ git branch
* dev  # 当前分支前面会显示一个 * 号
  master

接下来我们对项目文件的修改都只会在dev分支上生效,例如给test.py文件添加一行内容

$ echo "dev test" >> test.py
$ git add test.py
$ git commit -m "branch dev test"
 1 file changed, 1 insertion(+), 1 deletion(-)

以上的修改操作不会影响到master分支的文件,切换到master分支查看test.py

$ git checkout master
Switched to branch 'master'
$ cat test.py
# This is test.py

当然,我们在dev 分支完成的开发工作可以合并到master分支:

$ git merge dev
Updating 459d678..a4a069b
Fast-forward
 test.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

合并分之后master分支的项目内容就和dev一样了。

分支管理的命令小结:

创建分支:git branch <name>

查看分支:git branch

切换分支: git checkout <name>

合并分支到当前分支:git merge <name>

删除分支:git branch -d <name>

合并冲突:

切换到dev分支,在test.py里面追加一行内容:"This is branch dev"

$ echo "This is branch dev" >> test.py
$ git add test.py
$ git commit -m "dev" 

切换到master分支, 在test.py里面追加一行内容:"This is branch master"

$ git checkout master
$ echo "This is branch master" >> test.py
$ git commit -am test.py

现在 dev 和master两个分支都对文件test.py做了改动,结下了合并分支就是出现冲突:

$ git merge devAuto-merging test.py
CONFLICT (content): Merge conflict in test.py
Automatic merge failed; fix conflicts and then commit the result.

使用git status查看存在冲突的文件:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")

接下来我们需要手动去修改冲突内容:

$ vim test.py
dev test
<<<<<<< HEAD
This is branch master
=======
This is branch dev
>>>>>>> dev

可以看到里面test.py里面已经标记了两个分支修改的内容,手动修改下冲突的内容,保存文件。

$ git add test.py
$ git commit -m "master"

冲突合并完成。

6、Git标签:

在做程序开发时,经常会发布维护多个版本,这时就可以用到标签(tag),需要用到某个版本时,根据标签就可以获取对应的版本。

给当前分支打上一个标签:

$ git tag v1.0
v1.0

如果在代码提交到git仓库的时候忘记打标签了,可以追加标签

$ git log --oneline --decorate --graph
*   97b7808 (HEAD -> master, tag: v1.0) Merge branch 'dev'
|
| * 3bbe270 (dev) dev
* | 0bad6a6 test.py
|/
* a4a069b branch dev test
* 459d678 这是一条描描述信息‘
* a7968a0 (origin/master, origin/HEAD) git demo
$ git tag v0.1 a7968a0   # 通过日志找到commit id 然后通过 commit id来追加标签

使用指定的tag来生成分支

git checkout -b <branch_name> <tag_name>
git checkout -B <branch_name> <tag_name> # 如果分支已经存在使用 -B 可以强制创建分支,覆盖原来的分支

git标签命令:

查看标签:git tag
查看标签详细信息:git show <tagname>
指定标签信息:git tag -a < tagname> -m <tag_desc>

删除一个标签:git tag -d <tagname>

7、使用远程仓库

生成密钥文件:

$ ssh-keygen -t rsa -C "youremail@example.com"

上面邮箱换成github注册邮箱,然后一直回车,系统会在用户目录下生成.ssh文件夹,打开id_rsa.pub,复制里面的key。

vim ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx984lhTT0nlW3QWTZBiO4WT3BtdOq7OGfgbWlT2NhcW0Uqj7UkMexG4dseUxz4L2TMDjTJIhRdMX+n5Qq1qQwsoQoA3AXg1TxEEZZQ9JJNcanQG58X09235XsIYwAGZbnoixjNAMbV1aa+oLafKvL3InQav+P0Xj38tWVuuJqjtt+QQCiEx8W828N6etR/dcoDw4Isa1k9Hntn4qoY+GqRnkcyBY4aIXMcrakghFcDVH5XlPBndXXRLm06VGtIgpqX82+2gXo0Pp4+p0LMTRnDVxE1fjJy2FwnQf4LkNgeDr7o+DMbTEpPgSKogI9kFpwnbCZjjab robbie_han@hotmail.com

登陆github网站进入-settings选择 SSH and GPG keys ,然后选择 New SSH key, 填写 title, 在key选项里面填入刚刚复制的key, 保存添加。

本地通过git bash 测试密钥链接是否成功

$ ssh -T git@github.com
Hi RobbieHan! You've successfully authenticated, but GitHub does not provide shell access.

在github上新建一个仓库,仓库名为:gitdemo, 将本地仓库提交到github

$ git remote add origin git@github.com:RobbieHan/git-demo.git
$ git push -u origin master

上传分支到github

$ git push -u origin dev

上传标签到github

$ git push origin v1.0  # 上传一个tag
$ git push origin --tags # 上传全部tag

我们从github上克隆了一个项目,如果这个项目更新了,可以使用两条命令来获取更新

$ git fetch origin master # 从远程仓库下载新的分支数据
$ git merge # 合并到本地当前分支

克隆某个分支到本地:git clone -b <branch name> [remote repository address]

查看当前远程库:git remote -v

删除远程仓库: git remote rm [别名]

更多git使用请参考网络资料

-----------------------------------------------------------------------------------------------

知乎专栏:Django 学习小组
知乎专栏:SandBox
更多实战文档可关注知识星球:https://t.zsxq.com/a6IqBMr(微信中打开链接)
轻量级办公管理系统项目开源地址:RobbieHan/gistandard
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值