如何在Docker容器中使用Git?

在Docker容器中使用Git,可以通过以下步骤进行:

  1. 拉取包含Git的镜像:
docker pull ubuntu:20.04  # Ubuntu包含Git
docker pull centos:7       # CentOS包含Git
docker pull gitlab/gitlab-ce:latest  # GitLab CE
  1. 运行容器并进入交互模式:
docker run -it ubuntu:20.04 bash
  1. 设置Git用户名和邮箱:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  1. 创建SSH Key并添加到GitHub:
ssh-keygen -t rsa -b 4096 -C "you@example.com"
cat ~/.ssh/id_rsa.pub

复制SSH密钥,并在GitHub的 SSH and GPG keys页面中添加。

  1. 克隆GitHub仓库:
git clone git@github.com:username/repo-name.git
  1. 添加文件并推送到GitHub:
cd repo-name/
echo "Hello" > test.txt
git add . 
git commit -m "Add test.txt" 
git push
  1. 打标签并推送标签到GitHub:
git tag v1.0
git push origin v1.0