常用git命令

记录对我有用的git命令。

个人信息配置

Git的config会作为git commit时的相关信息,但不会作为验证信息,这一点是很多新手的误区。能不能推送上去,只和ssh、https验证是否通过有关,和username及email无关。

也就是说,随便设置username和email,再配置好ssh,一样能推送上去。

那么为什么还要设置username和email?原因是,在GitHub的个人主页上,所有使用非个人邮箱的推送是不会被“count”的。

在GitHub的docs上关于哪些commit会被计算进入主页,有这么一段话:

Commits will appear on your contributions graph if they meet all of the following conditions:

  • The email address used for the commits is associated with your GitHub account.
  • The commits were made in a standalone repository, not a fork.
  • The commits were made:
    • In the repository's default branch
    • In the gh-pages branch (for repositories with project sites)

--GitHub docs

也就是说,小明的GitHub账号是"xiaomin@email.com",如果git config里把邮箱设置成"xiaohong@email.com",那推送是无法在个人主页显示的。

虽然这有一点“面子工程”的嫌疑,但是看到自己的主页绿油油的,心里还是会有一点开心?!

为所有git仓库设置邮箱和用户名的命令如下:

git config --global user.name "your global username"
git config --global user.email "your global email"

如果你有多个git仓库,并且想要为它们设置不同的username和email,需要在项目的根目录下,使用以下命令进行配置:

git config  user.name "your all repo username"
git config  user.email "your all reop email" 

关于查看配置的命令:

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

# 查看某个配置,显示的全局的配置
git config user.name
git config user.email 

# 某个项目根目录下使用,显示的是 全局配置+当前项目的配置
git config --list 

commit

记录一下empty commit的命令

git commit --allow-empty -m "commit message"

协同开发

本地创建切换分支

git checkout -b dev

现在位于dev分支下

推送到远程分支

git push -u origin dev

本地合并dev分支

先到把dev合并的分支,比如main

git  checkout main

然后merge

git merge dev

删除本地和远程的dev分支

先删除本地分支

git branch -d dev

再删除远程分支

git push origin --delete dev

参考资料

[1] Git全局配置和单个仓库的用户名邮箱配置

[2] Git合并分支

原文地址:https://www.cnblogs.com/ticlab/p/15821784.html