Collaboration in GitHub10 Jun 2022 / 7 minutes to read Elena Daehnhardt |
Introduction
In my post “GIT in 10 minutes”, I have covered the basics of Git setup and a few workflow commands to get started with using Git (version control system). As promised, I will go into the topic of how to use Git for collaborative work. Mainly, I will focus on contributions to other repositories, for instance, open-source or projects of your colleagues and friends. Let’s go!
GitHub Collaboration
As explained in GitHub documentation 1, GitHub supports two ways of collaborative work:
- Forking. You create a repository fork, which essentially copies a repository to your own GitHub account. You do not need to have any permissions for the copied repository. Your changes can be accepted by the repository owner once accepting your pull request and thus including your changes into the principal repository.
- Shared repository. In small teams, we can add project collaborators, usually working on their own branches and adding their contributions with push access. With pull requests, we can do code reviews and comment on the proposed changes.
Forking and Pull Requests
With forks, we can make code changes without affecting the parent repository. Let’s call the parent repository “upstream” and our own (forked) repository “origin.” To add an upstream repository, run:
git remote add upstream git@github.com:<upstream_repo_user>/<upstream_repo>.git
To see all your remote repository links:
git remote -v
origin https:// < user_secret_tocken > @github.com/ < your_user_name>/< repository_name > (fetch) origin https:// < user_secret_tocken > @github.com/ < your_user_name>/< repository_name > (push) upstream git@github.com: < parent_project_maintainer > / < repository_name > .git (fetch) upstream git@github.com: < parent_project_maintainer > / < repository_name > .git (push)
We observe that there are two links for push and fetch operations. One is the “origin”, which is automatically created because I have already cloned the upstream project. The upstream link was added above.
The overall process is as follows:
- Create a fork in GitHub by pressing the “Fork” button in the parent repository. We have created a complete copy of the parent repository with a fork! For instance, you can fork my public repository “deep_learning_notebooks” with some functions and experiments on deep learning.
- Clone the forked repository and further make your changes locally. To clone the repository, use the “git clone” command.
git clone https://<your_access_token>@github.com/<your_user_name>/<cloned_repo_name>
- To change the code in the forked repository, we can create a branch for our changes or new features
to the code.
# to see all project branches git branch # create and checkout a branch git checkout -b <new_branch_name>
- Check if there are changes in the upstream branch.
git remote update && git status git remote show upstream
- Adding changes from upstream
git checkout master git pull upstream master
- Adding your changes and committing
git add . git commit -am "your comment"
- Pushing changes to your fork
git push origin <new_branch_name>
-
In your GitHub forked repository, create a pull request by pressing the “Compare and Pull Request” button.
- This is optional or when you want to use “Issues.” Use an issue’s ID in the commit
message. This will close issue #7 when the related pull request is accepted.
git commit -am "fixed #7"
Syncing Your Forks
When working with others on GitHub, new contributions and repository updates are coming. Your fork might become behind all these changes and should be synced with the upstream repository. Thus, your repository ideally should have all the changes done by your collaborators. You could regularly sync your fork, especially before you do pull requests. This could help in minimising merge conflicts (check 3)
GitHub’s documentation on “Syncing a fork” gives a great explanation on syncing forks. I think the easiest way for a beginner is to use web UI and select the “Fetch Upstream” drop-down button.
As a result, we have updated the forked repository with all new changes, and GitHub’s response is: “Successfully fetched and fast-forwarded from upstream repo:master.”
Next, we will update the local copy of the forked repository with “git pull”.
If you have a more intricate update process, I suggest reading GitHub’s documentation on “Syncing a fork”, and an easy-to-read 5 with nice visuals.
Conclusion
In this post, I have covered GitHub collaboration when working with other team members. Git branching, forking, pull requests, and issues were briefly explained. You can use this post for your reference. However, I suggest going more into this topic with the help of the references below. Soon I will continue the collaboration using shared repositories. Thanks for reading!
References
1. About collaborative development models.
2. Lesson 1. Learn How To Use GitHub to Collaborate on Open Science Projects
3. How to Resolve Merge Conflicts in Git?
5. Lesson 3. Sync a GitHub Repo: How To Ensure Your GitHub Fork Is Up To Date
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for youAbout Elena Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.
|