首页 > 技术文章 > gerrit简版教程

jimmy-muyuan 原文

设置public key

1.生成密钥:ssh-keygen -t rsa -C "xiaoming" 
2.查看是否已经有了ssh密钥:cd ~/.ssh

3.不知道为什么hook下面总是少个文件,需要copy个commit-msg到hook下面(最下面附上commit-msg脚本)

git clone ssh://xxx@gerrit.com:10101/demo.git 克隆代码

git checkout -b branch origin/branch  将远程branch分支检出到本地

git init  初始化一个git仓库

git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:

$ git branch dev
$ git checkout dev


git pull 拉取最新代码到本地

git branch 查看当前分支

git add -A 选择所有文件提交到暂存区

git commit -m "第一次提交代码"  提交代码到版本库

git push origin HEAD:refs/for/branch 提交代码到远程分支

git status 查询当前分支的改动状态

git merge dev  git merge命令用于合并指定分支到当前分支

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
Git用<<<<<<<=======>>>>>>>标记出不同分支的内容,我们修改如下后保存:

git branch -d dev 删除分支dev

git log --graph 可以查看分支合并图

git commit --amend  可以修改提交的文案,提示没有chanage-id的时候可以进去看看,copy个commit-msg,退出在进入便可看到chanage-id

git diff HEAD -- readme.txt  查看工作区和版本库里面最新版本的区别

git checkout -- readme.txt  其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

git reset HEAD readme.txt可以把暂存区的修改撤销掉(unstage),重新放回工作区,再输入git checkout -- readme.txt还原文件

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

git log  查看提交记录,结果如下

commit 292460fb714e10b152669b341e0aed77a06a8

git reset --hard 版本号(292460fb714e10b152669b341e0aed77a06a8) 回滚会丢失代码

 git reset --soft 版本号(292460fb714e10b152669b341e0aed77a06a8) 回滚不会丢失代码

git  reflog 查看版本记录, 结果如下

e53e1ea HEAD@{2}: commit: add demo

git reset HEAD@{2}

GIT提交规范:

1.格式:关键字+正文

2.关键字,固定以下几个

added(新增,简写add)

fixed(修复,简写fix)

changed(修改原来的业务逻辑,简写change或cg)

upgraded(优化或组件升级,简写upgrade或up) 

例如:“add hello world test demo”

#!/bin/sh
# From Gerrit Code Review 2.13
#
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

unset GREP_OPTIONS

CHANGE_ID_AFTER="Bug|Issue|Test|Feature|Fixes|Fixed"
MSG="$1"

# Check for, and add if missing, a unique Change-Id
#
add_ChangeId() {
clean_message=`sed -e '
/^diff --git .*/{
s///
q
}
/^Signed-off-by:/d
/^#/d
' "$MSG" | git stripspace`
if test -z "$clean_message"
then
return
fi

# Do not add Change-Id to temp commits
if echo "$clean_message" | head -1 | grep -q '^(fixup|squash)!'
then
return
fi

if test "false" = "`git config --bool --get gerrit.createChangeId`"
then
return
fi

# Does Change-Id: already exist? if so, exit (no change).
if grep -i '^Change-Id:' "$MSG" >/dev/null
then
return
fi

id=`_gen_ChangeId`
T="$MSG.tmp.$$"
AWK=awk
if [ -x /usr/xpg4/bin/awk ]; then
# Solaris AWK is just too broken
AWK=/usr/xpg4/bin/awk
fi

# Get core.commentChar from git config or use default symbol
commentChar=`git config --get core.commentChar`
commentChar=${commentChar:-#}

# How this works:
# - parse the commit message as (textLine+ blankLine*)*
# - assume textLine+ to be a footer until proven otherwise
# - exception: the first block is not footer (as it is the title)
# - read textLine+ into a variable
# - then count blankLines
# - once the next textLine appears, print textLine+ blankLine* as these
# aren't footer
# - in END, the last textLine+ block is available for footer parsing
$AWK '
BEGIN {
# while we start with the assumption that textLine+
# is a footer, the first block is not.
isFooter = 0
footerComment = 0
blankLines = 0
}

# Skip lines starting with commentChar without any spaces before it.
/^'"$commentChar"'/ { next }

# Skip the line starting with the diff command and everything after it,
# up to the end of the file, assuming it is only patch data.
# If more than one line before the diff was empty, strip all but one.
/^diff --git / {
blankLines = 0
while (getline) { }
next
}

# Count blank lines outside footer comments
/^$/ && (footerComment == 0) {
blankLines++
next
}

# Catch footer comment
/^[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
footerComment = 1
}

/]$/ && (footerComment == 1) {
footerComment = 2
}

# We have a non-blank line after blank lines. Handle this.
(blankLines > 0) {
print lines
for (i = 0; i < blankLines; i++) {
print ""
}

lines = ""
blankLines = 0
isFooter = 1
footerComment = 0
}

# Detect that the current block is not the footer
(footerComment == 0) && (!/^[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:///) {
isFooter = 0
}

{
# We need this information about the current last comment line
if (footerComment == 2) {
footerComment = 0
}
if (lines != "") {
lines = lines " ";
}
lines = lines $0
}

# Footer handling:
# If the last block is considered a footer, splice in the Change-Id at the
# right place.
# Look for the right place to inject Change-Id by considering
# CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
# then Change-Id, then everything else (eg. Signed-off-by:).
#
# Otherwise just print the last block, a new line and the Change-Id as a
# block of its own.
END {
unprinted = 1
if (isFooter == 0) {
print lines " "
lines = ""
}
changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
numlines = split(lines, footer, " ")
for (line = 1; line <= numlines; line++) {
if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
unprinted = 0
print "Change-Id: I'"$id"'"
}
print footer[line]
}
if (unprinted) {
print "Change-Id: I'"$id"'"
}
}' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
}
_gen_ChangeIdInput() {
echo "tree `git write-tree`"
if parent=`git rev-parse "HEAD^0" 2>/dev/null`
then
echo "parent $parent"
fi
echo "author `git var GIT_AUTHOR_IDENT`"
echo "committer `git var GIT_COMMITTER_IDENT`"
echo
printf '%s' "$clean_message"
}
_gen_ChangeId() {
_gen_ChangeIdInput |
git hash-object -t commit --stdin
}


add_ChangeId

推荐阅读