在服务器上创建git仓库

环境准备

服务器:阿里云轻量应用服务器

系统版本:ubuntu20.04.1 LTS

安装git

检查git是否安装

1
git

如果出现以下信息,说明安装了git

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
restore Restore working tree files
rm Remove files from the working tree and from the index
sparse-checkout Initialize and modify the sparse-checkout

examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
diff Show changes between commits, commit and working tree, etc
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status

grow, mark and tweak your common history
branch List, create, or delete branches
commit Record changes to the repository
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
reset Reset current HEAD to the specified state
switch Switch branches
tag Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

如果是其他报错信息,也没关系,我们进行git的安装

1
sudo apt-get install git

创建git用户

1
adduser git

切换到git用户, 带-表示切换并跳转目录到家目录

1
su - git

配置公钥

创建目录.ssh和文件authorization_keys

1
mkdir .ssh
1
2
cd .ssh
touch authorization_keys

将windows中的公钥上传至服务器

image-20221130205807626

然后写入authorization_key文件中

1
cat id_rsa.pub >> authorized_keys

sshauthorized_keys添加权限

1
2
chmod 700 .ssh
chmod 700 .authorized_keys

创建repositories目录

这个目录的主要作用是存放我们的git仓库文件

1
mkdir repositories

测试ssh免密登录

1
ssh git@你的服务器ip

如果可以免密登录,就说明免密配置成功了

测试push

在repositories目录创建test.git

1
2
cd repositories
mkdir test

初始化仓库

1
2
cd test.git
git init

同时在windows中也创建一个代码仓库test.git

并在仓库中添加一个hello.txt文件,内容如下

image-20221130212353817

然后在windows的git中执行如下命令

1
2
3
4
git add .
git commit -m "第一次测试"
git remote add origin git@你的服务器地址:/home/git/repositories/test.git(你的仓库所在位置)
git push -u origin master

出现错误

push失败

image-20221130222315736

1
git config receive.denyCurrentBranch ignore

参考文章:git push时出现错误refusing to update checked out branch: refs/heads/master_偏头痛时喝可乐的博客-CSDN博客_error: branch refs/heads/master:

这时,config配置后面多了一条

image-20221130223212773

服务器上没有看到push后的文件

这类问题就是push到服务器上之后,没有看到push后的内容,但是log里面却有提交记录

image-20221130222923538

解决办法

1
git reset --hard

参考文章

Git push 出错以及server端没有显示push后的文件的解决方法_R先森的博客-CSDN博客

然后查看hello.txt文件内容

image-20221130223320538

一切正常,nice!