每个开发人员都应该知道的 20 个 Git 命令行技巧

2025-05-25

每个开发人员都应该知道的 20 个 Git 命令行技巧

每个开发人员都应该知道的 20 个 Git 命令行技巧
Git 是开发人员必备的版本控制工具。虽然 GUI 工具可以简化某些任务,但掌握 Git 命令行可以带来更深入的控制、灵活性和速度。以下是每个开发人员都应该知道的20 个 Git 命令行技巧,以简化他们的工作流程。


1. 设置全局配置

确保您的提交带有正确的身份标记。

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Enter fullscreen mode Exit fullscreen mode

💡提示:使用--local而不是--global来设置特定于项目的配置。


2. 撤消最后一次提交(不丢失更改)

如果您在上次提交时犯了错误,您可以撤消它。

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

这会将您的更改保留在暂存状态,以便您可以修改提交或修复问题。


3. 修改最后一次提交

忘记包含更改或想要更新提交消息?

git add .
git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode

这将更新之前的提交,而不会创建新的提交。


4. 存储未提交的更改

需要快速切换分支而不提交吗?

git stash
Enter fullscreen mode Exit fullscreen mode

💡 稍后使用以下命令检索存储:

git stash pop
Enter fullscreen mode Exit fullscreen mode

5. 以图形方式查看提交历史记录

可视化提交历史可以更容易地了解项目的状态。

git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

6. 更改提交作者

更改最后一次提交的作者。

git commit --amend --author="New Author <newauthor@example.com>"
Enter fullscreen mode Exit fullscreen mode

7. 检查阶段性变更的差异

使用 git diff 比较不同阶段的文件。

git diff --staged
Enter fullscreen mode Exit fullscreen mode

这显示了已进行但尚未提交的更改。


8. 使用 Bisect 查找 Bug

使用 git bisect 查找引入错误的提交。

git bisect start
git bisect bad  # Current commit is bad
git bisect good <commit-hash>  # A known good commit
Enter fullscreen mode Exit fullscreen mode

Git 将遍历提交历史记录来识别有问题的提交。


9. Rebase 以清理提交历史

重新定基将重写您的提交历史,以便更加清晰。

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

这使您可以编辑、压缩或重新排序最后 3 次提交。


10. 挑选特定的提交

想要从另一个分支带来特定的提交吗?

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

11.列出所有分支(本地和远程)

查看有哪些分支机构可用。

git branch -a
Enter fullscreen mode Exit fullscreen mode

12.清理未跟踪的文件和目录

快速删除 Git 未跟踪的不需要的文件。

git clean -fd
Enter fullscreen mode Exit fullscreen mode

💡 用于-n试运行以预览将要删除的内容。


13. 跟踪上游分支

保持本地分支与远程分支同步。

git branch --set-upstream-to=origin/main
Enter fullscreen mode Exit fullscreen mode

14. 使用交互式 Rebase 进行 Squash 提交

将多个提交合并为一个。

git rebase -i HEAD~n  # Replace 'n' with the number of commits
Enter fullscreen mode Exit fullscreen mode

15. 查看特定提交的文件

检查特定提交时的文件状态。

git show <commit-hash>:path/to/file
Enter fullscreen mode Exit fullscreen mode

16. 提交后编辑 .gitignore

如果您忘记忽略某些文件,请更新.gitignore。

echo "node_modules/" >> .gitignore
git rm -r --cached node_modules/
git commit -m "Update .gitignore"
Enter fullscreen mode Exit fullscreen mode

17. 撤销已推送的提交

撤消特定提交的更改而不更改历史记录。

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

18. 仅获取元数据

想要避免获取整个存储库吗?

git fetch --dry-run
Enter fullscreen mode Exit fullscreen mode

这样,您无需实际下载数据即可看到获取的内容。


19. 归咎于一行代码

找出谁在文件中写了特定的一行。

git blame path/to/file
Enter fullscreen mode Exit fullscreen mode

20. 将文件重置为最后一次提交

放弃对特定文件的本地更改。

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

无论您是独自工作还是团队协作,这 20 个 Git 命令行技巧都能让您的开发过程更加顺畅。虽然 GUI 工具提供了便利,但掌握 Git 命令行可以更好地控制您的工作流程。尝试这些命令,提升您的 Git 技能!

祝你编码愉快!🚀

在 github 上关注我:

Jagroop2001 (Jagroop) · GitHub

👨‍💻 全栈开发人员 | 🤖 机器学习开发人员 | 🤝 开发关系专家 – 💼 可聘用 - Jagroop2001

网站图标github.com
文章来源:https://dev.to/jagroop2001/20-git-command-line-tricks-every-developer-should-know-1i21
PREV
每个开发人员都必须知道的 20 个 JavaScript 技巧
NEXT
为什么要记录代码日志 | 代码日志(第 1 部分,共 4 部分)