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 2git 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 3cd /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
-
Method 1 (Old method):
You need to install
Git Credential Manager. You can run the following command in the terminal:1brew install --cask git-credential-managerAfter 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.
Git Credential Manager supports various Git hosting platforms, including GitHub, GitLab, Bitbucket, etc., and you can use this tool for credential management.
-
Method 2 (Recommended method):
For GitHub accounts, you can use the GitHub CLI tool for credential management. You can run the following command in the terminal to install the GitHub CLI tool:
1brew install ghAfter installation, run the following command to log in to your GitHub account:
1gh auth loginFollow the prompts to log in to your GitHub account via a browser. After a successful login, the GitHub CLI tool will automatically configure Git’s credential management, so you don’t need to enter your username and password again.
-
-
Linux
Linux systems are similar to macOS and can choose either Git Credential Manager or GitHub CLI tool for credential management. Just use the package manager of your Linux distribution to install it.
-
GitHub collaboration development process
Operations on the GitHub website —— Fork
-
Open the GitHub page of the project you want to participate in, click the
Forkbutton in the upper right corner, and fork the project to your own GitHub account. We call the original project theoriginal repositoryand the project forked to your own account theFork repository. -
In your GitHub account, find the forked project (i.e.,
Fork repository), click theClone or downloadbutton, 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.
1git clone https://github.com/your-username/project-name.gitHere,
your-usernameis your GitHub username, andproject-nameis the name of the project. -
Enter the project directory, run the following command to add the
original repositoryas a remote repository.1git remote add upstream https://github.com/authors-usename/project-name.gitHere,
authors-usernameis the GitHub username of the author of theoriginal repository, andproject-nameis the name of the project.Then run the following command to check the status of the remote repository.
1git remote -vIf everything is normal, you will see output similar to the following:
1 2 3 4origin 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,
originis yourFork repository, andupstreamis 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.
1git checkout -b new-branch-nameHere,
new-branch-nameis 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 2git add . git commit -m "Your commit message"You can also push the changes in the local repository to the
Fork repository.1git 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 themainbranch of theoriginal repository, and then merge the latest code of themainbranch of theoriginal repositoryinto yournew-branch-namebranch.1 2 3 4git checkout main git pull upstream main git checkout new-branch-name git merge mainWhen doing this, if there have been new commits to the
mainbranch of theoriginal repositorysince you started developing the new feature, yournew-branch-namebranch 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 2git add . git commit -m "Your commit message"Then run the following command again to push the modified files to the
Fork repository.1git push origin new-branch-nameThis way, your
new-branch-namebranch will not have conflicts when merged into themainbranch of theoriginal repository.
Operations on the GitHub website —— Pull Request
-
Open the GitHub page of your
Fork repository, click theNew pull requestbutton, and create a newPull Request. Note that if theoriginal repositoryprovides aPull Requesttemplate, you need to fill in the title and content of thePull Requestaccording to thePull Requesttemplate of theoriginal repository. -
Wait for the author of the
original repositoryto 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 repositoryaccepts 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
mainbranch, run the following command to pull the latest code of themainbranch of theoriginal repositoryto your local machine.1 2git checkout main git pull upstream mainYou can also use the
git fetch upstream maincommand to check the latest code of themainbranch of theoriginal repository, and then use thegit merge upstream/maincommand to merge the latest code of themainbranch of theoriginal repositoryinto themainbranch of your local machine.1 2 3git fetch upstream main git checkout main git merge upstream/mainThis way, your local
mainbranch will be synchronized with themainbranch of theoriginal repository. -
Then you can push the
mainbranch of your local machine to theFork repository.1git push origin main -
(Optional) If your
new-branch-namebranch has been merged into themainbranch of theoriginal repository, you can delete thenew-branch-namebranch.1git branch -d new-branch-nameYou can also delete the remote branch of the
new-branch-namebranch.1git 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.