Skip to content

Git 一些配置

用户信息

全局信息

设置个人信息

shell
git config --global user.name 'zhengxinonly'
git config --global user.email 'pyxxponly@gmail.com'

查看个人信息

shell
git config --global user.name
git config --global user.email

# 查看所有的配置
git config --global --list

git 配置存放位置,的全局配置一般会存在 home 目录的 .gitconfig 文件。如 /home/ubuntu/.gitconfig

可以通过以下命令查看全局配置的内容:

shell
cat ~/.gitconfig

局部信息

设置信息

shell
# 进入项目的根目录
cd [project_dir]

# 设置用户名和邮箱
git config user.name 'zhengxinonly'
git config user.email 'pyxxponly@gmail.com'

查看信息

shell
git config user.name
git config user.email

针对指定项目的配置会存在项目目录的 .git/config 文件。如 ./<project_dir>/.git/config

这个配置只针对你当前的项目有效,且优先级高于全局配置(如全局配置的 user.name 是 zhengxinonly,项目配置的 user.name 是 zhengxin,那么实际上你在这个项目中提交的 commit 的用户名是 zhengxin)。

git 设置多个上游分支

远程仓库到本地项目,这里首先就是三个平台远程仓库地址添加到本地,我们需要用 git remote add 分别执行

shell
git remote add github https://github.com/zhengxinonly/sample.git 
git remote add gitee https://gitee.com/zhengxinonly/sample.git 
git remote add gitea https://gitea.com/zhengxinonly/sample.git
shell
#!/bin/bash 
# 推送到 GitHub 
git push github main 
# 推送到 Gitee 
git push gitee main 
# 推送到 Gitea 
git push gitea main

git 由 HTTPS 切换为 ssh

服务器一般都采用公钥拉取代码,很少采用账号密码的方式

记录几条关键命令

  1. 查看当前仓库 URL 地址

    git remote -v

    这会列出所有远程仓库及其对应的 URL。例如,你可能会看到类似下面的输出:

    shell
    origin  https://gitee.com/username/repository.git (fetch)
    origin  https://gitee.com/username/repository.git (push)
  2. 复制项目的 ssh 地址,然后使用命令 git remote set-url 来重新设置 URL

    shell
    git remote set-url origin git@gitee.com:username/repository.git
  3. 用命令 git remote -v 查看一下,URL 是否已经变成了 ssh 地址

    shell
    git remote -v

    你应该看到类似下面的输出:

    shell
    origin  git@github.com:username/repository.git (fetch)
    origin  git@github.com:username/repository.git (push)
  4. 最后验证一下是否成功

    shell
    git pull