forgejo 仓库

使用

  • 本地仓库上传:Forgejo 提供了一个非常强大的特性叫 “推送到创建 (Push-to-Create)”,git push会自动创建仓库
  • 管理员添加用户(去掉强制更新密码)之后
  • 用户侧的网页后台:http://git.kc.com:3000    必须http,没开证书
  • 用户侧的git使用

    -------------------- 必须设置host --------------------------
    192.168.1.142 git.kc.com
    
    -------------------- 本地git仓库第一次使用-------------------
    git config --global user.name "YourName"        	# 确保 commit 记录有主 (否则 commit 会报错)
    git config --global user.email "you@example.com" 	# 确保邮箱正确
    git config --global core.autocrlf input         	# 防止 Windows/Linux 换行符冲突导致的代码变动
    
    -------------------- 远端空,第一次推本地项目 -------------------
    cd /your/local/project					# 本地项目目录
    git init								# 本地git初始化						
    git add .
    git commit -m "initial commit"
    git branch -M master					# 当前本地分支命名为master
    git remote remove origin 2>/dev/null	# 先清空关联
    git remote add origin http://git.kc.com:3000/用户名/预期仓库名.git		# 关联远端仓库
    git config credential.helper store		# 开启本地登录凭证存储,只需要执行一次
    git push -u origin master				# 推送并自动创建,会提示输入用户名密码
    
    ###############################################################################
    # Git 常用命令手册 (vm-alpine Forgejo 环境专用)
    ###############################################################################
    
    # --- 1. 基础配置 ---
    git config --global user.name "at"               	# 设置全局用户名
    git config --global user.email "at@kc.com"       	# 设置全局邮箱
    git config --global credential.helper store      	# 永久存储 HTTP 密码 (避免重复输入)
    git init                                         	# 初始化本地仓库
    
    # --- 2. 本地操作 (暂存区与提交) ---
    git status                                       	# 查看当前工作区状态 (哪些文件改了)
    git add .                                        	# 将所有修改添加到暂存区
    git commit -m "feat: initial commit"             	# 提交暂存区代码到本地仓库
    git commit --amend                               	# 修正最后一次提交 (改注记或补漏文件)
    
    # --- 3. 远端同步 (Remote & Push/Pull) ---
    git remote add origin http://at:pwd@git.kc.com/at/repo.git	# 关联远端 (带用户名密码)
    git remote set-url origin http://at:new@git.kc.com/at/repo.git	# 修改远端地址或密码
    git remote -v                                    	# 查看当前远程库地址
    git push -u origin master                        	# 首次推送并建立分支追踪 (触发自动建库)
    git push origin master                           	# 推送本地 master 分支到远端
    git pull origin master                           	# 拉取远端更新并合并到本地
    git fetch origin                                 	# 获取远端最新索引 (不合并)
    
    # --- 4. 分支管理 (Branching) ---
    git branch                                       	# 查看本地分支列表
    git checkout -b dev                              	# 创建并切换到 dev 分支
    git checkout master                              	# 切换回 master 分支
    git merge dev                                    	# 将 dev 分支的代码合并到当前分支
    git branch -d dev                                	# 删除已合并的本地分支
    
    # --- 5. 状态检查与日志 ---
    git log --oneline                                	# 查看简略提交历史 (一行一个)
    git log --graph --all                            	# 图形化查看分支演变历史
    git diff                                         	# 查看当前未暂存的修改细节
    git ls-remote origin                             	# 检查远端引用 (验证远端库是否存在)
    git reflog                                       	# 查看命令历史 (误删找回神器)
    
    # --- 6. 撤销与回滚 ---
    git checkout -- <file>                           	# 撤销某个文件的修改 (恢复到未改状态)
    git reset HEAD <file>                            	# 将文件从暂存区撤回工作区
    git reset --soft HEAD~1                          	# 撤销最近一次 commit,代码保留在暂存区
    git reset --hard HEAD~1                          	# 彻底撤销最近一次提交,代码不保留 (慎用!)
    git clean -fd                                    	# 删除工作区中所有未追踪的文件
  • 初始化示范

    (solara3142)D:\codeOC\omo\kc-tools-ppt>
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git init
    Initialized empty Git repository in D:/codeOC/omo/kc-tools-ppt/.git/
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git add .
    warning: in the working copy of '日程安排h01/KC整改执行方案.json', LF will be replaced by CRLF the next time Git touches it
    warning: in the working copy of '日程安排h01/任务梳理与日程安排.csv', LF will be replaced by CRLF the next time Git touches it
    ...
    warning: in the working copy of '路演报告设计/package.json', LF will be replaced by CRLF the next time Git touches it
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git commit -m "initial commit"
    [master (root-commit) 671f006] initial commit
     32 files changed, 13074 insertions(+)
     create mode 100644 "\346\227\245\347\250\213\345\256\211\346\216\222h01/KC\346\225\264\346\224\271\346\211\247\350\241\214\346\226\271\346\241\210.json"
     create mode 100644 "\346\227\245\347\250\213\345\256\211\346\216\222h01/\344\273\273\345\212\241\346\242\263\347\220\206\344\270\216\346\227\245\347\250\213\345\256\211\346\216\222.csv"
     ...
     create mode 100644 "\350\267\257\346\274\224\346\212\245\345\221\212\350\256\276\350\256\241/\345\244\232\347\255\226\347\225\245\346\212\225\350\265\204\347\220\206\345\277\265_\347\247\201\345\213\237\345\237\272\351\207\221_\346\212\225\350\265\204\346\274\224\347\244\272.pptx"
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git remote add origin http://git.kc.com:3000/at/kc-tools-ppt.git
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git config credential.helper store
    
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git push -u origin master
    Enumerating objects: 37, done.
    Counting objects: 100% (37/37), done.
    Delta compression using up to 16 threads
    Compressing objects: 100% (36/36), done.
    Writing objects: 100% (37/37), 189.01 KiB | 8.22 MiB/s, done.
    Total 37 (delta 8), reused 0 (delta 0), pack-reused 0
    To http://git.kc.com:3000/at/kc-tools-ppt.git
     * [new branch]      master -> master
    branch 'master' set up to track 'origin/master'.
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git branch -r
      origin/master
    
    (solara3142)D:\codeOC\omo\kc-tools-ppt>git log origin/master --oneline -n 5
    671f006 (HEAD -> master, origin/master) initial commit

目录

mkdir -p /www/git/data

docker-compose.yml

services:
  forgejo:
    image: codeberg.org/forgejo/forgejo:15.0.2-rootless
    container_name: git
    restart: unless-stopped
    networks:
      - ghost_net
    # rootless 镜像内部默认就是以 1000 运行,这里保持一致
    user: "1000:1000"
    environment:
      - USER_UID=1000
      - USER_GID=1000
      
      # --- 数据库连接 ---
      - FORGEJO__database__DB_TYPE=mysql
      - FORGEJO__database__HOST=mariadb:3306
      - FORGEJO__database__NAME=forgejo_db
      - FORGEJO__database__USER=ghost
      - FORGEJO__database__PASSWD=123456
      
      # --- 域名与 URL 修正 ---
      - FORGEJO__server__DOMAIN=git.kc.com
      - FORGEJO__server__ROOT_URL=http://git.kc.com
      
      # --- SSH 配置 (针对 Rootless) ---
      # 注意:rootless 容器内无法监听 22 端口,必须改为 >1024 的端口
      - FORGEJO__server__SSH_LISTEN_PORT=2222
      - FORGEJO__server__SSH_PORT=2222
      # 建议开启:允许通过 HTTP 认证进行操作
      - FORGEJO__auth__DISABLE_BASIC_AUTHENTICATION=false
      
      # 禁用密码复杂度检查(设置为 off)
      - FORGEJO__security__PASSWORD_COMPLEXITY=off
      # 将最小密码长度设置为 1 (Forgejo 不允许完全禁用,但设为 1 即相当于无限制)
      - FORGEJO__security__MIN_PASSWORD_LENGTH=1
      
      # 开启用户推送到创建功能
      - FORGEJO__repository__ENABLE_PUSH_CREATE_USER=true
      # 如果你也想在组织中自动创建,可以开启下面这个(可选)
      - FORGEJO__repository__ENABLE_PUSH_CREATE_ORG=true
      
    volumes:
      - ./data:/var/lib/gitea	# rootless版本映射路径
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "2222:2222"          # 宿主机 2222 映射容器内 2222
      - "3000:3000"          # HTTP 端口
      
networks:
  ghost_net:
    external: true

迁移手册