关于Git

Git安装与版本库创建

  • sudo apt-get install git

安装后的配置

  • git config --global user.name"username"
  • git config --global user.email “xxxxxxxx@xxxx.com”

创建版本库

 版本库又名仓库,英文名repository
你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。

  • 第一步
1.mkdir git
2.cd git
3.pwd 查看所在路径
  • 第二步
    通过该指令将这个目录变为git可以管理,仓库建好了,并且这是一个空的仓库。
1.git init

输入指令 ls -ah ,可以看见隐藏文件夹,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。

也不一定必须在空目录下创建Git仓库,选择一个已经有东西的目录也是可以的。不过,不建议你使用自己正在开发的公司项目来学习Git,否则造成的一切后果概不负责。

将文件添加到版本库

  • 首先
    编写一个reame.txt文件,内容如下
1. Git is a version control system
2. Git is free software

一定要放在learnGit目录或子目录下。 因为learnGit是一个Git仓库,git找文件也是在仓库中找。

  • 第一步
    使用-git add指令将文件添加到仓库
git add readme.txt
  • 第二步
    使用-git commit指令。将文件提交给仓库 ( -m 表示备注信息)
git commit -m “wrote a readme file”

当提交成功后会提示你:
1.file changed:一个文件被改动
2.insertions:插入了两行内容

追加本地仓库到远程仓库

  • git remote add origin ssh://xxx@ip:port/GSFileParse(前提是对应存在父目录)
  • git pull origin master
  • git push -u origin master
  • 在本地仓库添加一个远程仓库,并将本地的master分支跟踪到远程分支
    追加仓库
  • 关于本地仓库的取消
ls -a
rm .git

注:Git命令必须在Git仓库目录内执行(git init除外),在仓库目录外执行是没有意义的。

Git 版本管理(更新与回退)

修改文件,之前创建的文件中,现在进行更新内容``

可通过git status指令查看仓库当前状态

1.Git is a version control system.
2.Git is free software.The 1st update

查看修改内容

使用-git diff指令,从输出我们可以看到,在最后一行我们添加了一段文字

更新并提交

通过使用- git diff可以查看到修改内容后可以更加安心的提交代码了。

  • 第一步 git add
    无任何输出说明没有问题,在执行git commit之前,可以使用git status看看当前仓库状态
  • 第二步 git commit
1. git commit -m “the 1st update”

最后使用git status查看状态,会提示nothing to commit,working directory clean,当前没有需要提交的修改,工作目录是干净的。

版本回退

通过指令查看更新历史

  • 通过git log指令查看,如果嫌输出信息太多则可以加上--pretty=omline参数:
1. git log --pretty=oneline

版本回退

如果想要跳转则需要知道当前是哪个版本
在Git中:

  • HEAD表示当前版本
  • HEAD^表示上一个版本
  • HEAD^^表示上上版本
  • HEAD-100表示上100个版本

在这里插入图片描述

现在,我们将版本回退到上一版本,就可以使用下面指令:

1.git reset --head HEAD^

–hard参数有何意义?我们放到后面再讲,现在请放心使用

再看看readme.txt里的内容,已经被还原

1.cat readme.txt

版本回退撤销

如果回退之后后悔了,则先查看 git log发现第二次更新消失了,如果窗口未关闭可以往上找 commit id 可以发现之前的最新版本commit id 是在这里5aa1503e270a325654e435872e12c2769253e6e5

则使用git reset --hard 版本号(版本无需写全写几位就好了,但是太少也不行)

git reset --hard 5aa15

此时再用cat readme .txt可查看到已经复原了。

  • 如果找不到commit id怎么办?
    使用git提供的git reflog的记录指令,可以看到版本号,则可以使用git reset --hard 版本号进行复原

在这里插入图片描述== git跟踪并管理的是修改,而非文件。如果每次没有用 git add 放到缓存区,那么就不会加入到commit中==

Git提交

// vi ~/.bashrc 
alias mastergit='git push origin HEAD:refs/for/master'
alias xxcpe='git push origin HEAD:refs/for/XXXX'

Git 撤销修改

错误并未 commit

  • 使用 git checkout -- file来丢弃工作区的修改,让文件回到最近一次的git commit或者git add状态。
1.git checkout -- readme.txt

回车后中断未打印信息,没有问题。

错误已被git add

不小心添加到了缓存区,但是还没有提交
我们可以看到git给我们的提示:使用git reset HEAD file把缓存区的修改撤销掉,重新放回工作区

1.git reset HEAD readme.txt
2.git status
3.git checkout -- readme.txt

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

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

场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。

参考原文:https://blog.csdn.net/qq_28258885/article/details/115009876

Gerrit 配置

Gerrit搭建自己的仓库参考文档
搭建自己的Gerrit

  • gerrit
创建新工程:
ssh -p 29418 user@ip gerrit create-object --empty-commit --name project_name
创建新分支
git checkout -b name_of_branch origin/xxx

重复提交

  • gedit …/.git/hooks/commit-msg
#!/bin/sh
#
# Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
#
# 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"
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

        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

        # 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 "#" without any spaces before it.
        /^#/ { 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 "\n";
                }
                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 "\n"
                        lines = ""
                }
                changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
                numlines = split(lines, footer, "\n")
                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
  • chmod u+x …/.git/hooks/commit-msg
  • 设置完成之后git commit --amend 无需添加changeId了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值