【git】服务器的构建

1 git服务器搭建文档
① 安装 Git
② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码
③ 服务器端创建 Git 仓库
④ 客户端 clone 远程仓库
⑤ 客户端创建 SSH 公钥和私钥
⑥ 服务器端 Git 打开 RSA 认证
⑦ 将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件
⑧ 客户端再次 clone 远程仓库
⑨ 禁止 git 用户 ssh 登录服务器

1 上传文件
git-2.8.3.tar.gz

2 解压文件
tar -zxvf git-2.8.3.tar.gz

3 进入git目录下进行编译
cd git-2.8.3

4 进行编译安装
[root@localhost git-2.8.3]# make prefix=/usr/common/git
[root@localhost git-2.8.3]# make prefix=/usr/common/git install


5 配置环境变量
[root@localhost git]# vim /etc/pofile
export PATH=/usr/common/git/bin:$PATH

6 重新激活一下邮件
[root@localhost git]# source /etc/profile

7 此时已经可以在服务器端敲命令了 测试
[root@localhost git]# git --version
git version 2.8.3

8 服务端创建git用户 用来管理git服务 并为git用户设置密码
[root@bogon project1]# id git
id: git: No such user
[root@bogon project1]# useradd git
[root@bogon project1]# passwd git

9 服务器端创建git仓库 设置 /home/data/git/gittest.git为Git仓库
然后把Git仓库的owner修改为git

10 创建git仓库
[root@bogon git]# mkdir -p data/git/gittest.git
[root@bogon git]# git init --bare data/git/gittest.git
Initialized empty Git repository in /home/git/data/git/gittest.git/
[root@bogon git]# cd data/git/
[root@bogon git]# chown -R git:git gittest.git/

此时服务器端已经构建好了


11 客户端从Linux Git服务器端clone项目到本地
$ git clone git@192.168.232.131:/home/data/gittest.git
如果SSH用的不是默认的22端口,则需要使用以下的命令(假设SSH端口号是7700):
$ git clone ssh://git@192.168.232.131:22/home/data/gittest.git

12 当第一次连接到目标 Git 服务器时会得到一个提示:
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.
Are you sure you want to continue connecting (yes/no)?
选择 yes:
Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.
如果不是提示这个,可以到administrator/目录下删除.ssh目录

13 此时C:\Users\Administrator\.ssh目录下会出现known_hosts文件
以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。
后面提示要输入密码,可以采用 SSH 公钥来进行验证。

14 客户端创建ssh公钥
ssh-keygen -t rsa -C "894154899@qq.com"

此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub
id_rsa 是私钥
id_rsa.pub 是公钥


15  服务器端 Git 打开 RSA 认证
进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存并重启 sshd 服务:

[root@localhost ssh]# /etc/rc.d/init.d/sshd restart
由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,
由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys

在 /home/git/ 下创建目录 .ssh

[root@localhost git]# pwd
/home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh

然后把 .ssh 文件夹的 owner 修改为 git
复制代码

[root@localhost git]# chown -R git:git .ssh
[root@localhost git]# ll -a
总用量 32
drwx------. 5 git  git  4096 8月  28 20:04 .
drwxr-xr-x. 8 root root 4096 8月  28 19:32 ..
-rw-r--r--. 1 git  git    18 10月 16 2014 .bash_logout
-rw-r--r--. 1 git  git   176 10月 16 2014 .bash_profile
-rw-r--r--. 1 git  git   124 10月 16 2014 .bashrc
drwxr-xr-x. 2 git  git  4096 11月 12 2010 .gnome2
drwxr-xr-x. 4 git  git  4096 5月   8 12:22 .mozilla
drwxr-xr-x. 2 git  git  4096 8月  28 20:08 .ssh


16 将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件
回到 Git Bash 下,导入文件:此时ssh-key已经导入服务器端了
Administrator@PC-201709252329 MINGW64 ~
$ ssh git@192.168.232.131 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
git@192.168.232.131's password

17 回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:
[root@localhost git]# cd .ssh
[root@localhost .ssh]# ll
总用量 4
-rw-rw-r--. 1 git git 398 8月  28 20:08 authorized_keys
可以查看一下是否是客户端生成的公钥。


18 修改 .ssh 目录的权限为 700
修改 .ssh/authorized_keys 文件的权限为 600
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 600 authorized_keys


19 客户端再次 clone 远程仓库
git clone git@192.168.232.131:/home/data/git/gittest.git
git clone ssh://git@192.168.232.131:22/home/data/git/gittest.git


20 可能报错
$ git clone root@192.168.232.131:/home/data/git/gittest.git
Cloning into 'gittest'...
root@192.168.232.131's password:
Permission denied, please try again.
root@192.168.232.131's password:
bash: git-upload-pack: command not found
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

原因git安装不在服务器的默认安装路径下/usr/bin/git-upload-pack 所以要建立软连接
服务器端找到git的安装路径

 ln -s /usr/common/git/bin/git-upload-pack /usr/bin/git-upload-pack
 ln -s /usr/common/git/bin/git-receive-pack /usr/bin/git-receive-pack
此时就可以git clone 成功了


 
21 可能报错
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=<remote>/<branch> master

git本地新建一个分支后,必须要做远程分支关联。如果没有关联,
git会在下面的操作中提示你显示的添加关联。
关联目的是如果在本地分支下操作: git pull, git push ,
不需要指定在命令行指定远程的分支.推送到远程分支后,
你只要没有显示指定,git pull的时候,就会提示你,立连接。


22 可能报错
error: src refspec master does not match any.
error: failed to push some refs to 'git@github.com:hahaha/ftpmanage.git'
出现这个的原因是因为仓库是空的,所示要在仓库里添加文件


23 此时客户端已经可以用了
Administrator@PC-201709252329 MINGW64 /e/newre/gittest (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        1.txt

nothing added to commit but untracked files present (use "git add" to track)

Administrator@PC-201709252329 MINGW64 /e/newre/gittest (master)
$ git add *
Administrator@PC-201709252329 MINGW64 /e/newre/gittest (master)
$ git commit -m "A"
[master (root-commit) ddea193] A
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1.txt


Administrator@PC-201709252329 MINGW64 /e/newre/gittest (master)
$ git pull origin master
fatal: Couldn't find remote ref master
fatal: The remote end hung up unexpectedly

Administrator@PC-201709252329 MINGW64 /e/newre/gittest (master)
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 199 bytes | 49.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 192.168.232.131:/home/data/git/gittest.git
 * [new branch]      master -> master

Administrator@PC-201709252329 MINGW64 /e/newre/gittest (master)
$ git pull
Already up to date.

24 客户端冲突解决

Administrator@PC-201709252329 MINGW64 /e/project1/gittest (master)
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.232.131:/home/data/git/gittest
   ddea193..2662d23  master     -> origin/master
Auto-merging 1.txt
CONFLICT (content): Merge conflict in 1.txt
Automatic merge failed; fix conflicts and then commit the result.

Administrator@PC-201709252329 MINGW64 /e/project1/gittest (master|MERGING)

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .1.txt.swp

no changes added to commit (use "git add" and/or "git commit -a")

25 冲突解决步骤

1 查看冲突 git diff

2 编辑冲突文件

<<<<<<< HEAD

=======
1111
>>>>>>> 2662d23f20c204bed4b04c5fd783eb6e6d5afc3e


如上所示冲突文件,你本地是head加了一个回车,你的同事提交了1111所以你要对这个文件进行冲突处理

在你本地修改这个冲突文件 ,修改完成以后,把文件添加到暂存区

Administrator@PC-201709252329 MINGW64 /e/project1/gittest (master|MERGING)
$ git add 1.txt
warning: LF will be replaced by CRLF in 1.txt.
The file will have its original line endings in your working directory.

Administrator@PC-201709252329 MINGW64 /e/project1/gittest (master|MERGING)
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        modified:   1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .1.txt.swp


Administrator@PC-201709252329 MINGW64 /e/project1/gittest (master|MERGING)
$ git commit -m "aa"
[master 26b1d4f] aa

Administrator@PC-201709252329 MINGW64 /e/project1/gittest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .1.txt.swp

nothing added to commit but untracked files present (use "git add" to track)

此时冲突已经解决完成了,已经不提示有冲突了,此时可以推送远程了

产生冲突的原因分析,你修改了文件提交到了本地仓库,你的同事修改了

同一个文件提交到了远程仓库,此时你pull或者fetch,merge的时候,文件

合并的时候就产生了冲突,所以为了尽量避免冲突的产生,你的本地修改的

代码,如果可以的情况下,

解决方案    本地未提交,可以放到暂存区 提交的时候

git pull 更新最新的代码

git add 添加自己修改的代码

git commit -m "d" 提交自己的代码到本地仓促

git push 再推送到远程

26 分支的作用 

由于你正在执行开发任务,突然来了一个bug 那么就意味着你要停止开发任务,去解决bug并提交,或者你本身有两个任务是

同事进行开发的,然而中途可能某一个任务优先级比较高,要尽快提交,又不能丢了另一个任务

解决方案

1 为新来的bug任务创建分支 git fetch origin master:tempbug

2 提交当前正在开发的任务到本地仓库,或者放入暂存区,然后切换到新分支

方法一

    git add *   //添加到暂存区

    git commit -m "need to be reset" //提交到本地仓库

    git checkout tempbug //切换到需要解决bug的目录

方法二

   git add *  //添加到暂存区

   git stash save "aa"  //保存到暂存区临时存储

   git checkout tempbug //切换到需要解决bug的目录

3 在tempbug分支上 解决完bug以后

git pull

git add *

git commit -m "tempbug resoleve"

git push

4 切换回原来的分支

git checkout master

方法一

git log -10  查看最近的提交记录

git reset headID(根据日志找到提交的id)

方法二

git stash list 查看你提交的暂存

git stash pop stashId(根据stash list查出来) 把你的暂存出栈

此时就可以继续你手头的工作了

如下是本人对Git整个理解的讲述 希望大家多多关注

CSDN程序员研修院

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值