现在的位置: 首页 > 综合 > 正文

Git 使用教程

2017年12月08日 ⁄ 综合 ⁄ 共 2912字 ⁄ 字号 评论关闭
1:下载Git
 Git for Windows

2:点击安装,依次默认下一步

 

3:安装完成

4:设置SSH建立计算机与Github的链接

4.1 点击开始菜单找到Git Bash,并点击:

 

4.2 运行命令 cd ~/.ssh 检查自己的电脑上是否存在ssh keys

如果显示No such file or directory 则需要去创建一个新的ssh keys

4.3 创建新的ssh keys

运行命令:$ ssh-keygen -t rsa -C "your_email@youremail.com"点击回车

点击回车

输入你的passphrase(密码),并重新输入确认

注:在Enter passphrase 的时候,输入的密码是看不到的,其实已经输入了,输完后点击回车就可以了

这样一个新的keys就创建完成了,上面代码显示,密匙位置放在了C:/Users/用户名/.ssh/文件夹中。(.ssh文件夹可能是隐藏的,需要查看隐藏文件

4.4 将你新生成的ssh keys 添加到github中

在 GitHub 网站点击“Account Settings” > 点击 “SSH Public Keys”> 点击 “Add another public key”

在本机找到你创建的密匙文件id_rsa.pub,使用记事本打开,复制里面所有的内容,粘贴到网站key的文本框中,点击Add Key 保存

5:测试一下我们的设置是否正确

输入命令:$ ssh -T git@github.com

输入yes

我本机显示连接关闭,然后我继续执行上诉命令,提示输入passphrase(密码),输入前面自己设置的passphrase。回车登录成功

 

6:在本地设置Git信息

6.1设置用户名和邮箱

$ git config --global user.name"Firstname Lastname"

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

此处用户名为自己的实际姓名(自定义的),而非登录用户名

 

 

 

 

Git 创建一个库Create a Repository

  官网注册

1:点击 New repository

2:输入库的相关信息,然后点击Create Repository

3:上传一个文件

3.1首先在本地创建一个目录(这个目录名需要和上一步创建的项目名相同):依次执行下列命令行

mkdir Helloworld
cd Helloworld
git init
touch README
git add README
git commit -m 'first commit'

3.2:打开本机的c:/User/用户名/Helloworld文件夹中的readme文件(使用记事本)编辑内容,保存。

3.3:将文件提交到github中的库中,执行下列命令行

git remote add origin git@github.com:github用户名/Helloworld.git
git push -u origin master

 

4:这样我们就已经把一个文件上传到了githb代码库中了!


问题:

当要push代码到git时,出现提示:

error:failed to push some refs to ...

Dealing with “non-fast-forward” errors
From time to time you may encounter this error while pushing:

  1. $ git push origin master  
  2. To ../remote/  
  3.  ! [rejected]        master -> master (non-fast forward)  
  4. error: failed to push some refs to '../remote/'  

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.
This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching
and merging the remote branch, or using pull to perform both at once.
In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this
is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:

1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

git push -f

2,先把git的东西fetch到你本地然后merge后再push

$ git fetch

$ git merge

这2句命令等价于

  1. $ git pull  

可是,这时候又出现了如下的问题:

上面出现的 [branch "master"]是需要明确(.git/config)如下的内容
[branch "master"]
    remote = origin

    merge = refs/heads/master

这等于告诉git2件事:

1,当你处于master branch, 默认的remote就是origin。

2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变

如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:

  1. $ git config branch.master.remote origin  
  2. $ git config branch.master.merge refs/heads/master  

之后再重新git pull下。最后git push你的代码吧

抱歉!评论已关闭.