Lesson Three: Using GitHub to Collaborate

  • Get practice using GitHub to share your code and collaborate on multi-developer projects.
  • Learn how to make and review a pull request on GitHub.
  • Get practice by collaborating with other Udacity students to write a create-your-own-adventure story.

In course 3, we will introduce the GitHub operations, including creating a Repository, pulling, fork, push, and pull Request.


Creation of GitHub Repository

GitHub is a Git Repository site for open source projects. Its Repository is hosted on GitHub server, and its Working Directory and Staging Area cannot be accessed. Therefore, upload to GitHub Repository after local commit.

Local Local Local Remote
Working Directory Staging Area Repository GitHub Repository
Files git add git commit git push

GitHub Repository is also called Remote Repository, or Remote, because it receives pushes and pulls commits to local commits. In addition, uploading local commits to GitHub usually assigns a branch, which enables multiple Reachable commits to be pushed at one time, rather than pushed.

Git Clone command can be used to download existing projects from GitHub to GitHub, and to upload local projects to GitHub.

  1. Create a Repository manually on GitHub, type the Repository name, and uncheck Initialize this Repository with a README. Git clone command cannot be used locally in Remote Repository without commit.
  2. Enter the command locally at Git Bash or Terminal
Remote Repository is not available
git remote
Git Clone is a Remote Repository that can be found on GitHub.
Git for Windwos: This command will ask you to enter the GitHub account password. For Git for Windwos, you can enter it in Git Bash
# git config --global credential.helper wincred cache the account password to avoid the need to enter the account password each time
git remote add origin url
Remote Repository: Origin
git remote
View details of Remote Repository, including fetch and push addresses, which are usually the same URL
git remote -v
Description Local Master commits push to origin Remote
git push origin master
Copy the code

On the Refresh GitHub Repository page in the browser, you can see locally uploaded files and commits.

GitHub Pull

When communicating with someone on a project or after editing a file directly on the GitHub page, commits that need to be updated to local from Remote are maintained using the Git pull directive.

The description of the local branch checkout is
git pull origin master
# Retrieve the description of Remote commits
git log
Copy the code

Like Git Push, Remote’s latest files and change logs are available locally.

The following diagram shows the concepts that have been introduced.

GitHub Fork

When you need to participate in someone else’s existing project, GitHub can directly fork someone else’s Remote to your own account, instead of pulling it locally and pushing it to your own Remote. Git clone of GitHub server-side Repository Fork has a quantitative record and links to the original Repository to facilitate contributions to the original Repository.

When Local and Remote are modified at the same time, push or pull collaboration conflicts occur, similar to merge conflict. As shown in the figure above, a push or pull conflict occurs when a commit A72 change has been made locally but has not been pushed to Remote and a commit 5B3 change has been made in Remote.

To resolve these types of Commits, a new concept should be introduced: Local Copies of Remote Commits. Continuing with the example above, when a change to COMMIT A72 is made locally but not yet pushed to Remote, Description Remote commits origin/master are not recent changes in Remote commits 5B3.

In order to resolve the interaction between Local COMMIT A72 and Remote Commit 5B3, origin/master commits should be updated to COMMIT 5B3. Git fetch is used to update copies of Origin/Master commits, which are pushed to Remote after resolving the conflict.

Update description origin/ Master commits
git fetch origin
Description Merge copies conflict with local recent commits
git merge master origin/master
git add file_conflict
git commit
# Push back Remote after conflict resolution
git push origin master
The status of local branch and Remote is up to date
git status
Copy the code

Git pull origin master is equivalent to git fetch Origin and Git merge master origin/master. GitHub automatically completes the merge operation without any conflicts. Strictly speaking, Git checks for Local and Remote merge commit relationships (accessible), and if so, triggers a human-free fast-forward merge.

GitHub Pull Request

When collaborating with others on your project (Settings in GitHub Repository → Properties enter user names to set collaborators), it is common to create a branch of local changes and push them to Remote, Use pull Requests on GitHub to have collaborators review changes or get feedback; At the same time, it does not break the master. After discussion, we decide whether to merge branch into the master. In fact, the concept of pull request is merge. Specific operation examples are as follows:

Create a branch
git branch branch_change
Switch to the branch and make changes
git checkout branch_change
git add file_change
git commit
# push to the branch_change branch of Remote
git push origin branch_change
Copy the code

From GitHub Repository, switch to the branch_change branch (equivalent to checkout) and click pull Request. The default request address is the Repository of the original fork (if your project is forked). You need to change it to your master. After filling in relevant information, click Create Pull Request to successfully send the Pull Request.

If the master has changed after sending the pull Request, you can also merge master branch_change to update the pull request.

Switch to the branch_change branch
git checkout branch_change
# merge master to branch_change and resolve conflicts
git merge master branch_change
git add file_conflict
git commit
Push the solution to the branch_change branch
git push origin branch_change
Copy the code

After the final confirmation, click Merge to complete a Merge commit. In this case, you can delete branch_change (delete only GitHub branches, local pull will prompt you that the branch cannot be found).

If you are involved in other people’s projects and want to contribute to the original project, it is common to create a branch after fork, and wait for the appropriate opportunity to pull the request to the original fork address, let the original author review whether to merge the master. In addition to the origin Remote automatically generated during Git Clone, it is required to manually add a fork Remote (git Remote add upstream URL). URL is the GitHub address of the original fork. The operation of pull Request is similar to that of your own project. Click on GitHub Repository to switch to your own branch, click on Pull Request, keep the default request address of the original fork, and fill in the relevant information. Click Create Pull Request to successfully send the Pull Request while waiting or communicating with the original author.

Other Git directives:

Support long directory addresses
git config --system core.longpaths true
Copy the code

The diagram of all the concepts introduced so far is as follows.