If you've found yourself on this page, we're assuming you're brand new to Git and GitHub. This guide will walk you through the basics and explain a little bit about how everything works along the way.

Contributing to a project

At some point you may find yourself wanting to contribute to someone else's project, or would like to use someone's project as the starting point for your own. This is known as "forking". For this tutorial, we'll be using theSpoon-Knifeproject, hosted on GitHub.com.

Step 1: Fork the "Spoon-Knife" repository(第一步,找到这个项目,点击 fork图标

To fork this project, click the "Fork" button in the GitHub.com repository.

Click "Fork"

Step 2: Clone your fork(第二步,在shell窗口中输入如下命令

You've successfully forked the Spoon-Knife repository, but so far it only exists on GitHub. To be able to work on the project, you will need to clone it to your local machine.

Run the following code:(命令如下

git clone https://github.com/username/Spoon-Knife.git(注意要替换成自己的username # Clones your fork of the repository into the current directory in terminal

Step 3: Configure remotes(第三步,配置一个映射到原始项目的流(upstream),用于从原始项目获取更新,输入下面两个命令

When a repository is cloned, it has a default remote calledoriginthat points to your fork on GitHub, not the original repository it was forked from. To keep track of the original repository, you need to add another remote namedupstream:

 cd Spoon-Knife
# Changes the active directory in the prompt to the newly cloned "Spoon-Knife" directory
git remote add upstream https://github.com/octocat/Spoon-Knife.git命令1# Assigns the original repository to a remote called "upstream"
git fetch upstream命令2# Pulls in changes not present in your local repository, without modifying your files

More Things You Can Do(more

You've successfully forked a repository, but get a load of these other cool things you can do:

Pull in upstream changes(将原始项目中的更新合并到自己的master分支上

If the original repository you forked your project from gets updated, you can add those updates to your fork by running the following code:

git fetch upstream
# Fetches any new changes from the original repository
git merge upstream/master
# Merges any changes fetched into your working files

Push commits(提交本地的更新(到本地master分支上)

Step 2: Commit your README

Now that you have your README set up, it's time to commit it. A commit is essentially a snapshot of all the files in your project at a particular point in time. In the prompt, type the following code:

 git add README
# Stages your README file, adding it to the list of files to be committed

git commit -m 'first commit'
# Commits your files, adding the message "first commit"

Step 3: Push your commit

So far, everything you've done has been in your local repository, meaning you still haven't done anything on GitHub yet. To connect your local repository to your GitHub account, you will need to set a remote for your repository and push your commits to it.

 git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository

git push origin master
# Sends your commits in the "master" branch to GitHub

 

 

Create branches

Branching allows you to build new features or test out ideas without putting your main project at risk. In git, branch is a sort of bookmark that references the last commit made in the branch. This makes branches very small and easy to work with.

 

Pull requests

If you are hoping to contribute back to the original fork, you can send the original author apull request.

Unwatch the main repository

When you fork a particularly popular repository, you may find yourself with a lot of unwanted updates about it. To unsubscribe from updates to the main repository, click the "Unwatch" button on themain repositoryand select "Not Watching".