一、环境搭建
1、环境准备
centos 192.168.58.135
windows 192.168.58.2
2、环境安装
yum install gcc gcc-c++ make
[root@node1 ~]# yum install git
[root@node1 ~]# git --version
git version 1.8.3.1
二、本地代码库
1、本地仓库日常操作
#初始化本地仓库
[root@node1 git]# git init lwtz.git
Initialized empty Git repository in /home/git/lwtz.git/.git/
[root@node1 lwtz.git]# ls -a
. .. .git
[root@node1 lwtz.git]# ls .git/
branches config description HEAD hooks info objects refs
[root@node1 lwtz.git]# touch test
[root@node1 lwtz.git]# cat test
file rows
[root@node1 lwtz.git]# git status #查看的当前主分支暂存区信息
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test
nothing added to commit but untracked files present (use "git add" to track)
#版本提交
[root@node1 lwtz.git]# git add . #提交到暂存区
[root@node1 lwtz.git]# git commit -m 'v1' #提交到当前分支
[master (root-commit) 153d816] v1
1 file changed, 1 insertion(+)
create mode 100644 test
#查看本地库提交版本记录
[root@node1 lwtz.git]# git log
commit 153d816734329a21c6c4b87429cefd15ace82f8c
Author: pulin <cdutpulin@163.com>
Date: Fri Aug 18 09:53:10 2023 +0800
v1
[root@node1 lwtz.git]# git reflog
153d816 HEAD@{0}: commit (initial): v1
[root@node1 lwtz.git]# git blame test
^153d816 (pulin 2023-08-18 09:53:10 +0800 1) file rows
[root@node1 lwtz.git]# echo "test 2 rows" >>test
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
[root@node1 lwtz.git]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use "git add" and/or "git commit -a")
#查看当前文件修改前后变化
[root@node1 lwtz.git]# git diff test
diff --git a/test b/test
index 2410415..7dea803 100644
--- a/test
+++ b/test
@@ -1 +1,2 @@
file rows
+test 2 rows
[root@node1 lwtz.git]# git commit -am 'v2'
[master 230256e] v2
1 file changed, 1 insertion(+)
[root@node1 lwtz.git]# git reflog
230256e HEAD@{0}: commit: v2
153d816 HEAD@{1}: commit (initial): v1
#隐藏当前分支修改,处理别的事务,完成临时修改并提交。再恢复隐藏的修改
[root@node1 lwtz.git]# echo '3 rows' >> test
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
3 rows
[root@node1 lwtz.git]# git stash
Saved working directory and index state WIP on master: 230256e v2
HEAD is now at 230256e v2
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
[root@node1 lwtz.git]# git stash pop #等价命令git stash apply && git stash drop
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (606000ed8a83735082ad93b8016a88fabb698358)
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
3 rows
#版本回退
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
3 rows
[root@node1 lwtz.git]# git reflog
098cfaf HEAD@{0}: commit: v3
230256e HEAD@{1}: commit: v2
153d816 HEAD@{2}: commit (initial): v1
[root@node1 lwtz.git]# git reset --hard HEAD~1
HEAD is now at 230256e v2
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
#版本回退方法2
[root@node1 lwtz.git]# git reflog
230256e HEAD@{0}: reset: moving to HEAD~1
098cfaf HEAD@{1}: commit: v3
230256e HEAD@{2}: commit: v2
153d816 HEAD@{3}: commit (initial): v1
[root@node1 lwtz.git]# git reset --hard 098cfaf
HEAD is now at 098cfaf v3
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
3 rows
[root@node1 lwtz.git]# git reflog
098cfaf HEAD@{0}: reset: moving to 098cfaf
230256e HEAD@{1}: reset: moving to HEAD~1
098cfaf HEAD@{2}: commit: v3
230256e HEAD@{3}: commit: v2
153d816 HEAD@{4}: commit (initial): v1
#撤销当前修改和删除操作(未提交)
[root@node1 lwtz.git]# echo '4 rows' >>test
[root@node1 lwtz.git]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 lwtz.git]# git checkout --
.git/ test
[root@node1 lwtz.git]# git checkout -- test
[root@node1 lwtz.git]# git status
# On branch master
nothing to commit, working directory clean
[root@node1 lwtz.git]# cat test
file rows
test 2 rows
3 rows
#撤销删除文件
[root@node1 lwtz.git]# ls
test
[root@node1 lwtz.git]# rm test
rm: remove regular file ‘test’? y
[root@node1 lwtz.git]# ls
[root@node1 lwtz.git]# git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 lwtz.git]# git checkout -- test
[root@node1 lwtz.git]# ls
test
[root@node1 lwtz.git]# git status
# On branch master
nothing to commit, working directory clean
2、本地仓库分支操作
#新建分支并进入新建的分支内
[root@node1 lwtz.git]# git checkout -b kobe
Switched to a new branch 'kobe'
[root@node1 lwtz.git]# git branch
* kobe
master
[root@node1 lwtz.git]# ls
test
#创建分支(不进入新建分支)
[root@node1 lwtz.git]# git branch kobe1
[root@node1 lwtz.git]# git branch
kobe
kobe1
* master
#分支编辑内容并提交
[root@node1 lwtz.git]# echo 'kobe fenzhi' >>test
[root@node1 lwtz.git]# cat test
file rows
2 rows
kobe fenzhi
[root@node1 lwtz.git]# git status
# On branch kobe
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 lwtz.git]# git commit -a
Aborting commit due to empty commit message.
#切换到主分支
[root@node1 lwtz.git]# git branch #查看分支信息及所在分支
kobe
* kobe1
master
[root@node1 lwtz.git]# git checkout master
Switched to branch 'master'
[root@node1 lwtz.git]# git branch
kobe
kobe1
* master
[root@node1 lwtz.git]# cat test
file rows
2 rows
#将其他分支合并到当期主分支
[root@node1 lwtz.git]# git merge kobe
Updating 7706bb0..3d526c4
Fast-forward
test | 1 +
1 file changed, 1 insertion(+)
[root@node1 lwtz.git]# cat test
file rows
2 rows
kobe fenzhi
[root@node1 lwtz.git]# git branch
kobe
* master
#删除分支
[root@node1 lwtz.git]# git branch
kobe
kobe1
* master
[root@node1 lwtz.git]# git branch -d kobe1
Deleted branch kobe1 (was 3d526c4).
[root@node1 lwtz.git]# git branch
kobe
* master
3、远程连接阿里代码库
配置远程仓库
[root@node1 lwtz.git]# git remote add orign https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
[root@node1 lwtz.git]# git config -l
user.name=pulin
user.email=cdutpulin@163.com
core.bare=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.orign.url=https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
remote.orign.fetch=+refs/heads/*:refs/remotes/orign/*
远程交互数据
#远程仓库拉取数据
[root@node1 ts]# git pull orign master
Username for 'https://codeup.aliyun.com': cdutpulin
Password for 'https://cdutpulin@codeup.aliyun.com':
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Total 11 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (11/11), done.
From https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny
* branch master -> FETCH_HEAD
[root@node1 ts]# ls
123 readme.txt test
#远程仓库上传代码
[root@node1 ts]# echo 123456 > new
[root@node1 ts]# git add .
[root@node1 ts]# git commit -m 'v1'
[master df277f8] v1
1 file changed, 1 insertion(+)
create mode 100644 new
[root@node1 ts]# git status
# On branch master
nothing to commit, working directory clean
[root@node1 ts]# git reflog
df277f8 HEAD@{0}: commit: v1
da68743 HEAD@{1}: initial pull
[root@node1 ts]# git push orign master
Username for 'https://codeup.aliyun.com': cdutpulin
Password for 'https://cdutpulin@codeup.aliyun.com':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 319 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
da68743..df277f8 master -> master
SSH远程交互数据
- 生成秘钥
#生成ssh秘钥
[mike@node1 ts]$ ssh-keygen -t rsa -C "cdutpulin@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mike/.ssh/id_rsa):
Created directory '/home/mike/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/mike/.ssh/id_rsa.
Your public key has been saved in /home/mike/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZKLhj5ohdEEFxP/qlFZB5+9Gk5PxD7E8ouOMImM46DM cdutpulin@163.com
The key's randomart image is:
+---[RSA 2048]----+
| o+o. . . |
| .. . o |
| .o ..o. . . |
| ..+ +. . * o |
| . .o ..S O * |
|. . oo. + + + |
|o o .+o o o . |
|.E Boo + o |
|..B +... o |
+----[SHA256]-----+
[mike@node1 ts]$ ls ~/.ssh/
id_rsa id_rsa.pub
[mike@node1 ts]$
- 阿里远程代码库设置ssh信息
- 配置ssh远程信息及拉取数据
#配置信息
[root@node1 ts]# git remote add orign1 git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git
[root@node1 ts]# git config -l
user.name=pulin
user.email=cdutpulin@163.com
core.bare=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.orign.url=https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
remote.orign.fetch=+refs/heads/*:refs/remotes/orign/*
remote.orign1.url=git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git
remote.orign1.fetch=+refs/heads/*:refs/remotes/orign1/*
#上传数据
[root@node1 ts]# git push orign1 master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 341 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git
df277f8..99eca14 master -> master
#下载最新数据
[root@node1 ts]# cat new
git new data
[root@node1 ts]# git pull orign1 master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny
* branch master -> FETCH_HEAD
Updating da808f5..3b4c6cf
Fast-forward
new | 1 +
1 file changed, 1 insertion(+)
[root@node1 ts]# cat new
git new data
ali git new data
4、远程配置其他命令
[root@node1 ts]# git remote
orign
orign1
[root@node1 ts]# git remote -v
orign https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git (fetch)
orign https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git (push)
orign1 git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git (fetch)
orign1 git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git (push)#修改远程名称
[root@node1 ts]# git remote rename orign1 orign2
[root@node1 ts]# git remote -v
orign https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git (fetch)
orign https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git (push)
orign2 git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git (fetch)
orign2 git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git (push)
#修改远程地址信息[root@node1 ts]# git remote set-url orign https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
[root@node1 ts]# git remote show orign
Username for 'https://codeup.aliyun.com': cdutpulin
Password for 'https://cdutpulin@codeup.aliyun.com':
* remote orign
Fetch URL: https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
Push URL: https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
HEAD branch: master
Remote branches:
master tracked
ts new (next fetch will store in remotes/orign)
Local ref configured for 'git push':
master pushes to master (up to date)
[root@node1 ts]# git remote rm orign2
[root@node1 ts]# git config -l
user.name=pulin
user.email=cdutpulin@163.com
core.bare=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.orign.url=https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
remote.orign.fetch=+refs/heads/*:refs/remotes/orign/*
5、远程克隆仓库
[root@node1 ts]# git remote add orign1 it@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git
[root@node1 ts]# git config -l
user.name=pulin
user.email=cdutpulin@163.com
core.bare=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.orign.url=https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwny.git
remote.orign.fetch=+refs/heads/*:refs/remotes/orign/*
remote.orign1.url=git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git
remote.orign1.fetch=+refs/heads/*:refs/remotes/orign1/*
远程可浓
[root@node1 ts]# git clone git@codeup.aliyun.com:64dc46fd95afb811ce32016b/lwny.git
Cloning into 'lwny'...
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Total 23 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (23/23), done.
[root@node1 ts]# ls
123 bendi lwny master new readme.txt test
[root@node1 ts]# ls lwny/
123 bendi master new readme.txt test
四、搭建私有远程仓库
1、新建GIT仓库用户
#新建用户
[root@localhost /]# id git # 查看用户
[root@localhost /]# useradd git
[root@localhost /]# passwd git
2、创建远程仓库
#搭建远程仓库 git init --bare demo.git 搭建裸仓库,服务端无法查看代码库文件
[root@node1 git]# git init test.git
Initialized empty Git repository in /home/git/test.git/.git/
[root@node1 git]# ls test.git/ -aa
. .. .git
#需要修改仓库配置值
[root@node1 test.git]# git config receive.denyCurrentBranch 'ignore'
[root@node1 test.git]# git config -l
user.name=pulin
user.email=cdutpulin@163.com
core.bare=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
receive.denycurrentbranch=ignore
#若需要在服务端查看当前所在分支变化的数据
[root@node1 test.git]# git reset --hard
3、配置服务端ssh公钥信息
[root@node1 test.git]# vim /home/git/.ssh/authorized_keys
#如果存在多个客户端。公钥信息每行一个
[root@node1 test.git]# cat /home/git/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCYtQg8o6sW+f7zaNOBVJU5aegorgZ432lmj3jPyNNOsC0pO+9AbXSKOZzvDonok9zcu0BQ+x5QZMqq+Eq9wZ+mDHcTGyoxvuAJoEWllUrRPnP8lvZUfiP2Rl2xYjDt3XrZ1MVu0hL+FZHouZYDTyPORmCeRqATp+AeaVeWs405dWoNkgcBEI/MrRBwOB/2/QivY34nCds1zpFdj++BpO/kMcfLkrTz6aQ6rTfIku/6XQQyg0Ov3OK3e1od1p5+/cb8LRp/XhHe/j000yfFn6qkdTBGp+YkfORRCPq5mT7JZy7le2sV54+sDW8655/epST/G/BrI2eB86IyxUyEG503 root@Server
[root@node1 test.git]# chown 700 /home/git/.ssh/authorized_keys
[root@node1 test.git]# ls -l /home/git/.ssh
total 4
-rwx------ 1 700 git 393 Aug 18 15:35 authorized_keys
#打开 RSA 认证
vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
4、给远程仓库授予访问控制权限
[root@node1 test.git]# chown -R git.git /home/git/
[root@node1 test.git]# chmod 755 /home/git/test.git/
5、客户端上传和下载数据
[root@node2 test]# git config -l
user.name=lwtz
user.email=cdutpulin@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.orign.url=https://codeup.aliyun.com/64dc46fd95afb811ce32016b/lwtz.git
remote.orign.fetch=+refs/heads/*:refs/remotes/orign/*
branch.master.remote=node1
branch.master.merge=refs/heads/master
remote.node1.url=git@192.168.58.135:/home/git/test.git
remote.node1.fetch=+refs/heads/*:refs/remotes/node1/*
remote.node11.url=git@192.168.58.135:/home/git/git.git
remote.node11.fetch=+refs/heads/*:refs/remotes/node11/*
[root@node2 test]# git push node1 master
Counting objects: 85, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (66/66), done.
Writing objects: 100% (85/85), 6.83 KiB | 0 bytes/s, done.
Total 85 (delta 28), reused 0 (delta 0)
To git@192.168.58.135:/home/git/test.git
* [new branch] master -> master
#服务端查看上传结果
[root@node1 test.git]# git reset --hard
HEAD is now at 9cfab4b 67
[root@node1 test.git]# ls
123 cilect echo kobe kobe1 Readme README.md test
#服务端查修改并提交
[root@node1 test.git]# echo "siyou repository service"> server
[root@node1 test.git]# git add .
[root@node1 test.git]# git commit -m 'v2'
[master 5f805ee] v2
1 file changed, 1 insertion(+)
create mode 100644 server
[root@node1 test.git]# ls
123 cilect echo kobe kobe1 Readme README.md server test
#客户端下载数据
[root@node2 test]# git pull node1 master
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.58.135:/home/git/test
* branch master -> FETCH_HEAD
Updating 9cfab4b..5f805ee
Fast-forward
server | 1 +
1 file changed, 1 insertion(+)
create mode 100644 server
[root@node2 test]# ls
123 cilect echo kobe kobe1 Readme README.md server test
[root@node2 test]# cat server
siyou repository service