蓝天

配置项目的git

只需要编辑项目根目录下的 .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 。

posted on 2024-02-24 09:27  #蓝天  阅读(5)  评论(0编辑  收藏  举报

导航