ubuntu 下安装git 并上传代码至github

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ipatient/article/details/51334153

参考博客
http://www.xitongzhijia.net/xtjc/20150320/42297.html
http://blog.chinaunix.net/uid-17188120-id-4650534.html

1.Ubuntu下安装Git

Ubuntu14.04 LTS默认是已经安装Git的,可以使用 git –version 测试是否安装。
如果没有安装,使用命令: sudo apt-get install git git-core 安装git

2.ssh认证

在Ubuntu下使用ssh命令连接github.com的SSH服务,登录名为[email protected](所有GitHub用户共享此SSH用户名)。

ssh -T [email protected]

执行之后提示:

Permission denied (publickey).

这说明我们还没有在GitHub账户中正确设置公钥认证

进入

settings->SSH and GPG keys->new SSH key

但是不建议这么做

使用github 官方教程
https://help.github.com/articles/generating-an-ssh-key/

具体步骤为:

Checking for existing SSH keys

(1) Open Terminal.

(2) Enter ls -al ~/.ssh to see if existing SSH keys are present:

ls -al ~/.ssh
#Lists the files in your .ssh directory, if they exist

(3) Check the directory listing to see if you already have a public SSH key.

出现 github 为私钥,github.pub为公钥。

Generating a new SSH key

(1) Open Terminal.

(2) Paste the text below, substituting in your GitHub email address.

ssh-keygen -t rsa -b 4096 -C "[email protected]"
# Creates a new ssh key, using the provided email as a label
**Generating public/private rsa key pair.**

(3) When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

(4) At the prompt, type a secure passphrase. For more information, see “Working with SSH key passphrases”.

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Adding your SSH key to the ssh-agent

Before adding a new SSH key to the ssh-agent, you should have checked for existing SSH keys and generated a new SSH key.

(1) Ensure ssh-agent is enabled:

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
Agent pid 59566

(2) Add your SSH key to the ssh-agent. If you used an existing SSH key rather than generating a new SSH key, you’ll need to replace id_rsa in the command with the name of your existing private key file.

$ ssh-add ~/.ssh/id_rsa

(3)Add the SSH key to your GitHub

  1. Copy the SSH key to your clipboard.

If your SSH key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

$ sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)

$ xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Tip: If xclip isn’t working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

2.In the top right corner of any page, click your profile photo, then click Settings.

3.In the user settings sidebar, click SSH and GPG keys

4.Click New SSH key.

5.In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.
The key field

6.Paste your key into the “Key” field.

7.Click Add SSH key.

8.Confirm the action by entering your GitHub password.

设置账户信息

git config --global user.name “lukeyan”
git config --global user.email [email protected]

设置成功后,用Terminal用ssh命令访问GitHub,会显示一条认证成功的消息并退出。

ssh -T [email protected]

执行后提示:

Hi github! You've successfully authenticated, but GitHub does not provide shell access.

经过以上几步设置就可以直接使用git命令访问github的代码仓库了。

3.提交代码至GitHub

首先,在github.com上创建一个新的repo,根据情况加上适当的.gitignore,LICENSE等,然后提交本地代码至github

git pull #获取新版本

git status #获取需要上传的文件 

git add . # .表示全添加, git add README.md 表示只添加说明文件

git commit -m "add new files" # a commit

git remote add origin [email protected]:yourgithubname/yourrepositoryname

git push -u origin master

附上一张图,很有用:
这里写图片描述

4.建立分支与Pull requests

To create a new branch

(1) Go to your new repository e.g. hello-world.
(2) Click the drop down at the top of the file list that says branch: master.
(3) Type a branch name, readme-edits, into the new branch text box.
(4) Select the blue Create branch box or hit “Enter” on your keyboard.
这里写图片描述

Now you have two branches, master and readme-edits. They look exactly the same, but not for long! Next we’ll add our changes to the new branch.

对新分支做一些改变

Click the README.md file.
Click the pencil icon in the upper right corner of the file view to edit.
In the editor, write a bit about yourself.
Write a commit message that describes your changes.
Click Commit changes button.
这里写图片描述

Open a Pull Request

Pull Request 是什么意思
百度知道:有一个仓库,叫Repo A。你如果要往里贡献代码,首先要Fork这个Repo,于是在你的Github账号下有了一个Repo A2,。然后你在这个A2下工作,Commit,push等。然后你希望原始仓库Repo A合并你的工作,你可以在Github上发起一个Pull Request,意思是请求Repo A的所有者从你的A2合并分支。如果被审核通过并正式合并,这样你就为项目A做贡献了

Click the Pull Request tab, then from the Pull Request page, click the green New pull request button.

这里写图片描述

Select the branch you made, readme-edits, to compare with master (the original). 这里写图片描述

Look over your changes in the diffs on the Compare page, make sure they’re what you want to submit.

这里写图片描述

When you’re satisfied that these are the changes you want to submit, click the big green Create Pull Request button.
这里写图片描述

Give your pull request a title and write a brief description of your changes.
这里写图片描述

When you’re done with your message, click Create pull request!

Merge your Pull Request

In this final step, it’s time to bring your changes together – merging your readme-edits branch into the master branch.

Click the green Merge pull request button to merge the changes into master.
Click Confirm merge.
Go ahead and delete the branch, since its changes have been incorporated, with the Delete branch button in the purple box.
这里写图片描述
这里写图片描述

*

5. 从GitHub克隆项目到本地

*

第一步: 到GitHub的某个仓库,然后复制右边的有个“HTTPS clone url”

第二步: 回到要存放的目录下,使用命令 “git clone https://github.com/chenguolin/scrapy.git“,红色的url只是一个例子

第三步: 如果本地的版本不是最新的,可以使用命令 “git fetch origin”,origin是本地仓库

第四步: 把更新的内容合并到本地分支,可以使用命令 “git merge origin/master”

如果你不想手动去合并,那么你可以使用: git pull <本地仓库> master // 这个命令可以拉去最新版本并自动合并

*

6 GitHub的分支管理

*

创建

1 创建一个本地分支: git branch <新分支名字>

2 将本地分支同步到GitHub上面: git push <本地仓库名> <新分支名>

3 切换到新建立的分支: git checkout <新分支名>

4 为你的分支加入一个新的远程端: git remote add <远程端名字> <地址>

5 查看当前仓库有几个分支: git branch

删除

1 从本地删除一个分支: git branch -d <分支名称>

2 同步到GitHub上面删除这个分支: git push <本地仓库名> :

*

7常见错误

*

1 如果出现报错为ERROR: Repository not found.fatal: The remote end hung up unexpectedly则代表你的 origin 的url 链接有误,可能是创建错误,也可能是这个 [email protected]:xxx/new-project.git url 指定不正确。重新创建。

gedit  ./git/config

将文件中的 [remote “origin”]部分去掉!
重新初始化

git init

猜你喜欢

转载自blog.csdn.net/ipatient/article/details/51334153