配置项目的git

本文介绍了如何在Git中编辑`.git/config`文件来管理多个来源的项目,包括设置远程仓库、分支操作、强制用远程仓库覆盖本地、以及解决提交权限问题的方法。
摘要由CSDN通过智能技术生成

只需要编辑项目根目录下的 .git/config 文件,其中 .git 为根目录下的子目录。当需要操作多个来源不同仓库的项目时,需要做这个设置,比如一个来自 github.com,一个来自私有仓库的。

% cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/eyjian/libmooon.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[user]
	name = eyjian
	email = eyjian@qq.com
  • 附:强制用远程仓库的覆盖本地
git fetch --all&&git reset --hard origin/BRANCH

# 示例 1
git fetch --all&&git reset --hard origin/main

# 示例 2
git fetch --all&&git reset --hard origin/master

如果是 Makefile 中,可如下书写:

fetch: # 强制用远程仓库的覆盖本地,运行时指定分支名,如:make fetch branch=main
	git fetch --all&&git reset --hard origin/$$branch
  • 分支名由main改为master
git branch # 显示所有分支(当前分支前会有一个星号“*”)
git checkout main # 切换到 main 分支
git branch -m main master # 将本地的 main 分支改名为 master
git push -u origin master # 将本地的 master 分支推送到远端仓库

注意默认的分支,需要上 git 管理端设置,不能通过 git 命令完成。

如果使用错误的用户和邮箱执行了git提交,在执行 git push 时将遇到如下错误:

 ! [remote rejected] feature_116390305_story_0 -> feature_116390305_story_0 (You can't push commits with committer ‘yijian’ or email 'eyjian@qq.com' who is not exit among the registered users)
error: failed to push some refs to 'https://github.com/eyjian/sql2struct.git'

解决方法:

为项目配置正确的用户名和邮箱,这个可以直接编辑项目的 .git 子目录下的 config 文件,加上这部分信息,如:

[user]
	name = eyjian
	email = eyjian@qq.com

也可 git 命令方式设置项目的 git:

git config user.name "eyjian"
git config user.email "eyjian@qq.com"

也可设置全局的 git:

git config --global user.name "eyjian"
git config --global user.email "eyjian@qq.com"

为能够 git push 成功,还需要执行下来命令最近一次提交的用户信息:

git commit --amend --author="eyjian <eyjian@qq.com>"

然后就可执行 git push 了,如果需要强制,可以:git push --force 。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值