git 学习

来源:互联网 发布:电脑分辨率测试软件 编辑:程序博客网 时间:2024/05/17 23:52

学习参考网址 :http://blog.csdn.net/googdev/article/details/52575079

非常感谢大牛stormzhang,他的文章非常赞!!!


一、Git 安装

Mac:https://sourceforge.net/projects/git-osx-installer/
Windows:https://git-for-windows.github.io/
Linux:apt-get install git

二、Git基础操作

(注:Windows环境)

1、创建一个文件夹test,并在test下创建一个a.md文件,然后在git bash下用cd命令进入到刚才创建的test文件夹,

2、初始化 git 仓库

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git init  
3、查看状态

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git status  
4、把a.md文件添加到本地Git仓库

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git add a.md  
5、设置下自己的用户名与邮箱

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git config —global user.name "JasonLi"   
  2. git config —global user.email "lijingxuan92@126.com"  

6、正式提交文件

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git commit -m ‘first commit’  

-m 代表是提交信息

7、查看所有产生的 commit 记录

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git log  
8、把本地 test 项目与 GitHub 上的 test 项目进行关联(切换到 test 目录)

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git remote add origin git@github.com:JasonLi-cn/test.git  
(查看我们当前项目有哪些远程仓库)
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git remote -v  
9、向远程仓库进行代码提交(前提是你已经配置好公钥和密钥,配置方法见第三部分

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git push origin master  
提交时,可能出现的问题:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $ git push origin master  
  2. To github.com:JasonLi-cn/test.git  
  3.  ! [rejected]        master -> master (fetch first)  
  4. error: failed to push some refs to 'git@github.com:JasonLi-cn/test.git'  
  5. hint: Updates were rejected because the remote contains work that you do  
  6. hint: not have locally. This is usually caused by another repository pushing  
  7. hint: to the same ref. You may want to first integrate the remote changes  
  8. hint: (e.g., 'git pull ...') before pushing again.  
  9. hint: See the 'Note about fast-forwards' in 'git push --help' for details.  
说明在远程仓库中存在本地仓库没有的文件,所以需要先pull操作

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git pull origin master  
此时可能会遇到的问题:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $ git pull origin master  
  2. From github.com:JasonLi-cn/test  
  3.  * branch            master     -> FETCH_HEAD  
  4. fatal: refusing to merge unrelated histories  
解决方法:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git pull origin master --allow-unrelated-histories  
然后就可以 push了!!!

三、公钥和密钥配置方法

在Git bash中执行:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ssh-keygen -t rsa  
会生成两个文件 id_rsa 和 id_rsa.pub , id_rsa 是密钥,id_rsa.pub 就是公钥。

第一步先在 GitHub 上的设置页面,点击最左侧 SSH and GPG keys ,然后点击右上角的 New SSH key 按钮,

在 Key 那栏把 id_rsa.pub 公钥文件里的内容复制粘贴进去就可以了,

Title 那栏不需要填写,点击 Add SSH key 按钮。

SSH key 添加成功之后,输入

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ssh -T git@github.com  

  进行测试,如果出现以下提示证明添加成功了。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $ ssh -T git@github.com  
  2. Hi JasonLi-cn! You've successfully authenticated, but GitHub does not provide shell access.  
四、其它常用命令

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. git branch aaa 新建分枝aaa  
  2. git branch 查看分枝  
  3. git checkout aaa 切换到分枝aaa  
  4. git checkout -b aaa 新建并切换到aaa  
  5. git merge aaa 把aaa分支的代码合并过来(当前所在分枝,比如master)  
  6. git branch -d aaa 删除分枝aaa  
  7. git branch -D aaa 强制删除aaa  
  8. git tag v1.0 加版本号  
  9. git tag 查看版本号  
  10. git checkout v1.0 切换到版本v1.0  
0 0