git 使用教程

来源:互联网 发布:mc录音软件 编辑:程序博客网 时间:2024/05/02 04:48
  1. Check for SSH keys. Have an existing key pair? You can skip to Step 4.

    First, we need to check for existing ssh keys on your computer:

    $ cd ~/.ssh

    If it says “No such file or directory“ skip to step 3. Otherwise continue tostep 2.

  2. Backup and remove existing SSH keys.

    Since there is already an SSH directory you’ll want to back the old one up and remove it:

    $ ls$ mkdir key_backup$ cp id_rsa* key_backup$ rm id_rsa*
  3. Generate a new SSH key.

    To generate a new SSH key, enter the code below. We want the default settings so when asked to enter a file in which to save the key, just press enter.

    $ ssh-keygen -t rsa -C "your_email@youremail.com"

    Now you need to enter a passphrase.

    Why do passphrases matter?

    Which should give you something like this:

  4. Add your SSH key to GitHub.

    On the GitHub site Click “Account Settings” > Click “SSH Keys” >Click “Add SSH key”

    Account Settings

    Open the id_rsa.pub file with a text editor (Notepad, TextEdit, or gedit will do just fine). This is your public SSH key. You may need to turn on “view hidden files” to find it because the.ssh directory is hidden. It’s important you copy your SSH key exactly as it is written without adding any newlines or whitespace. Now paste it into the “Key” field.

    Can’t view hidden files? Other ways to copy:

    Now paste it into the “Key” field.

    Paste your SSH Key

    Hit “Add Key.”

  5. Test everything out.

    To make sure everything is working you’ll now SSH to GitHub. Don’t change the “git@github.com” part. That’s supposed to be there.

    $ ssh -T git@github.com

    Which should give you this:

    Don’t worry, this is supposed to happen. Type “yes”.

    Having problems?

Then: Set Up Your Info

Now that you have Git set up and your SSH keys entered into GitHub, it’s time to configure your personal info.

  1. Set your username and email.

    Git tracks who makes each commit by checking the user’s name and email. In addition, we use this info to associate your commits with your GitHub account. To set these, enter the code below, replacing the name and email with your own. The name should be youractual name, not your GitHub username.

    $ git config --global user.name "Firstname Lastname"$ git config --global user.email "your_email@youremail.com"

    More about user info

  2. Set your GitHub token.

    Some tools connect to GitHub without SSH. To use these tools properly you need to find and configure your API Token.

    On the GitHub site Click “Account Settings”

    Copy your API token

    At the command line run the following code, using your GitHub username and token in place of the ones shown.

    $ git config --global github.user username$ git config --global github.token 0123456789yourf0123456789token

    *Note* If you ever change your GitHub password, a new token will be created and will need to be updated.




Create A Repo

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.

First: Create A Repo

Every time you make a commit with Git, it is stored in a repository (a.k.a. “repo”). To put your project up on GitHub, you’ll need to have a GitHub repository for it to live in.

More about repositories

  1. Create a new repo

    Click New Repository.

    Click “New Repository

    Fill out the information on this page. When you’re done, click “Create Repository.”

    Fill in the info

    Congratulations! You have successfully created your first repo!

Next: Create a README for your repo.

While a README isn’t a required part of a GitHub repo, it is a good idea to have one. READMEs are a great place to describe your project or add some documentation such as how to install or use your project.

More about READMEs

  1. Create the README file

    In the prompt, type the following code:

        $ mkdir ~/Hello-World    $ cd ~/Hello-World    $ git init        $ touch README    

    Open the new README file found in your Hello-World directory in a text editor and add the text “Hello World!” When you are finished, save and close the file.

  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:

    More about commits

        $ git add README    $ git commit -m 'first commit'    

    The code above executes actions locally, 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 repo and push your commits to it:

    More about remotes

        $ git remote add origin git@github.com:username/Hello-World.git    $ git push -u origin master    

    Now if you look at your repository on GitHub, you will see your README has been added to it.

    Your README has been created