A fork is a new repository that shares code and visibility settings with the original upstream
repository.
A fork 是一个新的存储库,它与原 upstream
存储库共享代码和可见性设置。
Forks let you make changes to a project without affecting the original repository, also known as the upstream
repository. After you fork a repository, you can fetch updates from the upstream
repository to keep your fork up to date, and you can propose changes from your fork to the upstream
repository with pull requests. A fork can be owned by either a personal account or an organization.
Forks 可让你在不影响原始存储库 (也称为 upstream
存储库) 的情况下对项目进行更改。Fork 存储库后,可以从 upstream
存储库获取更新以使 fork 保持最新状态,并且可以使用 pull requests 将更改建议从 fork 提交到 upstream
存储库。A fork 可由个人帐户或组织拥有。
When you view a forked repository on GitHub, the upstream
repository is indicated below the name of the fork.
查看 GitHub 上 forked 存储库时,upstream
存储库会显示在 fork 名称下方。
fork [fɔː(r)k]:n. 叉,岔路,耙,路的岔口 v. 分岔,用叉子叉起食物,用叉举起,在路或河道的岔口转弯
upstream [ʌpˈstriːm]:adv. 逆流,向 (或在) 上游 adj. (石油工业等) 上游的,溯流而上的
In open source projects, forks are often used to iterate on ideas or changes before incorporating the changes into the upstream
repository. If you fork a public repository to your personal account, make changes, then open a pull request to propose your changes to the upstream
repository, you can give anyone with push access to the upstream
repository permission to push changes to your pull request branch (including deleting the branch). This speeds up collaboration by allowing repository maintainers to make commits or run tests locally to your pull request branch from a user-owned fork before merging. You cannot give push permissions to a fork owned by an organization.
在开源项目中,forks 常用于迭代想法或更改,然后将其合并到 upstream
存储库。如果你将一个公共存储库 fork 到你的个人帐户,进行更改,然后创建一个 pull request,向 upstream
存储库提出你的更改,便可允许对 upstream
存储库具有推送权限的任何人将更改推送到 pull request branch (including deleting the branch)。
Deleting a fork will not delete the original upstream
repository. You can make any changes you want to your fork, and there will be no effect on the upstream
. For example, you can add collaborators, rename files, or generate GitHub Pages on the fork without affecting the upstream
. After a fork is deleted, you cannot restore the fork.
删除 fork 不会删除原始 upstream
仓库。你可以对 fork 进行任何所需的更改,并且不会对 upstream
产生任何影响。例如,可以在 fork 上添加协作者、重命名文件或生成 GitHub Pages,而不会影响上游。删除 fork 后,无法还原该 fork。
$ git clone https://github.com/YOUR_USERNAME/YOUR_FORK.git
yongqiang@yongqiang:~/yongqiang_work$ git clone https://github.com/ForeverStrongCheng/kmeans.git
Cloning into 'kmeans'...
remote: Enumerating objects: 152, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 152 (delta 0), reused 0 (delta 0), pack-reused 151
Receiving objects: 100% (152/152), 36.68 KiB | 417.00 KiB/s, done.
Resolving deltas: 100% (82/82), done.
yongqiang@yongqiang:~/yongqiang_work$
yongqiang@yongqiang:~/yongqiang_work$ cd kmeans/
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
yongqiang@yongqiang:~/yongqiang_work/kmeans$
cd
with no other text.ls
.cd your_listed_directory
.cd ..
.When you fork a project in order to propose changes to the upstream repository, you can configure Git to pull changes from the upstream repository into the local clone of your fork.
当你 fork 一个项目以向 upstream repository 提出更改建议时,你可以配置 Git 以将更改从 upstream repository 拉到你 fork 的本地克隆中。
Open Git Bash.
Type git remote -v
and press Enter. You will see the current configured remote repository for your fork.
List the current configured remote repository for your fork.
$ git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git remote -v
origin https://github.com/ForeverStrongCheng/kmeans.git (fetch)
origin https://github.com/ForeverStrongCheng/kmeans.git (push)
yongqiang@yongqiang:~/yongqiang_work/kmeans$
git remote add upstream
, and then paste the URL your upstream repository and press Enter.Specify a new remote upstream
repository that will be synced with the fork.
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
git remote -v
again. You should see the URL for your fork as origin
, and the URL for the upstream repository as upstream
.Verify the new upstream repository you’ve specified for your fork.
$ git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git remote add upstream https://github.com/NVIDIA/kmeans.git
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git remote -v
origin https://github.com/ForeverStrongCheng/kmeans.git (fetch)
origin https://github.com/ForeverStrongCheng/kmeans.git (push)
upstream https://github.com/NVIDIA/kmeans.git (fetch)
upstream https://github.com/NVIDIA/kmeans.git (push)
yongqiang@yongqiang:~/yongqiang_work/kmeans$
Sync a fork of a repository to keep it up-to-date with the upstream repository.
upstream
repository. Commits to BRANCHNAME
will be stored in the local branch upstream/BRANCHNAME
.upstream
仓库获取分支及其各自的提交。 对 BRANCHNAME
的提交将保存在本地分支 upstream/BRANCHNAME
中。Fetch the project branches from the upstream repository
$ git fetch upstream
master
.$ git checkout master
upstream
default branch - in this case, upstream/master
- into your local default branch. This brings your fork’s default branch into sync with the upstream
repository, without losing your local changes. If your local branch didn’t have any unique commits, Git will perform a fast-forward.upstream
默认分支 (在本例中为 upstream/master
) 的更改合并到本地默认分支中。这会使复刻的默认分支与 upstream
仓库同步,而不会丢失本地更改。Merge the upstream counterpart into your local master
$ git merge upstream/master
or
$ git rebase upstream/master
$ git push origin master
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git fetch upstream
From https://github.com/NVIDIA/kmeans
* [new branch] master -> upstream/master
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git push origin master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com':
Everything up-to-date
yongqiang@yongqiang:~/yongqiang_work/kmeans$
Syncing your fork with the upstream repository
$ git fetch upstream
$ git rebase upstream/master
Before making changes to the project, you should create a new branch and check it out. By keeping changes in their own branch, you follow GitHub Flow and ensure that it will be easier to contribute to the same project again in the future.
在对项目进行更改之前,应创建新的分支。通过将更改保留在在自己的分支中,可以遵循 GitHub 流,并确保它将来再次为同一项目做出贡献会更容易。
Creating a branch (创建分支)
$ git branch BRANCH-NAME
$ git checkout BRANCH-NAME
or
$ git checkout -b
When you’re ready to submit your changes, stage and commit your changes. git add .
tells Git that you want to include all of your changes in the next commit. git commit
takes a snapshot of those changes.
当您准备好提交更改时,请暂存并提交更改。git add .
告诉 Git 你希望在下一次提交中包含所有更改。git commit
会拍摄这些更改的快照。
$ git add .
or
$ git add
$ git commit -m "a short description of the change"
or
$ git commit -s
Note:
$ git add -p
$ git add --patch
Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.
交互式地选择 the index and the work tree 之间的补丁块并将它们添加到索引中。这使用户有机会在将修改的内容添加到索引之前查看差异。
This effectively runs add --interactive
, but bypasses the initial command menu and directly jumps to the patch subcommand.
You can now push your local changes to your forked repository
$ git push origin
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git branch
* master
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git checkout -b yongqiang
Switched to a new branch 'yongqiang'
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git branch
master
* yongqiang
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ vim README.md
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git diff
diff --git a/README.md b/README.md
index 517638b..7927c0b 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-kmeans
+k-means
======
A simple kmeans clustering implementation for double precision data,
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git status
On branch yongqiang
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git add .
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git status
On branch yongqiang
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: README.md
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git commit -m "a short description of the change"
[yongqiang 9cf098e] a short description of the change
1 file changed, 1 insertion(+), 1 deletion(-)
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git push origin yongqiang
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 295 bytes | 98.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'yongqiang' on GitHub by visiting:
remote: https://github.com/ForeverStrongCheng/kmeans/pull/new/yongqiang
remote:
To https://github.com/ForeverStrongCheng/kmeans.git
* [new branch] yongqiang -> yongqiang
yongqiang@yongqiang:~/yongqiang_work/kmeans$
$ git commit --amend
$ git push origin yongqiang -f
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git commit --amend
[yongqiang 8cd312b] Refine the README.md file
Date: Sun Apr 16 23:21:08 2023 +0800
1 file changed, 1 insertion(+), 1 deletion(-)
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git push origin yongqiang
Username for 'https://github.com': chengyq116
Password for 'https://[email protected]':
To https://github.com/ForeverStrongCheng/kmeans.git
! [rejected] yongqiang -> yongqiang (non-fast-forward)
error: failed to push some refs to 'https://github.com/ForeverStrongCheng/kmeans.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git push origin yongqiang -f
...
yongqiang@yongqiang:~/yongqiang_work/kmeans$
Now that you’ve created and pushed changes to a feature branch in your forked repository, you can now open a pull request from which you created your fork.
In the local branch you are working from, you may add additional commits and re-push as documented above. This will automatically add the new commits to the pull request and CI checks will be re-triggered.
在你工作的本地分支中,你可以添加额外的提交并重新推送,如上文所述。这将自动将新提交添加到 pull request 中,并且将重新触发 CI 检查。
You could amend the original commit and force push it back to your remote origin:
$ git add
$ git commit --amend
$ git push origin -f
The pull request will be updated accordingly and CI checks will be re-triggered.
git branch -d
git push --delete origin
https://yongqiang.blog.csdn.net/
GitHub Contributions
https://hyperledger-fabric.readthedocs.io/zh_CN/latest/github/github.html
Getting started with GitHub documentation
https://docs.github.com/en/get-started
https://codex.so/fork-and-pull-en