Git 操作文档

Git 是一个十分流行的版本控制系统,Git 和 SVN 区别在于,SVN使用增量文件系统,存储每次提交之间的差异。而 git 使用全量文件系统,存储每次提交的文件的全部内容(snapshot)。

git 保存的不是文件的变化或者差异,而是一系列不同时刻的文件快照。

Git 文档地址

目录

安装

linux 下 git 安装很简单,apt-get 和 yum 直接装即可.

  1. $ yum install -y git

配置

使用git config进行信息配置, 其实是针对 config 文件进行配置

  1. $ cat config
  2. [core]
  3. repositoryformatversion = 0
  4. ...
  5. [remote "origin"]
  6. url = https://...
  7. fetch = +refs/heads/*:refs/remotes/origin/*
  8. [branch "master"]
  9. remote = origin
  10. merge = refs/heads/master

若加上--global, 则是全局配置, 比如配置 [user] 域下的 name 和 email 信息。

  1. $ git config --global user.name "bascker"
  2. $ git config --global user.email "xxx@xx.com"
  3. # 查看 git 配置信息
  4. $ git config --list

别名

跟 linux 别名命令 alias 一样,git 也可以设置别名。

  1. # 给 checkout 设置别名 ck,git ck 等价于 git checkout
  2. $ git config --global alias.ck checkout
  3. $ git config -l | grep alias
  4. alias.ck=checkout

初始化仓库

使用git init可以创建一个 git 仓库。一般而言,纯粹用于版本控制的仓库,以 .git结尾。

  1. # 初始化一个 git 仓库
  2. $ git init sample.git

一般创建完仓库后,我们需要执行如下命令来允许 push 操作.

  1. $ git config --gloabl receive.denyCurrentBranch ignore
  2. # 若不执行, 可能会导致 push 失败, 报错
  3. $ git push
  4. remote: error: refusing to update checked out branch: refs/heads/master
  5. remote: error: By default, updating the current branch in a non-bare repository
  6. remote: error: is denied, because it will make the index and work tree inconsistent
  7. remote: error: with what you pushed, and will require 'git reset --hard' to match
  8. remote: error: the work tree to HEAD.
  9. remote: error:
  10. remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
  11. remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
  12. remote: error: its current branch; however, this is not recommended unless you
  13. remote: error: arranged to update its work tree to match what you pushed in some
  14. remote: error: other way.
  15. remote: error: To squelch this message and still keep the default behaviour, set
  16. remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

上面这个配置比较麻烦,可以在创建 git 仓库时一次性解决。使用如下命令即可。

  1. # --shared 参数
  2. $ git init --bare --shared sample.git

获取项目

git 支持 ssh/http(s)/ftp(s) 方式来获取远端代码. 通过git clone命令即可.

  1. $ git clone https://USER_NAME:USER_PASS@IP/sample.git

命令选项:

  • -b: 获取指定分支,若不存在则报错
  • --depth: 获取代码的深度。若指定为 1,则只会获取到远程仓库的 master 分支

更新代码

最常用/基本操作,就是在本地进行代码/文本编辑后,提交代码入库.

  1. # 跟踪当前目录下所有文件的变更
  2. $ git add .
  3. # 提交变更
  4. $ git commit -m "描述信息"
  5. # 推送到远端代码库
  6. $ git push

对于 commit 而言, 注释最好以一行短句子(小于 50 个字符)作为开头, 然后空一行再把进行详细的注释,这样可以很方便的用工具把 commit 注释变成 email 通知,第一行作为标题,剩下的部分就作 email 正文.

分支管理

一般使用 git branch 和 git checkout 进行基础的分支管理操作.当然更进一步就是使用git merge/fetch/rebase命令了(这些命令的使用, 容后再议).

  1. # 查看当前仓库的所有分支
  2. $ git branch -a
  3. * master
  4. remotes/origin/master
  5. # 切换分支: -b 表示若无指定分支,则新建一个分支
  6. $ git checkout -b bascker
  7. Switch to a new branch 'bascker'
  8. # 删除分支: 若使用 -D,则强制删除
  9. $ git branch -d bascker

第一次 push 本地分支代码到远端时, 需要加 --set-upstream, 从而在远端创建一个分支.

  1. $ git push --set-upstream origin bascker

git 分支本质就是指向提交对象(校验和文件)的指针,每次 commit,指针都会移动,指向最后一个 commit 对象。commit 流程图如下所示。

特殊 HEAD 指针,指向当前分支。若当前在 master 分支,则 HEAD 指向 master 分支。若在 bascker 分支,则指向 bascker。

  1. # 使用 --decorate 查看分支各分支指针所指向的对象
  2. $ git log --oneline --decorate
  3. aa403d4 (HEAD -> master, bascker) add b2.txt
  4. ...

如上,HEAD 指向 master 分支,master & bascker 均执行 aa403d4 提交对象。

分支合并

当在分支 bascker 上进行开发后,想把代码合入主干 master,就可以执行合并操作 git merge.

  1. # master 分支执行
  2. $ git merge bascker

若出现冲突,则解决完冲突,在继续 merge.

Note:除第一次 commit 外,每次 commit 对象都会有一个父对象,多分支合并产生的 commit 对象有多个父对象。

查看状态

使用 git status 可以查看当前工作目录的状态,查看索引内容,哪些文件被暂存了(在Git索引中), 哪些文件被修改了但是没有暂存, 哪些文件没有被跟踪(untracked)。git 十分人性化的一点就在于每一个时间点,使用 git status 都有提示,告诉你下一步怎么做.

  1. $ echo aaa > a.txt
  2. $ git add a.txt
  3. $ git status
  4. On branch master
  5. Your branch is up-to-date with 'origin/master'.
  6. Changes to be committed:
  7. (use "git reset HEAD <file>..." to unstage)
  8. new file: a.txt
  9. Untracked files:
  10. (use "git add <file>..." to include in what will be committed)
  11. .idea/

查看历史

使用git log可以看到历史提交信息,搭配git show使用效果更佳.

  1. $ git commit -m "add a.txt"
  2. $ git log
  3. commit 3da5d8691146288de84a466cbfb123fed5171329
  4. Author: bascker <xxx@aliyun.com>
  5. Date: Sat May 26 22:16:15 2018 +0800
  6. add a.txt
  7. # 查看 commit 对象信息
  8. $ git show 3da5d8691146288de84a466cbfb123fed5171329
  9. commit 3da5d8691146288de84a466cbfb123fed5171329
  10. Author: bascker <jp2317015793@aliyun.com>
  11. Date: Sat May 26 22:16:15 2018 +0800
  12. add a.txt
  13. diff --git a/a.txt b/a.txt
  14. new file mode 100644
  15. index 0000000..3c8969b
  16. --- /dev/null
  17. +++ b/a.txt
  18. @@ -0,0 +1 @@
  19. +aaa
  20. # 使用 --oneline 可以将各个 commit 信息简单化
  21. $ git log --oneline
  22. aa403d4 add b2.txt
  23. e4bf2fe add b1.txt
  24. ...

git show 用于查看 git 对象信息, 每一次提交都是一个 commit 对象,包括分支、tag 也是对象.

git log 常见选项

  • --stat: 显示在每个提交(commit)中哪些文件被修改了, 这些文件分别添加或删除了多少行内容
  • --pretty: 格式化日志输出
  • --graph: 图形化显示

标签管理

标签即 tag,通过git tag来进行标签管理,用于版本发布. 标签分为 2 种:

  • 轻量标签:指向 commit 提交对象的引用
  • 附注标签:生成独立的 tag 对象,拥有完整信息,建议使用
  1. # 轻量标签
  2. $ git tag v1.0.light
  3. # 附注标签: 最简单方式就是使用 -a -m 参数
  4. $ git tag -a v1.1 -m "附注标签"
  5. $ git tag
  6. v1.0.light
  7. v1.1
  8. # 查看标签对象
  9. $ git show v1.1
  10. Tagger: bascker <xxx@aliyun.com>
  11. Date: Sat May 26 22:31:35 2018 +0800
  12. <E9><99><84><E6><B3><A8><E6><A0><87><E7><AD><BE>
  13. commit 3da5d8691146288de84a466cbfb123fed5171329
  14. Author: bascker <xxx@aliyun.com>
  15. Date: Sat May 26 22:16:15 2018 +0800
  16. add a.txt
  17. diff --git a/a.txt b/a.txt
  18. new file mode 100644
  19. index 0000000..3c8969b
  20. --- /dev/null
  21. +++ b/a.txt
  22. @@ -0,0 +1 @@
  23. +aaa
  24. # 使用 git checkout 可以切换到指定标签
  25. $ git checkout v1.1

变基操作

一般都是使用git merge进行分支合并, 但还有一种更棒的方式就是git rebase变基操作.所谓变基,就是变更基线.

如图所示,我们基于 master 创建了 merge 和 rebase 分支, 在 merge 和 rebase 分支上分别前进了 2 次, 与此同时, master 也前进了, 这时候我们分别在 merge 分支执行 git merge master 和在 rebasr 分支执行 git rebase master 利用 gitk 查看结果,可以看到 rebase 操作的分支历史比 merge 干净整洁很多.

Note: gitk 是一个以图形的方式显示项目历史的命令

  1. $ git rebase master
  2. First, rewinding head to replay your work on top of it...
  3. Applying: add r1.txt
  4. Applying: add r2.txt

git rebase 操作会将本地的提交历史,由远及近逐一跟 master 进行合并。若发送冲突,则需要我们手工解决冲突后,执行 git rebase --continue 继续之前的操作即可.

git rebase 还拥有将多个 commit 合并成一个 commit 的逆天功能,这个功能十分有用.

  1. # 将前 2 次提交进行 rebase
  2. $ git rebase -i HEAD~2

这样就会进入命令模式, 然后根据提示进行操作即可

  1. pick f8f5714 add b1.txt
  2. pick aea2a1b add b2.txt
  3. # Rebase ba6bea9..aea2a1b onto ba6bea9 (2 command(s))
  4. #
  5. # Commands:
  6. # p, pick = use commit
  7. # r, reword = use commit, but edit the commit message
  8. # e, edit = use commit, but stop for amending
  9. # s, squash = use commit, but meld into previous commit
  10. # f, fixup = like "squash", but discard this commit's log message
  11. # x, exec = run command (the rest of the line) using shell
  12. #
  13. # These lines can be re-ordered; they are executed from top to bottom.
  14. #
  15. # If you remove a line here THAT COMMIT WILL BE LOST.
  16. #
  17. # However, if you remove everything, the rebase will be aborted.
  18. #
  19. # Note that empty commits are commented out

暂存变更

有时候我们还未开发代码完毕,需要切到其他分支,但又不想执行 commit 提交变更时,可通过git stash命令来暂存变更.

  1. $ echo ccc > c.txt
  2. $ git stash # 暂存变更
  3. $ git stash list
  4. stash@{0}: WIP on bascker: aea2a1b add b2.txt
  5. # 切换到其他分支, 执行某些操作
  6. $ git checkout master
  7. $ ...
  8. # 切换回后,取回缓存
  9. $ git stash pop
  10. # 再执行 git stash list 显示没有暂存了

获取指定分支代码

有时候我们需要获取指定分支的代码,怎么办呢?可以通过git fetch & git checkout 来实现.

  1. # 1.fetch 指定分支到本地
  2. $ git fetch origin ORIGIN_BRANCE_NAME
  3. # 2.将远程分支映射到本地
  4. $ git checkout -b LOCAL_BRANCH_NAME origin/ORIGIN_BRANCE_NAME

回滚操作

git 操作万一失误了,怎么办? 放心,git 提供了强大的故障恢复机制,提供 git reset 命令来提供回滚操作。

  1. $ echo "just a text" > test.txt
  2. # 对 test.txt 进行跟踪
  3. $ git add .
  4. $ git status
  5. On branch master
  6. Changes to be committed:
  7. (use "git reset HEAD <file>..." to unstage)
  8. new file: test.txt
  9. # 回滚
  10. $ git reset
  11. $ git status
  12. On branch master
  13. Untracked files:
  14. (use "git add <file>..." to include in what will be committed)
  15. test.txt
  16. nothing added to commit but untracked files present (use "git add" to track)
  17. $ rm c.txt
  18. $ git add .
  19. $ git commit -m "rm c.txt"
  20. $ git log --oneline
  21. 0ac49d7 rm c.txt
  22. aa403d4 add b2.txt
  23. e4bf2fe add b1.txt
  24. ...
  25. $ ls
  26. a.txt* b.txt* b1.txt* b2.txt*
  27. # 回滚到 rm 前的
  28. $ git reset --hard HEAD~2
  29. HEAD is now at e4bf2fe add b1.txt
  30. $ ls
  31. a.txt* b.txt* b1.txt* c.txt*
  32. $ git log --oneline
  33. e4bf2fe add b1.txt
  34. b0ccab5 c.txt
  35. ...

也可以搭配 git reflog(可看到所有的 git 操作记录) 来进行回滚,如执行了错误的 rebase 时,就可以搭配进行救急。

  1. $ git reflog
  2. e7ebdde HEAD@{0}: reset: moving to HEAD~1
  3. d7154af HEAD@{1}: commit: add test.txt
  4. e7ebdde HEAD@{2}: reset: moving to HEAD
  5. # 回滚到 add test.txt 这个阶段
  6. $ git reset --hard HEAD@{1}

附录A 本地版本库

若没有远程 git 仓库,没法很好的练习 git 操作咋办呢?没问题,我们可以使用本地版本库。通过在本地创建一个 git 仓库,可以将其 clone 到另一路径,然后进行 git 操作.

  1. $ cd repo
  2. $ git init sample.git
  3. $ cd ..
  4. $ git clone repo/sample.git

其实就是 git 本地协议的支持,将本地硬盘的一个目录作为远程版本库来使用。git 支持 4 种协议:

  • 本地协议
  • HTTP(S) 协议
  • SSH 协议
  • git 协议

附录B 支持 http 方式 clone

想通过 HTTP 方式来进行 git clone 操作,是需要配置 httpd 服务的.

1 安装软件

为了使用 git-http-backend(支持 git 的 CGI 程序), 需要按照 git-core,apache 支持 git 就靠它

  1. $ yum install -y git git-core httpd

2 配置 http 账户

  1. # 修改仓库所有者,使得 apache 可以访问
  2. $ chown -R apache:apache /workspace/gitrepo
  3. # 创建 apache 账户 tanlang.
  4. # -m:使用 MD5 算法对密码进行加密
  5. # -c:创建一个加密文件
  6. $ htpasswd -m -c /etc/httpd/conf.d/git.htpassd tanlang
  7. New password:
  8. Re-type new password:
  9. Adding password for user tanlang
  10. # 修改 git.htpasswd 文件的所有者与所属群组, 以及权限
  11. $ chown apache:apache /etc/httpd/conf.d/git.htpassd
  12. $ chmod 640 /etc/httpd/conf.d/git.htpassd

3 配置 httpd

修改 /etc/httpd/conf/httpd.conf 配置文件,末尾添加如下内容.

  1. <VirtualHost *:80>
  2. ServerName 106.xxxx.xxx # 服务器地址/域名
  3. SetEnv GIT_HTTP_EXPORT_ALL
  4. SetEnv GIT_PROJECT_ROOT /workspace/gitrepo/ # 代码存放路径
  5. ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/ # 以/git/开头的访问路径映射至git的CGI程序git-http-backend
  6. <Location />
  7. AuthType Basic
  8. AuthName "Git"
  9. AuthUserFile /etc/httpd/conf.d/git.htpasswd # 验证用户帐户的文件
  10. Require valid-user
  11. </Location>
  12. </VirtualHost>

4 启动服务

  1. $ systemctl enable httpd
  2. $ systemctl start httpd

5 测试

通过以上步骤,就可以使用 http 方式进行 clone 了.

  1. $ git clone http://***/git/sample.git
  2. Cloning into 'sample'...

附录C 将 git 上的项目 push 到自己的 repo

以 spring-framework 为例,当 clone 下这个项目后,改动代码,提交变更, 然后 push 的话会 push 到 github 上去。那么如果我们只想将代码 push 到自己的 git 仓库呢?其实很简单,只需要添加自己的远程仓库地址,在 push 是选择 push 到我们自己的仓库就行.

  1. $ git remote add bascker ssh://IP/root/workspace/gitrepo/spring-framework.git
  2. $ git remote -v
  3. origin https://github.com/spring-projects/spring-framework.git (fetch)
  4. origin https://github.com/spring-projects/spring-framework.git (push)
  5. bascker ssh://IP/root/workspace/gitrepo/spring-framework.git (fetch)
  6. bascker ssh://IP/root/workspace/gitrepo/spring-framework.git (push)

用小乌龟 push 时,选择目标 bascker 分支即可.

Git操作文档的更多相关文章

  1. GIT 操作文档

    https://git-scm.com/book/en/v2 安装git地址:https://git-scm.com/downloads 一.初始化设置 1.设置你用户名称与邮件地址(每一个 Git ...

  2. MongoDB(五):MongoDB操作文档

    本篇文章中将讲解如何使用MongoDB操作文档. 文档的数据结构和JSON基本一致,所有存储在集合中的数据都是BSON格式.BSON是一种类似json格式的一种二进制形式的存储格式,简称Binary ...

  3. pageoffice razor pageofficelink方式调用js实现操作文档

    用官方的开发者帮助文档即可,在后台实例化时一定要将pageofficecontrol控件的id属性定义,然后在前台通过定义的id属性来调用相应的方法

  4. 从域名到网站,快速创建全新社区站点 - phpwind一键部署操作文档

    关于phpwind一键部署服务,请查阅PW官网:http://www.phpwind.com/setup.html 选择一键部署镜像版本,立即开始使用: PW建站系统(Centos 64位) ———— ...

  5. MongoDB第四天(操作文档,添加,更新,查询以及对于日期的相关操作)

    添加文档: 添加单个文档,多个文档 package com.bjsxt.mongodbdemo; import com.mongodb.client.MongoCollection; import o ...

  6. ElasticSearch使用C#操作文档

    一.ElasticSearch的.net客户端驱动程序 ElasticSearch官方网站提供了两个.net客户端驱动程序,其中Elasticsearch.Net是一个非常底层且灵活的客户端驱动程序, ...

  7. Linux常用命令操作文档

    Ls命令:列出目录内容 选项 长选项 含义 -a --all 列出所有文件,包括隐藏的文件 -d --directory 指定一个目录 -F --classify 在每个列出的名字后面加上类型指示符( ...

  8. ElasticSearch 基础入门 and 操作索引 and 操作文档

    基本概念 索引: 类似于MySQL的表.索引的结构为全文搜索作准备,不存储原始的数据. 索引可以做分布式.每一个索引有一个或者多个分片 shard.每一个分片可以有多个副本 replica. 文档: ...

  9. lua的io操作文档

    2014-09-16~15:26:35 I/O库提供两种不同的方式进行文件处理1.io表调用方式:使用io表,io.open将返回指定文件的描述,并且所有的操作将围绕这个文件描述 io表同样提供三种预 ...

随机推荐

  1. layui中流加载layui.flow

    1.引入layui.css和layui.js 2. html中定义容器 <div id="demo"></div> js部分: layui.use('flo ...

  2. Map 实现类之一:HashMap

    Map 实现类之一:HashMapHashMap是 Map 接口 使用频率最高的实现类.允许使用null键和null值,与HashSet一样,不保证映射的顺序.所有的key构成的集合是Set:无 ...

  3. Git安装教程最新版本(国内gitee国外github)

    Git安装教程最新版本(国内gitee国外github) 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章, 关注回复「资源」, 获取大师使用的typora主题: http://w ...

  4. Docker Swarm(十)Portainer 集群可视化管理

    前言 搭建好我们的容器编排集群,那我们总不能日常的时候也在命令行进行操作,所以我们需要使用到一些可视化的工具,Docker图形化管理提供了很多工具,有Portainer.Docker UI.Shipy ...

  5. Docker Swarm(一)集群部署

    一.机器环境 机器规划 172.16.0.89 swarm的manager节点 manager-node 172.16.0.90 swarm的node节点 node1 机器版本(均是:CentOS L ...

  6. VMware(Caps Lock键)切换大小写作用失效的Bug的解决办法

    前言 第一种情况是:进入VMware虚拟机的时候,即使按了Capslock键开启大写,灯虽然亮了,但是,打出来的字母还是小写,没有有任何的效果,根本不能转换成大写. 只有按Shift+字母才能输入大写 ...

  7. 9.4-6 kill & killall & pkill

    kill:终止进程 能够终止你希望停止的进程. kill 命令的参数选项及说明 -l    列出全部的信号名称 -p    指定kill命令只打印相关进程的进程号,而不发送任何信号 -s    指定要 ...

  8. 在安装python 第三方库时遇到【WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, st】问题

    在命令执行窗输入: pip install Pyinstaller -i http://pypi.douban.com/simple --trusted-host pypi.douban.com (其 ...

  9. Linux命令学习—— fdisk -l 查看硬盘及分区信息

    Linux命令学习(3)-- fdisk -l 查看硬盘及分区信息注意:在使用fdisk命令时要加上sudo命令,否则什么也不能输出linux fdisk 命令和df区别是什么? fdisk工具是分区 ...

  10. 【三】Kubernetes学习笔记-Pod 生命周期与 Init C 介绍

    一.容器生命周期 Init C(初始化容器)只是用于 Pod 初始化的,不会一直随着 Pod 生命周期存在,Init C 在初始化完成之后就会死亡. 一个 Pod 可以有多个 Init C,也可以不需 ...