摘自 Linux Shell 脚本攻略 第六章 仓储管理
简介大多数Linux发行版中都已经包含了Git。如果你的系统中还没有安装,可以通过yum(Redhat 或SuSE)或apt-get(Debian或Ubuntu)获取
$ sudo yum install git-all $ sudo apt-get install git-all创建新的 git 仓库
如果你在开发自己的项目,那么可以创建对应的项目仓库。仓库可以创建在本地系统中,也 可以创建在如GitHub这样的远程站点上
git中的所有项目都需要有一个用于保存项目文件的主目录(master folder)$ mkdir MyProject $ cd MyProjectgit init命令会在当前工作目录下创建子目录.git并初始化git配置文件
$ git init如果你想让远程用户也能够访问这个仓库
$ git update-server-info克隆远程 git 仓库 从已知的远程站点(如GitHub)克隆
$ git clone http://github.com/ProjectName从需要用户名和密码的站点(可能是你自己的服务器)克隆
$ git clone clif@172.16.183.130:gitTest clif@172.16.183.130's password:使用 git 添加与提交变更 git add命令可以将工作代码(working code)中的变更添加到暂存区
该命令并不会改变仓库内容,它只是标记出此次变更,将其加入下一次提交中
$ vim SomeFile.sh $ git add SomeFile.sh
也可以一次添加多个文件
$ git add *.cgit commit命令可以将变更提交至仓库
$ vim OtherFile.sh $ git add OtherFile.sh $ git commit
git commit 命令会打开shell环境变量EDITOR中定义好的编辑器,其中包含如下预生成的
文本
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
# Committer: ClifFlyntclif@cflynt.com
# On branch branch1
# Changes to be committed:
# (use “git reset HEAD …” to unstage)
# modified: SomeFile.sh
# modified: OtherFile.sh
输入注释信息之后,你所作出的变更就被保存在仓库的本地副本中了
可以利用-a和-m选项缩短add/commit操作的输入
- -a:在提交前加入新的代码
- -m:指定一条信息,不进入编辑器
git commit -am "Add and Commit all modified files."使用 git 创建与合并分支 切换到之前创建的分支
$ git checkout OldBranchName你可以使用checkout的选项-b来创建新的分支
$ git checkout -b MyBranchName Switched to a new branch 'MyBranchName'git branch命令可以查看分支
$ git branch * MyBranchName master
当前分支由星号(*)着重标出
示例创建了新分支,添加并提交过变更之后,切换回起始分支,然后使用git merge命令将变更 合并入新分支
$ git checkout originalBranch $ git checkout -b modsToOriginalBranch # 编辑,测试 $ git commit -a -m "Comment on modifications to originalBranch" $ git checkout originalBranch $ git merge modsToOriginalBranch如果合并完分支之后不再需要该分支,可以使用选项-d进行删除
$ git branch -d MyBranchName分享工作成果 制作补丁
format-patch命令会汇集你所作出的变更,创建一个或多个补丁文件。补丁文件名由数字、 描述以及.patch组成
以父分支名作为参数的format-patch子命令会生成当前分支的补丁文件
$ git checkout master $ git checkout -b newFeature # 编辑、添加并提交 $ git format-patch master 0001-Patch-add-new-feature-to-menu.patch 0002-Patch-support-new-feature-in-library.patch
你可以使用git log命令查看仓库中所有提交的日志
$ git log commit 82567395cb97876e50084fd29c93ccd3dfc9e558 Author: Clif FlyntDate: Thu Dec 15 13:38:28 2016 -0500 Fixed reported bug #1 commit 721b3fee54e73fd9752e951d7c9163282dcd66b7 Author: Clif Flynt Date: Thu Dec 15 13:36:12 2016 -0500 Created new feature
使用SHA1作为参数的git format-patch命令形式如下
$ git format-patch SHA1 # 你可以在命令中使用完整的SHA1字符串或是只使用其中不重复的起始部分 $ git format-patch 721b $ git format-patch 721b3fee54e73fd9752e951d7c9163282dcd66b7 # 也可以根据与当前位置的距离来标识某个快照,这可以通过选项-#来实现 # 下列命令会为主分支上的最近一次变更生成补丁文件 $ git format-patch -1 master # 下列命令会为bleedingEdge分支上最近的两次变更生成补丁文件 $ git format-patch -2 bleedingEdge选项–check可以测试补丁是否有效
$ git apply --check 0001-Patch-new-feature.patch error: patch failed: feature.txt:2 error: feature.txt: patch does not apply
如果通过了–check的测试,就可以使用git apply命令应用补丁了
$ git apply 0001-Patch-new-feature.patch推送分支 git push命令可以将分支推送到主线
$ git push origin MyBranchName
修改了现有分支后,你可能会接收到如下错误信息
- -remote:error:Refusing to update checked out branch: refs/heads/master
- -remote:error:By default, updating the current branch in a non-bare repotory
在这种情况下,需要将变更推送到远程的新分支上
$ git push origin master:NewBranchNameget fetch和git pull命令可以将数据从远程下载到本地仓库
要克隆的仓库名为origin
$ get fetch origin
下列命令可以从其他开发者仓库中获取数据
$ get fetch Username@Address:Projectgit pull命令会获取并合并变更到工作代码
$ git pull origin $ git pull Username@Address:Project检查 git 仓库状态
git status命令会输出项目的当前状态。它会告诉你当前所处分支、是否有未提交的变更 以及是否与origin仓库①保持同步
$ git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # # Changed but not updated: # se "git add..." to update what will be committed) # (use "git checkout -- ..." to discard changes in workingdirectory) # #modified: newFeature.tcl
# Your branch is ahead of ‘origin/master’ by 1 commit.
以下行说明文件已经修改,但尚未提交
#modified: newFeature.tcl gitconfig --global user.name "Your Name" gitconfig --global user.email you@example.com
如果用于提交的身份信息不对,可以使用下面的命令修正
git commit --amend --author='Your Name查看 git 历史记录 git log命令可以生成一份报告,帮助你了解项目的一系列变更' 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 testfile.txt
$ git log commit fa9ef725fe47a34ab8b4488a38db446c6d664f3e Author: Clif Flynt查找 bug git blame命令可以返回一个列表,其中包含提交的SHA、作者、提交日期以及提交信息的第一行Date: Fri Dec 16 20:58:40 2016 -0500 Fixed bug # 1234
$ git blame testGit.sh d5f62aa1 (Flynt 2016-12-07 09:41:52 -0500 1) Created testGit.sh 063d573b (Flynt 2016-12-07 09:47:19 -0500 2) Edited on master repo. 2ca12fbf (Flynt 2016-12-07 10:03:47 -0500 3) Edit created remotely and merged.git bisect命令找出 引发问题的提交
git bisect命令需要两个标识符,一个用于最近所知的好代码(the last known good code),
另一个用于坏代码(bad release)。bisect命令会找到位于好代码和坏代码之间的中间提交点以 供测试
# 将当前(有bug的)代码拉取进git仓库 $ git checkout buggyBranch # 初始化git bisect $ git bisect start # 将当前提交标记为bad $ git bisect bad # 将没有问题的提交标记为good # 拉取中间提交点进行测试 $ git bisect good v2.5 Bisecting: 3 revisions left to test after this (roughly 2 steps) [6832085b8d358285d9b033cbc6a521a0ffa12f54] New Feature # 编译并测试 # 标记为good或bad # 拉取下一个提交进行测试 $ git bisect good Bisecting: 1 revision left to test after this (roughly 1 step) [2ca12fbf1487cbcd0447cf9a924cc5c19f0debf9] Merged. Merge branch 'branch1'
git bisect命令能够找出好坏版本之间的中间版本。你可以构建并测试这个版本,然后重新 运行git bisect来标记出good或bad。接着git bisect再找出好坏版本之间另一个新的中间版本
快照标签git支持轻量标签(仅为快照打标签)以及注解标签
git标签仅在本地范围内有效。git push默认不会推送标签。要想把标签发送到origin仓库, 必须加上选项–tags
$ git push origin --tagsgit tag命令包括可以用于添加、删除和列出标签的选项
$ git tag release-1.0 release-1.0beta release-1.1
你可以通过添加标签名在当前检出中创建标签
$ git tag ReleaseCandidate-1
在git tag命令中加入指定提交的SHA-1
$ git tag ReleaseCandidate-1 # 在gittag命令中加入指定提交的SHA-1,就可以为该提交添加标签 $ git log --pretty=oneline 72f76f89601e25a2bf5bce59551be4475ae78972 Initialcheckin fecef725fe47a34ab8b4488a38db446c6d664f3e Added menu GUI ad606b8306d22f1175439e08d927419c73f4eaa9 Added menu functions 773fa3a914615556d172163bbda74ef832651ed5 Initial action buttons $ git tag menuComplete ad606b # 选项-a可以为标签加入注解: $ git tag -a tagWithExplanation # git会打开编辑器,创建注解 # 你可以在命令行中使用-m选项定义信息: $ git tag -a tagWithShortMessage -m "A short description" # 如果使用git show命令,会显示如下信息: $ git show tagWithShortMessage tag tagWithShortmessage Tagger: Clif Flynt提交信息规范Date: Fri Dec 23 09:58:19 2016 -0500 A short description ... # 选项-d可以删除标签 $ git tag tag1 tag2 tag3 $ git tag -d tag2 $ git tag tag2 tag3F
- 每行长度在72个字符左右。使用空行分隔段落。
- 第一行的长度应该保持在50个字符左右并总结出此次提交的原因。其内容应该足够具体, 不要泛泛而谈,要让用户一眼就能看明白做了什么
- 不要写成Fix bug,甚至是Fix bugzilla bug #1234,应该写作Remove silly messages that appear each April 1



