Background
If you see an interesting project on GitHub and want to participate in its development, you generally need to follow a certain process. Here’s a general project collaboration development process.
Of course, this process is not only applicable to GitHub, but also to other code hosting platforms such as GitLab.
Prerequisites
- Git is installed
- GitHub account is registered
Git settings
-
Set username and email
There are two modes for setting the username and email.
-
If you only use one GitHub account, you can set the global username and email.
1 2
git config --global user.name "Your Name" git config --global user.email "Your Email"
-
If you have multiple GitHub accounts, you can set the username and email for each Git repository separately.
1 2 3
cd /path/to/your/repo git config user.name "Your Name" git config user.email "Your Email"
-
-
Login credential management
After setting the username and email in the previous step, when cloning a repository and every time you `git push`, you need to enter your GitHub username and password, which is very cumbersome. You can use a credential manager to save your username and password. - Windows If the version number of Git you installed is higher than `2.29`, Git has integrated support for GitHub OAuth. When you first clone a GitHub private repository via HTTPS, Git will prompt you to log in to GitHub using a browser and authorize Git to access your GitHub account. After that, Git will automatically save your GitHub credentials, so you don't need to enter your username and password again. - macOS You need to install [`Git Credential Manager`](https://github.com/git-ecosystem/git-credential-manager)。You can run the following command in the terminal: ```bash brew install --cask git-credential-manager ``` After installation, when you first clone a GitHub private repository via HTTPS, Git will prompt you to log in to GitHub using a browser and authorize Git to access your GitHub account. After that, Git will automatically save your GitHub credentials, so you don't need to enter your username and password again.
GitHub collaboration development process
Operations on the GitHub website —— Fork
-
Open the GitHub page of the project you want to participate in, click the
Fork
button in the upper right corner, and fork the project to your own GitHub account. We call the original project theoriginal repository
and the project forked to your own account theFork repository
. -
In your GitHub account, find the forked project (i.e.,
Fork repository
), click theClone or download
button, and copy the URL of the project.
Local operations —— Clone
-
In the terminal on your local machine, switch to the directory where you want to store the project, and run the following command to clone the project to your local machine.
1
git clone https://github.com/your-username/project-name.git
Here,
your-username
is your GitHub username, andproject-name
is the name of the project. -
Enter the project directory, run the following command to add the
original repository
as a remote repository.1
git remote add upstream https://github.com/authors-usename/project-name.git
Here,
authors-username
is the GitHub username of the author of theoriginal repository
, andproject-name
is the name of the project.Then run the following command to check the status of the remote repository.
1
git remote -v
If everything is normal, you will see output similar to the following:
1 2 3 4
origin https://github.com/your-username/project-name.git (fetch) origin https://github.com/your-username/project-name.git (push) upstream https://github.com/authors-username/project-name.git (fetch) upstream https://github.com/authors-username/project-name.git (push)
Here,
origin
is yourFork repository
, andupstream
is theoriginal repository
.
Local development
-
In the terminal on your local machine, switch to the project directory, run the following command to create a new branch.
1
git checkout -b new-branch-name
Here,
new-branch-name
is the name of your new branch, which can be the name of the new feature you want to add, or the name of the bug you want to fix. -
In the terminal on your local machine, develop, modify code/add new features/fix bugs. Then run the following command to add the modified files to the staging area and commit them to the local repository.
1 2
git add . git commit -m "Your commit message"
You can also push the changes in the local repository to the
Fork repository
.1
git push origin new-branch-name
-
Repeat the operations in step 2 until your new feature/bug fix is complete.
-
Now we can create a Pull Request, but before creating a Pull Request, to ensure that our new code does not conflict when merged into the
original repository
, it is best to pull the latest code of themain
branch of theoriginal repository
, and then merge the latest code of themain
branch of theoriginal repository
into yournew-branch-name
branch.1 2 3 4
git checkout main git pull upstream main git checkout new-branch-name git merge main
When doing this, if there have been new commits to the
main
branch of theoriginal repository
since you started developing the new feature, yournew-branch-name
branch may have conflicts. You need to resolve these conflicts. After resolving the conflicts, run the following command to add the modified files to the staging area and commit them to the local repository.1 2
git add . git commit -m "Your commit message"
Then run the following command again to push the modified files to the
Fork repository
.1
git push origin new-branch-name
This way, your
new-branch-name
branch will not have conflicts when merged into themain
branch of theoriginal repository
.
Operations on the GitHub website —— Pull Request
-
Open the GitHub page of your
Fork repository
, click theNew pull request
button, and create a newPull Request
. Note that if theoriginal repository
provides aPull Request
template, you need to fill in the title and content of thePull Request
according to thePull Request
template of theoriginal repository
. -
Wait for the author of the
original repository
to review yourPull Request
. If necessary, you need to make changes according to the feedback from the author of theoriginal repository
. -
If the author of the
original repository
accepts yourPull Request
, congratulations, your code will be merged into theoriginal repository
.
Local operations —— Update local repository
-
In the terminal on your local machine, switch to the
main
branch, run the following command to pull the latest code of themain
branch of theoriginal repository
to your local machine.1 2
git checkout main git pull upstream main
You can also use the
git fetch upstream main
command to check the latest code of themain
branch of theoriginal repository
, and then use thegit merge upstream/main
command to merge the latest code of themain
branch of theoriginal repository
into themain
branch of your local machine.1 2 3
git fetch upstream main git checkout main git merge upstream/main
This way, your local
main
branch will be synchronized with themain
branch of theoriginal repository
. -
Then you can push the
main
branch of your local machine to theFork repository
.1
git push origin main
-
(Optional) If your
new-branch-name
branch has been merged into themain
branch of theoriginal repository
, you can delete thenew-branch-name
branch.1
git branch -d new-branch-name
You can also delete the remote branch of the
new-branch-name
branch.1
git push origin --delete new-branch-name
This way, you have completed the work of collaborating with others to add a new feature/fix a bug on GitHub.
Continue to Develop New Features/Fix New Bugs
If you need to continue adding new features/fixing new bugs, you can first synchronize your main
branch of the Fork repository
with the main
branch of the original repository
to ensure that you start working from the latest version of the project:
```bash
git checkout main
git pull upstream main
git push origin main
```
Then, you can create a new branch on the main
branch to continue developing new features/fixing new bugs.
```bash
git checkout -b another-new-branch-name
```
The subsequent operations are the same as after local development.