发布于 2026-01-06 1 阅读
0

Git 基础 - Git + Hub 让一切变得简单 ✨💡👍 设置 配置 创建项目 克隆项目 检出分支 检出 PR 创建 PR - 想将其变基到单个提交 浏览问题 其他常用命令

Git 基础知识 - Git + Hub 让一切变得简单 ✨💡👍

设置

配置

创建项目

克隆项目

去分行看看

查看公关稿

创建了一个 PR - 希望将其合并为单个提交

浏览期刊

我经常使用的其他命令

Git 已经从酷炫的潮人工具变成了工具包中绝对必不可少的工具。

掌握 Git 对你的职业生涯至关重要。

以下是您可能需要用到的一些命令:

注意:这绝不是完整的 Git 入门教程,只是介绍一些有用的命令。

设置

从 GitHub安装Hub 。

适用于 Mac brew install hub
、Windows scoop | choco install hub
、Fedorasudo dnf install hub
和 Debiansudo apt install hub

为 hub 创建一个别名git

配置

使用 Git 最重要的就是添加用户名和邮箱配置。您可以使用以下命令全局设置用户名和邮箱配置。

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
Enter fullscreen mode Exit fullscreen mode

创建项目

无需打开浏览器即可创建本地和远程项目。

$ mkdir awesome-project
$ cd awesome-project
$ git init                          // Initializes git repo
$ git add .                        // Adds files and directories
$ git commit -am "Initial commit" // commits the changes
$ git create                     // Creates a remote repository
$ git push                      // push the changes
Enter fullscreen mode Exit fullscreen mode

🎉🎉🎉

克隆项目

对某个项目感兴趣并想做出贡献?您可以克隆并使用该项目的代码库。

$ git clone <org>/<repository> // Clone the repo
$ cd <repository>             // Go inside
Enter fullscreen mode Exit fullscreen mode

如果你想长期贡献代码或者进行一些定制,也可以创建分支(fork)。

$ git fork --remote-name=origin  // Fork the repo
Enter fullscreen mode Exit fullscreen mode

去分行看看

正在开发一个feature分支。去看看吧。

$ git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

有一些中间工作文件?想暂时存储一下stash

$ git stash
$ git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

想回到储藏室。

$ git stash list
$ git stash apply 
Enter fullscreen mode Exit fullscreen mode

查看公关稿

但有时候你只需要查看一下 PR 就能知道结果。维护者(或)审核者会喜欢的。

$ git pr list -L 20 --format='%t [%H] | %U%n' // List down the PRs
$ git pr checkout  <pr-number>               // Check out a particular PR
Enter fullscreen mode Exit fullscreen mode

目前公关审核正在进行中,还会有更多变动,请别担心。

$ git am -3 https://github.com/<org>/<repo>/pull/<pr-number>
Enter fullscreen mode Exit fullscreen mode

创建了一个 PR - 希望将其合并为单个提交

大多数项目维护者都希望保持 Git 历史记录的整洁。这个简单的操作可以让项目历史记录更易于理解和维护。你可能经常会遇到这样的情况:维护者要求你将多个提交合并为一个单独的提交。

$ git rebase -i HEAD~<number_of_commits>
Enter fullscreen mode Exit fullscreen mode

这很容易,但当你想要基于 master 分支进行 rebase 操作,或者遇到合并冲突时,就会变得棘手。你可以通过以下方式解决这个问题:

$ git rebase -i master
// (fix the conflicts)
$ git add .
$ git rebase --continue
Enter fullscreen mode Exit fullscreen mode

想通过以下方式中止它git rebase --abort

浏览期刊

想查看要抓取的 issue。觉得输入完整的 GitHub URL 很麻烦?你可以用 `.` 来完成hub

$ git browse --issues
Enter fullscreen mode Exit fullscreen mode

我经常使用的其他命令

要获取我当前仓库的状态

$ git status
Enter fullscreen mode Exit fullscreen mode

记录提交历史记录

$ git log
Enter fullscreen mode Exit fullscreen mode

修改提交日志

$ git commit --amend 
<amend the commit>
Enter fullscreen mode Exit fullscreen mode

签署提交:

git commit --amend --signoff
Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/sendilkumarn/git-with-hub-makes-it-easy-some-git-101-51jo