使用github
1.Fork a example repo(https://help.github.com/articles/fork-a-repo/)
Forking a repository is a simple two-step process. We've created a repository for you to practice with!
- On GitHub, navigate to the octocat/Spoon-Knife repository.
In the top-right corner of the page, click Fork.
That's it! Now, you have a fork of the original octocat/Spoon-Knife repository.
Keep your fork synced
You might fork a project in order to propose changes to the upstream, or original, repository. In this case, it's good practice to regularly sync your fork with the upstream repository. To do this, you'll need to use Git on the command line. You can practice setting the upstream repository using the same octocat/Spoon-Knife repository you just forked!
Step 1: Set up Git
If you haven't yet, you should first set up Git. Don't forget to set up authentication to GitHub from Git as well.
Step 2: Create a local clone of your fork
Right now, you have a fork of the Spoon-Knife repository, but you don't have the files in that repository on your computer. Let's create a clone of your fork locally on your computer.
- On GitHub, navigate to your fork of the Spoon-Knife repository.
Under the repository name, click Clone or download.-
In the Clone with HTTPs section, click to copy the clone URL for the repository. -
Open Git Bash.
-
Type
git clone, and then paste the URL you copied in Step 2. It will look like this, with your GitHub username instead ofYOUR-USERNAME:git clone https://github.com/YOUR-USERNAME/Spoon-Knife -
Press Enter. Your local clone will be created.
git clone https://github.com/YOUR-USERNAME/Spoon-Knife Cloning into `Spoon-Knife`... remote: Counting objects: 10, done. remote: Compressing objects: 100% (8/8), done. remove: Total 10 (delta 1), reused 10 (delta 1) Unpacking objects: 100% (10/10), done.
Now, you have a local copy of your fork of the Spoon-Knife repository!
Step 3: Configure Git to sync your fork with the original Spoon-Knife repository
When you fork a project in order to propose changes to the original repository, you can configure Git to pull changes from the original, or upstream, repository into the local clone of your fork.
- On GitHub, navigate to the octocat/Spoon-Knife repository.
Under the repository name, click Clone or download.-
In the Clone with HTTPs section, click to copy the clone URL for the repository. -
Open Git Bash.
-
Change directories to the location of the fork you cloned in Step 2: Create a local clone of your fork.
- To go to your home directory, type just
cdwith no other text. - To list the files and folders in your current directory, type
ls. - To go into one of your listed directories, type
cd your_listed_directory. - To go up one directory, type
cd ...
- To go to your home directory, type just
-
Type
git remote -vand press Enter. You'll see the current configured remote repository for your fork.git remote -v origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) -
Type
git remote add upstream, and then paste the URL you copied in Step 2 and press Enter. It will look like this:git remote add upstream https://github.com/octocat/Spoon-Knife.git -
To verify the new upstream repository you've specified for your fork, type
git remote -vagain. You should see the URL for your fork asorigin, and the URL for the original repository asupstream.git remote -v origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
Now, you can keep your fork synced with the upstream repository with a few Git commands. For more information, see "Syncing a fork."
2.Syncing a fork(https://help.github.com/articles/syncing-a-fork/)
Sync a fork of a repository to keep it up-to-date with the upstream repository.
Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git.
-
Open Git Bash.
-
Change the current working directory to your local project.
-
Fetch the branches and their respective commits from the upstream repository. Commits to
masterwill be stored in a local branch,upstream/master.
$ git fetch upstream
remote: Counting objects: 75, done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 62 (delta 27), reused 44 (delta 9)
Unpacking objects: 100% (62/62), done.
From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master
4.Check out your fork's local master branch.
$ git checkout master
Switched to branch 'master'
5.Merge the changes from upstream/master into your local master branch. This brings your fork's master branch into sync with the upstream repository, without losing your local changes.
$ git merge upstream/master
Updating a422352..5fdff0f
Fast-forward
README | 9 -------
README.md | 7 ++++++
2 files changed, 7 insertions(+), 9 deletions(-)
delete mode 100644 README
create mode 100644 README.md
If your local branch didn't have any unique commits, Git will instead perform a "fast-forward":
$ git merge upstream/master
Updating 34e91da..16c56ad
Fast-forward
README.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
3.Dealing with line endings(https://help.github.com/articles/dealing-with-line-endings/)
If you're using Git to collaborate with others on GitHub, ensure that Git is properly configured to handle line endings.
Every time you press return on your keyboard you're actually inserting an invisible character called a line ending. Historically, different operating systems have handled line endings differently.
When you view changes in a file, Git handles line endings in its own way. Since you're collaborating on projects with Git and GitHub, Git might produce unexpected results if, for example, you're working on a Windows machine, and your collaborator has made a change in OS X.
Global settings for line endings
The git config core.autocrlf command is used to change how Git handles line endings. It takes a single argument.
On Windows, you simply pass true to the configuration. For example:
git config --global core.autocrlf true
# Configure Git on Windows to properly handle line endings
Per-repository settings
Optionally, you can configure the way Git manages line endings on a per-repository basis by configuring a special .gitattributes file. This file is committed into the repository and overrides an individual's core.autocrlf setting, ensuring consistent behavior for all users, regardless of their Git settings. The advantage of a .gitattributes file is that your line configurations are associated with your repository. You don't need to worry about whether or not collaborators have the same line ending settings that you do.
The .gitattributes file must be created in the root of the repository and committed like any other file.
A .gitattributes file looks like a table with two columns:
- On the left is the file name for Git to match.
- On the right is the line ending configuration that Git should use for those files.
Example
Here's an example .gitattributes file. You can use it as a template for your repositories:
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text
# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf
# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
You'll notice that files are matched--*.c, *.sln, *.png--, separated by a space, then given a setting--text, text eol=crlf, binary. We'll go over some possible settings below.
text=auto
Git will handle the files in whatever way it thinks is best. This is a good default option.
text eol=crlf
Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux.
text eol=lf
Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.
binary
Git will understand that the files specified are not text, and it should not try to change them. The binary setting is also an alias for -text -diff.
Refreshing a repository after changing line endings
After you've set the core.autocrlf option and committed a .gitattributes file, you may find that Git wants to commit files that you have not modified. At this point, Git is eager to change the line endings of every file for you.
The best way to automatically configure your repository's line endings is to first backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once.
-
Save your current files in Git, so that none of your work is lost.
git add . -u git commit -m "Saving files before refreshing line endings" -
Remove the index and force Git to rescan the working directory.
rm .git/index -
Rewrite the Git index to pick up all the new line endings.
git reset -
Show the rewritten, normalized files.
git status -
Add all your changed files back, and prepare them for a commit. This is your chance to inspect which files, if any, were unchanged.
git add -u # It is perfectly safe to see a lot of messages here that read # "warning: CRLF will be replaced by LF in file." -
Rewrite the
.gitattributesfile.git add .gitattributes -
Commit the changes to your repository.
git commit -m "Normalize all the line endings"
更多推荐
所有评论(0)