这篇文章是为像我一样喜欢使用命令行操作 git
的开发者编写的。如果你更喜欢使用图形界面(GUI),希望你也能在这里找到一些有用的内容
Git 别名
Git 别名是一个强大的工作流工具,它可以为常用的 Git 命令创建快捷方式
git alias
(Git 别名)最简单的理解就是为那些较长的命令创建一个快捷方式(简短的命令
),这样不仅更容易记住,而且可以更快地输入。
语法
使用 --global
标记来告诉 git 这个别名将在所有项目中生效(否则,它只会在你当前的工作项目中生效!)
如果原始命令中包含空格,请使用引号(''
)。 对我来说,我几乎为所有日常使用的命令都创建了别名。
Git status
提交前检查更改:
git config --global alias.st status
# Now instead of `git status`, use `git st`
git st
On branch v2
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: components/ui/twemoji
modified: css/tailwind.css
modified: data/blog/git-notes.mdx
no changes added to commit (use "git add" and/or "git commit -a")
Tip: Use
git st
with--short
flag or-s
to see the short-format of the changes, and... you know it - create an alias for this command too
git config --global alias.s 'status --short'
# Now instead of `git st`, use `git s`
git s
M components/Image.js
M data/blog/git-notes.mdx
?? public/static/images/force-with-lease.jpg
结果更清晰,输入也更快捷,对吧?
Git commit
git config --global alias.cm 'commit -m'
Commit changes (add/stage
changes before):
git cm "Initial commit"
TIP
如果只修改了已存在的文件(既不是新文件也不是删除文件),可以使用 --all
或 -a
标记,这样就不需要在提交前添加或暂存更改
git config --global alias.cam 'commit -am'
# 现在不需要执行两个 git 命令
git add style.css # `style.css` 是已存在的文件,不是新文件!
git cm "Update style"
# 只需要使用一条命令
git cam "Update style"
Git stash
将脏工作目录中的更改暂时存储起来
正如定义所说,当你需要在从远程仓库拉取新内容之前**"暂存"**当前更改时,就可以使用 git stash
命令:
# 命令太短,没必要创建别名
git stash
拉取后应用暂存的更改:
git stash pop
为它创建一个别名:
git config --global alias.sp 'stash pop'
# Now
git sp
# Is equal
git stash pop
Git pull/push
始终使用 pull rebase
和 force push
来保持一个整洁的提交树!
pull rebase
git config --global alias.prb 'pull origin --rebase'
# 现在 git pull origin --rebase main # 等同于 git prb main # 或 git prb master
如果在变基后发生冲突怎么办?
使用
git diff
列出所有冲突文件,并为此命令创建一个别名:git config --global alias.cf 'diff --name-only --diff-filter=U'
# 列出所有冲突文件 git cf # 解决所有冲突后暂存更改 git add . # 完成变基 git rebase --continue
force push
当你完成变基后出现的冲突解决后,我们需要强制推送更改到远程仓库:
git config --global alias.pf 'push --force-with-lease' # 现在在变基之后 git pf
TL;DR
The
--force
flag will make git overwrite the remote repo with local changes without comparing with possible updates in the remote after rebasing, which can be dangerous if 2 developers working on the same branch.--force-with-lease
in the opposite way, make sure you can push only when no updates on the upstream exist.
Git checkout
git config --global alias.co 'checkout'
# Eg
git co main
创建新分支:
git config --global alias.cob 'checkout -b'
# Eg
git cob feature-x
TIP
Use git co -
to checkout to the previous branch.
Example:
git branch
dev
* feature-x-y-z__ISSUE_ID
main
# The current branch is `feature-x-y-z__ISSUE_ID`
# Checkout to `dev`
git co dev
# Do something
# Commit ...
# 现在要回到 `feature-x-y-z__ISSUE_ID` 分支,使用
git co -
# 而不是
git checkout feature-x-y-z__ISSUE_ID
# Eg
git d style.css
注意
所有的别名都可以在 ~/.gitconfig
文件(MacOS)中找到。你可以直接打开这个文件并编辑任何你想要修改的别名。
vim ~/.gitconfig
# 在配置文件中找到别名部分
[alias]
s = status --short
st = status
cm = commit -m
# ...
vim

Git 工作流
我的日常 git
工作流程(所有别名都在上一节中有详细说明)
# 暂存更改
git stash
# 从上游更新变更
git prb main
# 应用暂存的更改
git sp
# 如果存在冲突则解决
# 继续工作
# 检查工作状态
git s
# 检查文件更改(如需要)
git d # 或 git d file.ext
# 暂存更改
git add .
# 提交
git cm "commit message"
# 如果没有新建或删除文件,可以跳过暂存直接提交
git cam "commit message"
# 再次更新变更
git prb main
# 如果有冲突,解决后执行
git add file.ext
git rebase --continue
# 强制推送
git pf
# 创建拉取请求
以上就是我的完整工作流程,其中高亮的命令是最常用的
.gitignore
和 .gitkeep
.gitignore
提示:忽略目录下的所有文件但保留一个特定文件
# 忽略目录下的所有文件
homework/*
# 仅保留此文件
!homework/file-to-keep
.gitkeep
如何将一个空目录推送到远程仓库?
在空目录中创建一个
.gitkeep
文件,这样你就可以将该目录推送到远程仓库了!
这并不是 git
的官方功能!只是某个开发者创造的一个约定俗成的做法 。
**解释:**这个技巧的原理是通过在目录中放置一个文件来使其非空(里面有一个文件!)。这样我们就可以将它推送到远程仓库。因此,.gitkeep
可以是任何你想要的文件(是否为空文件都无所谓)。选择 .gitkeep
这个名字是因为它易于理解和记忆。
总结
以上就是我在使用 git
过程中的所有笔记,包括我如何理解这些概念以及如何更高效地使用它们。欢迎在评论区分享你的使用心得!
开心分享