Git很好很强大,功能也很多。用了也很久了,总是还能学到新知识。
有两个好用的小技巧,分享一下。
Git中是一定要设置user.name和user.email的。
在Git中,git config user.name
和 git config user.email
这两个命令用于设置或获取当前Git配置的用户名称和电子邮件地址。这些设置对于Git的许多功能至关重要,因为它们用于标识谁进行了提交。
git commit
命令时,Git会自动使用这些配置来填充提交信息的作者部分。--global
选项),也可以仅为当前项目设置(在项目的Git目录中执行命令)。如何设置?
场景一:全局级别。
设置全局用户名和电子邮件地址。
git config --global user.name "Your Name"
git config --global user.email "your.email@ai-as.cn"
设置完成后所有的Git仓库中的用户名和电子邮件地址都用这个
场景二:项目级别。
不同的仓库的user.name和user.email是不同的
step1: 把经常使用的user.name和user.email设置成全局的。
设置的办法同 场景一
step2: 为不同的Git仓库自定义user.name和user.email
2.1 cd 到Git仓库所在文件夹目录【很关键】
2.2 执行以下git config命令
git config user.name "Your Name"
git config user.email "your.email@ai-as.cn"
看下效果
另外,Idea提交代码时,也可以指定Author
但,只能指定git config配置过的。
如果指定的Author没有使用git config配置过。则会报错:
“fatal: --author '793059909@ai-as.cn' is not 'Name <email>' and matches no existing author”
使用IDEA merge这种报错,IDEA是解不了
因为,IDEA目前没有提供增加 --allow-unrelated-histories参数的地方
只能通过git merge命令来解决。
关键是增加这个参数:
--allow-unrelated-histories
知其然,还需要知其所以然,就这个报错我们来展开聊聊:
错误信息“fatal: refusing to merge unrelated histories”通常出现在你尝试将两个没有任何共同提交历史的Git仓库合并时。这通常发生在以下情况:
要解决这个问题,你可以使用--allow-unrelated-histories
选项来强制Git合并这些没有共同历史的仓库。以下是具体步骤:
如果你在克隆仓库时遇到这个问题,可以在克隆命令中添加--allow-unrelated-histories
选项:
git clone --allow-unrelated-histories <repository-url>
如果你在合并分支时遇到这个问题,可以在合并命令中添加--allow-unrelated-histories
选项:
git merge --allow-unrelated-histories <branch-name>
或者,如果你是在执行git pull
时遇到这个问题,可以这样使用:
git pull --allow-unrelated-histories <remote-name> <branch-name>
如果你在合并远程仓库时遇到这个问题,可以在合并命令中添加--allow-unrelated-histories
选项:
git merge --allow-unrelated-histories <remote-name>/<branch-name>
--allow-unrelated-histories
选项会强制合并两个没有共同历史的仓库,这可能会导致合并冲突。因此,在执行合并操作之前,确保你已经备份了重要的数据。