github workflow

本文主要叙述如何在已有的github repository添加自己写的部分,主要参考链接为:https://code.tutsplus.com/tutorials/how-to-collaborate-on-github--net-34267。一般github workflow的步骤为:

  1. Fork the target repo to your own account. (如果你是owner或者collaborator,就不用这一步)
  2. Clone the repo to your local machine.
  3. Check out a new "topic branch" and make changes.
  4. Push your topic branch to your fork.
  5. Use the diff viewer on GitHub to create a pull request via a discussion. (由owner进行)
  6. Make any requested changes.
  7. The pull request is then merged (usually into the master branch), and the topic branch is deleted from the upstream (target) repo.

1 Fork and Clone

在想要 fork 的 repository,点击右上角的 fork 键即可,具体可以参考链接中的图片。Clone 请在 repository 中点击绿色的 `Code` 按钮,复制其中末尾为 `.git` 的链接

 

 

然后新建文件夹,并在cmd中找到该文件夹的界面(或者在文件浏览器中打开该文件夹,并按 shift+鼠标右键,然后选择 "在此处打开PowerShell窗口" 也可以),输入指令

git clone https://github.com/YOUR_NAME/REPO_NAME.git

 

(文件夹中会clone下来包含代码的文件夹,直接把代码复制出来也可以,或者就直接在上一层的文件夹执行clone命令即可。)在该文件夹使用 remote add 指令,添加 upstream remote

git remote add upstream git@github.com:YOUR_NAME/REPO_NAME.git

若是 clone 下来的 repo,一般会默认设定好 upstream,设定好 upstream后就可以pull 代码

git pull upstream master

 

2 Check out, Commit, and Push

 进行自己的更改时,要先 checkout 一个branch,避免直接修改 master branch 的内容。否则 push 的时候经常会报错,无法 push

git checkout -b YOUR_BRANCH_NAME

命令行会显示:

Switched to a new branch 'YOUR_BRANCH_NAME'

然后用指令 `git branch`,可以看到自己新建的branch和 `main`,其中新建的branch会标绿并有个 `*`。

之后就可以尽情在该文件夹进行自己的修改,比如添加自己想要的功能,或者完成分配的任务。调试完成后,在命令行 commit 这些修改

git add .
git commit -m "Changed README repository"

另外,完成指令 `git add .` 后,可以通过 `git status` 来看自己添加了哪些更改,如果有不想添加的文件(比如vscode、poycharm的设置文件),可以在文件夹内的 `.gitignore` 文档添加这些文件(若没有则新建),然后用指令 `git rm --cached FILE_NAME` 来删掉添加的这些文件就可以了。

Commit 并且确定没有问题后,push到repository

git push origin YOUR_BRANCH_NAME

 

之后在网页端登录,并调整 branch 之后就可以看到更改的内容。 

 

3 Create a pull request

 进入该branch,然后点击 `Compare & pull request`, 输入 comments 后,点击 `Create pull request` 即可。

然后在该 repository 的 `Pull requests` 就可以看到刚刚提交的 request,接下来进行 Merge 即可。

 

posted @ 2023-01-04 13:45  hey,dummy  阅读(356)  评论(0编辑  收藏  举报