How to use Git and GitHub under Linux

Source: Internet
Author: User
Tags commit http request ssh how to use git

First, install Git software on Linux

Can be installed directly from the source of the release version

# sudo apt-get install git//ubuntu release
# yum-y Install git//redhat, CentOS release

Second, the use of HTTPS user name password Authentication connection GitHub

1, create the project on the GitHub

First need to apply for an account from the GitHub, after the application is clicked in the upper right corner of the "+" number to create a new repository project, as follows:

2, initialize the project on the host and sync to the GitHub server

Initialize the project on the Linux host and sync to the GitHub server.

[Root@361way abc]# Echo ' # 361way ' >> readme.md
[Root@361way abc]# git init
[Root@361way abc]# git add readme.md
[Root@361way abc]# git commit-m "the" "a"
[Root@361way abc]# git remote add Origin https://github.com/91it/361way.git
[Root@361way abc]# git push-u Origin Master
Username for ' https://github.com ': 91it
Password for ' https://91it@github.com ':
Counting Objects:3, done.
Writing objects:100% (3/3), 201 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (Delta 0)
To Https://github.com/91it/361way.git
* [New branch] Master-> Master
Branch Master is set to track remote branch master from Origin.
[Root@361way abc]#

3, password-free user name Login

If you want to avoid interactively entering user passwords, you can replace the GIT remote add package with:

Git remote add origin https://Username: password @github.com/user name/repository project name. Git
After configuration, go to git push
The URL values that have been configured can be modified by modifying the "remote" origin "item in the. git/config file in the local project directory, or through the GIT Remote set-url origin directive:

Git remote Set-url Origin https://user name: password @github.com/username/repository project name. Git
Local already existing projects, you can save git init process, direct execution of the last two steps push to the server

Git remote add origin https://Username: password @github.com/user name/repository project name. Git
Git push-u Origin Master

Third, the use of SSH key authentication connection GitHub Project

For some cloud hosts or VPS hosts, a 403 error occurs when connecting using HTTPS authentication, as follows:

[Root@91it test]# git push-u Origin Master
Error:the requested URL returned error:403 forbidden while accessing https://github.com/91it/test.git/info/refs
Fatal:http request failed

This situation can be attempted using SSH Key management.

1, SSH key authentication configuration

Linux host uses Ssh-keygen instruction to generate key

[Root@361way mnt]# ssh-keygen-t rsa-c "itybku@139.com"
Generating public/private RSA key pair.
Enter file in which to save the key (/ROOT/.SSH/ID_RSA):
Enter passphrase (empty for no passphrase):
Enter same Passphrase again:
Your identification has been saved In/root/.ssh/id_rsa.
Your public key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:
8d:da:34:b3:11:f9:58:e7:5e:28:9c:e4:31:3d:df:38 itybku@139.com
The key ' s Randomart image is:
+--[RSA 2048]----+
| |
|   .   . |
| O. = OE |
| B * = o+|
| S o B. o|
| + = . o |
| .    O. |
| |
| |
+-----------------+
The email address used here is my email address for GitHub account. Copies the contents of the. Ssh/id_rsa.pub in the user's home directory.

2, GitHub settings and Linux host authentication

Login github.com, enter account Settings, the left choice ssh keys,add ssh key,title casually fill, paste id_rsa.pub content. After the configuration is complete, you can use the following instructions to authenticate on the Linux host.

[Root@361way test]# ssh-t git@github.com
Warning:permanently added the RSA host key for IP addresses ' 192.30.252.130 ' to the ' list of known hosts.
Hi 91it! You have ' ve successfully authenticated, but GitHub does not provide shell access.
If the above hint indicates that the add key is successful, if the agent admitted failure to sign using the key prompt, you can add the key by executing the following instruction

Ssh-add ~/.ssh/id_rsa

3, using the SSH protocol to synchronize

Echo ' # 361way ' >> readme.md
Git init
git add readme.md
Git commit-m "the"
Git remote add Origin git@github.com:91it/361way.git
Git push-u Origin Master
The SSH protocol and the HTTPS protocol URL can be modified by modifying the. git/config file in the local project directory or the GIT remote set-url origin directive.

Four, view the GIT project source

You can view it by viewing the. git/config file under the project, or you can view it through the git remote-v directive, as shown in the following example:

#https认证
[Root@361way test]# git remote-v
Origin Https://github.com/91it/test.git (Fetch)
Origin Https://github.com/91it/test.git (push)
#ssh认证
[Root@361way 361way]# git remote-v
Origin Git@github.com:91it/361way.git (Fetch)
Origin Git@github.com:91it//361way.git (push)

V. Other errors

1. In the execution $ git remote addorigin git@github.com:91it/test.git error Tip: Fatal:remote origin already exists.

Workaround:

$ git Remote RM origin
Then execute: $ git Remote add Origin git@github.com:defnngj/hello-world.git won't be wrong
2. In the execution $ GIT push origin master Error tip: error:failed to push som refs to ....

Workaround:

$ Git pull Origin master//First pull the remote server github the file above and push it up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.