常用的 Git 配置和命令

Author Avatar
Klein 11月 12, 2023

记录一下我的 Git 配置和命令。真的很好用。

配置

补充下修改 Git 配置的两种方法

  1. 修改配置文件
1
code ~/.gitconfig
  1. 修改命令
1
git config --global alias.st status

大小写

1
git config core.ignorecase false

Git push自动设置upstream

1
git config --global push.default current

添加别名

  1. 方法1 修改配置文件
1
bash ~/.gitconfig

添加以下内容:

1
2
3
4
5
[alias]
st = status
ci = commit
co = checkout
br = branch

方法2 命令

1
git config --global alias.st status

在 unix 中,如果命令中有空格使用 单引号

1
git config --global alias.ci 'commit -v'

在 Windows ,如果命令有空格或有命令行参数使用双引号

1
c:\dev> git config --global alias.ci "commit -v"

命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 搜索分支
git branch | grep search-name

# 查看最新活动的分支
git branch --sort=-committerdate

# rebase
git rebase -i [startpoint] [endpoint]

#使用cherry-pick在两个项目之间共享代码

# 提交前会检查是否有新的提交,若有新的提交则会提交失败
# 注意 --force-with-lease 失败后再执行一次也会强制提交覆盖
git push --force-with-lease

# 暂存
git stash

# 暂存 + 备注
git stash save "message"

# 应用最近一次暂存的修改,并删除暂存的记录
git stash pop

# 应用某个存储, git stash apply stash@{$num} ,num默认为0
git stash apply

# 查看所所有暂存
git stash list

# 删除所有暂存
git stash clear

git revert

参考

How do I alias commands in git? - Stack Overflow
git淫技奇巧