10 个实用的 Git 技巧,助您改善工作流程

2025-05-28

10 个实用的 Git 技巧,助您改善工作流程

Git是一个免费的开源分布式版本控制系统,旨在快速高效地处理从小型到大型的所有项目。

无论你是刚开始使用 Git,还是已经熟悉命令行,提升技能总是有益的。下面我想分享 10 个实用的 Git 技巧,帮助你改进基于 Git 的工作流程。


1. Git 别名

为常用命令创建自己的别名,以节省您在终端上的时间。

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
Enter fullscreen mode Exit fullscreen mode

您无需打字git checkout master,只需输入即可git co master

您还可以通过~/.gitconfig直接修改文件来编辑这些命令或添加更多命令:

[alias]
    co = checkout
    ci = commit
    br = branch
Enter fullscreen mode Exit fullscreen mode

2. 比较提交 ⚖

比较同一文件的提交或版本之间的差异的一个简单方法是使用git diff命令。

如果要比较不同提交之间的同一文件,请运行以下命令:

git diff $start_commit..$end_commit -- path/to/file
Enter fullscreen mode Exit fullscreen mode

如果你想比较两次提交之间的变化:

git diff $start_commit..$end_commit
Enter fullscreen mode Exit fullscreen mode

这些命令将在终端内打开差异视图,但如果您更喜欢使用更直观的工具来比较差异,则可以使用。Meldgit difftool一个有用的查看器/编辑器,可以直观地比较差异。

要配置 Meld:

git config --global diff.tool git-meld
Enter fullscreen mode Exit fullscreen mode

要开始查看差异:

git difftool $start_commit..$end_commit -- path/to/file
Enter fullscreen mode Exit fullscreen mode

或者

git difftool $start_commit..$end_commit
Enter fullscreen mode Exit fullscreen mode

3. 存储未提交的更改🔖

如果你正在开发某个功能,并且需要对项目进行紧急修复,你可能会遇到一个问题。😰 你肯定不想提交未完成的功能,也不想丢失当前的更改。解决方案是使用 Git stash 命令暂时移除这些更改:

git stash
Enter fullscreen mode Exit fullscreen mode

git stash 命令隐藏更改,为您提供一个干净的工作目录并能够切换到新分支进行更新,而无需提交无意义的快照来保存当前状态。

完成修复工作并想要重新查看以前的更改后,您可以运行:

git stash pop
Enter fullscreen mode Exit fullscreen mode

您的更改将被恢复。🎉

如果您不再需要这些更改并想要清除存储堆栈,您可以这样做:

git stash drop
Enter fullscreen mode Exit fullscreen mode

4. 频繁拉取

为了避免重大冲突,您应该经常将更改从主分支拉到您的分支,以尽快解决任何冲突并使您的分支更容易合并到主分支。

5. 自动完成命令🤖

使用补全脚本bash,您可以快速创建、tcsh和 的命令zsh。如果您想输入git pull,只需输入 的首字母,git p然后输入 ,Tab将显示以下内容:

pack-objects   -- create packed archive of objects
pack-redundant -- find redundant pack files
pack-refs      -- pack heads and tags for efficient repository access
parse-remote   -- routines to help parsing remote repository access parameters
patch-id       -- compute unique ID for a patch
prune          -- prune all unreachable objects from the object database
prune-packed   -- remove extra objects that are already in pack files
pull           -- fetch from and merge with another repository or local branch
push           -- update remote refs along with associated objects
Enter fullscreen mode Exit fullscreen mode

要显示所有可用的命令,请git在终端中输入Tab+ Tab

6. 设置全局.gitignore🚫

如果您想避免提交像.DS_Store或 Vimswp文件这样的文件,您可以设置一个全局.gitignore文件。

创建文件:

touch ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

然后运行:

git config --global core.excludesFile ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

或者手动将以下内容添加到您的~/.gitconfig

[core]
  excludesFile = ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

您可以创建一个列表,列出希望 Git 忽略的内容。要了解更多信息,请访问gitignore 文档

7. 删除已在 fetch/pull 时从远程移除的本地分支

您的本地仓库中可能存在一些过时的分支,而这些分支在远程仓库中已经不存在了。要在每次 fetch/pull 操作时删除它们,请运行:

git config --global fetch.prune true
Enter fullscreen mode Exit fullscreen mode

或者手动将以下内容添加到您的~/.gitconfig

[fetch]
  prune = true
Enter fullscreen mode Exit fullscreen mode

8. 更有效地使用 Git blame

Git blame 是一个方便的方法来发现谁修改了文件中的某一行。根据你想要显示的内容,你可以传递不同的标志:

git blame -w  # ignores white space

git blame -M  # ignores moving text

git blame -C  # ignores moving text into other files
Enter fullscreen mode Exit fullscreen mode

HEAD9. 👀的别名

@与 相同HEAD。在 rebase 期间使用它可以节省时间:

git rebase -i @~2
Enter fullscreen mode Exit fullscreen mode

10. 重置文件↩

您正在修改代码,突然意识到所做的更改不太好,想要重置它们。与其点击“撤消”所有编辑,不如将文件重置到分支的 HEAD 分支。

git reset --hard HEAD
Enter fullscreen mode Exit fullscreen mode

或者如果你想重置单个文件:

git checkout HEAD -- path/to/file
Enter fullscreen mode Exit fullscreen mode

现在,如果您已经提交了更改,但仍想恢复,则可以使用:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

附加功能:✨

如果您想使用更多命令来提升 Git,请尝试git-extras 插件,其中包括git info(显示有关存储库的信息)和git effort(每个文件的提交次数)。

文章来源:https://dev.to/yenyih/10-useful-git-tips-to-improve-your-workflow-kf1
PREV
你绝对可以使用全局变量来管理 React 中的全局状态
NEXT
这些生活窍门将改变您编写 Markdown 的方式!