我每天使用的 Git 命令

2025-05-28

我每天使用的 Git 命令

1. 获取所有最新更改而不合并

别再拉取你认为会崩溃的代码了!fetch在你的工作流程中,你可以直接获取更新的代码,而无需立即合并。代码获取完成后,你可以像其他分支一样进行检出。当你满意后,就可以合并了。

git fetch --all
# git checkout upstream/some-branch
Enter fullscreen mode Exit fullscreen mode

2. 无论当前分支名称如何,都可以向上游推送

不要再输入分支名称,尤其是长分支名称。只需告诉 git 你想将当前分支推送到远程位置即可。HEAD是一个告诉 git 使用当前分支的关键字。

git push production head
# git push origin head
# git push github head
Enter fullscreen mode Exit fullscreen mode

3. 给你的藏品贴上标签

如果您存储了大量代码并且想要一眼就记住存储的内容,这将非常有用。

git stash save -m "my work in progress"
Enter fullscreen mode Exit fullscreen mode

4. 使用很久以前的储藏物

停止使用 来撤消存储,git pop以便恢复旧存储。您可以使用以下命令应用很久以前创建的存储。

# git stash list
git stash apply 3
Enter fullscreen mode Exit fullscreen mode

5. 检出前一个分支

当你在开发一些小功能,并想通过切换分支来比较行为/性能时,这非常有用。你无需输入名称,只需使用减号即可。

git checkout -
Enter fullscreen mode Exit fullscreen mode

我非常喜欢这个命令,所以我制作了一个简短的 YouTube 视频来展示它!

6. 签出后更改分支的基础

如果你创建了一个新分支,但基于错误的分支,这个功能就很有用。例如,假设你想从 Beta 代码分支,但却意外地使用了生产代码分支。

git rebase --onto beta production feature
# git rebase newBase oldBase currentBranch
Enter fullscreen mode Exit fullscreen mode

7. 将未提交的更改移至新的/现有的分支

git switch -c new-branch
# git switch existing-branch
Enter fullscreen mode Exit fullscreen mode

奖励 - 模糊结账

这个自定义命令允许你快速切换到另一个分支,而无需输入完整名称。当你使用命名约定并且厌倦了输入类似feature/或的前缀时,这个命令非常有用。issue/

function fc() {
    gco "$(git branch --format='%(refname:short)' | grep $1)"
}
Enter fullscreen mode Exit fullscreen mode

如果您的分支名称被调用,feature/dropdown-select-color您可以通过执行类似这样的操作快速切换分支。

fc dropdown
Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/wadecodez/the-git-commands-i-use-every-day-5g17
PREV
TailwindCSS 项目的专业技巧 1. 长宽比 2. 插入定位 分隔列表样式 结论 奖励
NEXT
5 个给 Web 开发者的全新项目创意