GIt

来源:互联网 发布:防小偷报警软件 编辑:程序博客网 时间:2024/06/06 00:41
首先来说一下git,git是源码管理工具.可以使用git init 创建一个仓库.git远程仓库和本地仓库没什么区别.  GitHub就是一个免费的托管开源代码库.如果让不想让代码开源,就需要搭建一个git服务器,git服务器本质就是一个裸的git仓库,就是没有工作区的仓库,只能够接受代码和提交记录.如何在买的云服务器上面搭建git服务器呢.


1首先root账户下,adduser git,创建一个git用户,


2创建证书登录:收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。


3选定一个目录做git仓库,假设选择的是 /srv/sample.git,在/srv目录下输入命令:sudo git init --bare sample.git,此时git就会创建一个没有工作区的裸仓库.


4.chown -R git:git sample.git 将git仓库的只给git用户.不让用户登录到服务器之间更改工作区.


5.禁用shell登录:


出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:


git:x:1001:1001:,,,:/home/git:/bin/bash
改为:


git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。


6.克隆远程仓库


$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
这样就搭建了一个git服务器..


如果项目共同搭建一个网站,项目提交的git仓库之后,可以写一个脚本自动拉取仓库里面代码,并且更新.在之前搭建的sample.git仓库目录下,cd hooks ,在这个文件夹下面,vim post-receive,写一个脚本,


 


#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".


set -e


git update-server-info


is_bare=$(git config --get --bool core.bare)


if [ -z "${is_bare}" ]
then
    # for compatibility's sake, guess
    git_dir_full=$(cd $GIT_DIR; pwd)
    case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi


update_wc() {
    ref=$1
    echo "Push to checked out branch $ref" >&2
    if [ ! -f ${GIT_DIR}/logs/HEAD ]
    then
        echo "E:push to non-bare repository requires a HEAD reflog" >&2
        exit 1
    fi
    if (cd ${GIT_WORK_TREE}; git diff-files -q --exit-code >/dev/null)
    then
        wc_dirty=0
    else
        echo "W:unstaged changes found in working copy" >&2
        wc_dirty=1
        desc="working copy"
    fi
    if git diff-index --cached HEAD@{1} >/dev/null
    then
        index_dirty=0
    else
        echo "W:uncommitted, staged changes found" >&2
        index_dirty=1
        if [ -n "$desc" ]
        then
            desc="$desc and index"
        else
            desc="index"
        fi
    fi
    if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
    then
        new=$(git rev-parse HEAD)
        echo "W:stashing dirty $desc - see git-stash(1)" >&2
        ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
        git update-ref --no-deref HEAD HEAD@{1}
        cd ${GIT_WORK_TREE}
        git stash save "dirty $desc before update to $new";
        git symbolic-ref HEAD "$ref"
        )
    fi


    # eye candy - show the WC updates :)
    echo "Updating working copy" >&2
    (cd ${GIT_WORK_TREE}
    git diff-index -R --name-status HEAD >&2
    git reset --hard HEAD
    # need to touch some files or restart the application? do that here:
    # touch *.wsgi
    )


}


if [ x"${is_bare}" = x"false" ]
then
    active_branch=$(git symbolic-ref HEAD)
    export GIT_DIR=$(cd ${GIT_DIR}; pwd)
    GIT_WORK_TREE="${GIT_DIR}/.."
    for ref in $(cat)
    do
        if [ x"$ref" = x"${active_branch}" ]
        then
            update_wc $ref
        fi
    done
fi
然后保存退出.chmod + x post-receive.


下面是git 使用出现过的一些错误.


我们拉取了远程服务器的代码后,在本地也会有一个git代码库.我们在本地项目上做过一些内容之后,使用git status可以查看修改过哪些文件,git add -A可以将改动的,需要提交的文件变成待提交状态,git commit -m '这里记录这一次提交做了什么',这次是提交到了本地仓库,git pull拉取git服务器里面代码(如果一个人写的,无需git pull),如果和你的代码有冲突,就需要合并,就会产生一次merge.可以通过gui工具让你解决冲突.需要merge的原因是,你改动的部分代码,其他人也改动了那部分代码.问你需要保存哪一部分.这样不会丢失本地代码,这样commit记录里面就会产生分支.如果不想产生分支,可以在git commit代码之前,git pull服务器上代码,拉取下来后,解决冲突,然后在commit,然后在push到服务器,如果对代码不是很熟悉,不建议这样做.因为你如果选择错了,代码就真的丢失了.无法挽回.


git stash建立一个暂存区,就是把你这次待做的事情暂存起来,你可以去做别的事情.解决完其他事情之后,可以继续处理这件事.这就类似操作系统的中断了,保持当前状态.


如果系统改动比较大,会创建一个分支,例如dev分支,在上面修改代码,再merge回master分支,这里需要注意的是,切换分支的时候,需要保证工作区干净,不然会就工作区的文件带过去,处理起来就有点麻烦.