发新帖

[Git] git 安装及使用

零下一度 2020-9-18 1295

一、官网托管的方式

…or create a new repository on the command line
echo "# demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/demo/demo.git
git push -u origin master
                
…or push an existing repository from the command line
git remote add origin https://github.com/demo/demo.git
git branch -M master
git push -u origin master
…or import code from another repository
You can initialize this repository with code from a Subversion, Mercurial, or TFS project.


git下载地址:https://git-scm.com/downloads

wget --no-check-certificate https://www.kernel.org/pub/software/scm/git/git-2.28.0.tar.gz
tar zxvf git-2.28.0.tar.gz 
cd git-2.28.0
make configure
./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
make all doc
make install install-doc install-html
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc

查看版本号

git --version

卸载

yum remove git  -y


//移除源

git remote rm origin


二、git服务端安装,安装自有git服务器

1. 检查系统上是否已经安装git,若已有则卸载

// 查看当前git版本

 git --version

// 卸载旧版本

yum remove -y git

2. 安装依赖包,下载最新版本git源码

yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
wget https://github.com/git/git/archive/v2.13.2.tar.gz
tar zxf v2.13.2.tar.gz

3. 安装git,配置环境变量

cd git-2.13.2
make prefix=/usr/local/git all
make prefix=/usr/local/git install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc    // 实时生效
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
source /etc/profile    // 实时生效

4. 查看git版本号,正确显示则安装成功

git --version
git version 2.13.2

5. 若编译时报错如下

libgit.a(utf8.o): In function `reencode_string_iconv':

/usr/local/src/git-2.13.2/utf8.c:463: undefined reference to `libiconv'

libgit.a(utf8.o): In function `reencode_string_len':

/usr/local/src/git-2.13.2/utf8.c:524: undefined reference to `libiconv_open'

/usr/local/src/git-2.13.2/utf8.c:535: undefined reference to `libiconv_close'

/usr/local/src/git-2.13.2/utf8.c:529: undefined reference to `libiconv_open'

collect2: ld returned 1 exit status

make: *** [git-credential-store] Error 1

可以按照如下方式解决

// 对之前git的make 操作进行 make clean

make clean

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

tar zxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local/libiconv

make && make install

创建一个软链接到/usr/lib

ln -s /usr/local/lib/libiconv.so /usr/lib
ln -s /usr/local/lib/libiconv.so.2 /usr/lib

然后 

make configure
./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv/
make && make install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc

出现如下错误

 * new build flags

    CC fuzz-pack-headers.o

    CC fuzz-pack-idx.o

……

    SUBDIR templates

    MSGFMT po/build/locale/bg/LC_MESSAGES/git.mo

/bin/sh: msgfmt: command not found

make: *** [po/build/locale/bg/LC_MESSAGES/git.mo] Error 127 

执行

yum install -y gettext-devel


// 对之前git的make 操作进行 make clean ,再次安装

done && \
remote_curl_aliases="git-remote-https git-remote-ftp git-remote-ftps" && \
for p in $remote_curl_aliases; do \
	rm -f "$execdir/$p" && \
	test -n "" && \
	ln -s "git-remote-http" "$execdir/$p" || \
	{ test -z "" && \
	  ln "$execdir/git-remote-http" "$execdir/$p" 2>/dev/null || \
	  ln -s "git-remote-http" "$execdir/$p" 2>/dev/null || \
	  cp "$execdir/git-remote-http" "$execdir/$p" || exit; } \
done && \
./check_bindir "z$bindir" "z$execdir" "$bindir/git-add"

安装成功


三、服务器设置

1、添加一个git用户,为安全起见禁用shell登录

useradd git
usermod -s git-shell git

创建git用户组

groupadd git  
usermod -G git git
chgrp -R git /demo_path/git
chmod 777 /demo_path/git -Rf

usermod -G groupname username (这种会把用户从其他组中去掉,只属于该组)

如:usermod -G git git (git只属于git组)

usermod -a -G groupname username (把用户添加到这个组,之前所属组不影响)

usermod -a -G git xiaofei

如:usermod -a -G www git (git属于之前git组,也属于www组)


// 为安全起见,禁用 git 用户的 shell 登录

vim /etc/passwd

// 修改 git 用户的 shell 为 git-shell,路径使用 which git-shell 查看

// 找到如下一行

git:x:1001:1001::/home/git:/bin/bash

// 修改成如下

git:x:1001:1001::/home/git:/usr/local/git/bin/git-shell

2、初始化一个项目目录为一个仓库

// 进入到项目目录

git init 
mkdir -p /DATA/asmdata1/git/MonitorFalls
git init --bare /DATA/asmdata1/git/MonitorFalls

3、将该目录克隆成为裸仓库(作为中介)并设置裸仓库所有者为git用户

// 创建一个裸仓库

git clone --bare /demo_path/git/MonitorFalls MonitorFalls.git

4、将裸仓库添加为远程仓库

进入到项目目录

git remote add origin /demo_path/git/MonitorFalls MonitorFalls.git

// 修改远程仓库所属主和所属组

chown -R git.git /srv/myProject.git

5、将服务器上的项目添加到远程仓库即 /srv/myProject.git

git add .
git commit -m 'comment'
git push origin master

6、在该用户的家目录下面保存本地用户的公钥

su git
cd ~
mkdir .ssh && chmod 700 .ssh
cd .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

 然后在 authorized_keys 文件中加入本地用户的公钥 id_rsa.pub

7、打开git服务器的RSA认证

vim /etc/ssh/sshd_config

// 找到下面3行并去掉注释

1. RSAAuthentication yes     

 2. PubkeyAuthentication yes     

 3. AuthorizedKeysFile  .ssh/authorized_keys

 重启:systemctl restart sshd.service


四、本地设置(Windows端)

1. 配置git用户名及邮箱

git config --global user.name 'your name'
git config --global user.email 'your email address'


2、 生成公钥并交给服务端

// 打开 git-bash,生成公钥

ssh-keygen -t rsa
ssh-keygen -t rsa -C "xiaofei@demo.com"

在用户目录下的.ssh目录里面会生成 id_rsa, id_rsa.pub

将 id_rsa.pub 里的内容拷贝到服务器上


3、 在保存公钥的同级目录下面新建config文件指定ssh端口(可选)

==类似使用搬瓦工VPS的可能默认ssh端口不是22的需要配置这个==

Host your domain name or server ip
User 服务端添加的git用户名
Hostname your domain name or server ip
Port ssh 端口
IdentityFile ~/.ssh/id_rsa

4、从服务器克隆项目到本地

git clone admin@demo.com:/demo_path/git/MonitorFalls.git

5、新建一个分支

git branch MonitorFalls_jf

检查分支是否创建成功

git branch

然后切换到你的新分支

git checkout newbranch

然后重新提交到新分支上

git add .
git commit -m "添加xxx"

然后切换到主分支

git checkout master

然后将新分支提交的改动合并到主分支上 

git merge newbranch

然后就可以push代码了

git push -u origin master

最后还可以删除这个分支

git branch -D newbranch


提交代码报一下错误,解决方法配置系统hosts文件  :

The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have t
13.250.177.223 github.com


解决 fatal: unable to access '***': SSL connect error

yum update nss



更详细的git服务器及权限的配置:

https://www.jianshu.com/p/a0eb79fa5b8d



最新回复 (0)
返回
零下一度
主题数
931
帖子数
0
注册排名
1