git 的简单使用

本文转载至 http://www.jianshu.com/p/4105f1f21490

之前用svn的,但是愕然发现好多公司都开始用git ,一开始不知道为什么,想想那么多公司用它,肯定有它的优点啥,带着好奇的心态,慢慢开始去研究它,由于我们公司,就我一个iOS,所以暂时好多git的好多优点都是持续发掘的,本文也会持续更新中····

一、安装

我们用的都是Mac,所以可以直接通过homebrew安装Git,具体方法请参考homebrew的文档:http://brew.sh/

 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

然后再检查

 $ git version // 判断是否安装成功,查看版本号

然后,设置你的个人信息

 $ git config --global user.name "YourName"
 $ git config --global user.email "YourEmail"

注意'git config'命令的'--global'参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

二、基本使用

2-1、可以新建一个文件夹,mkdir testGit, 然后cd testGit

 $ git init  // 创建仓库
    Initialized empty Git repository in /Users/testGit/.git/

2-2、增加你需要改变的东西,放到testGit中去,添加到仓库中去

 $ git add .  // 注意 . 是增加所有的

2-3、提交到仓库中去 -m “这里面是需要注释的内容”

 $ git commit -m "first init "

2-4、接下来就是看,你的远程仓库建在什么地方啦,我在github 和 coding 上都有,个人认为刚开始在coding 上使用还是不错的

 $ git remote add origin git@coding.com....

假如此时遇到这个问题,fatal: Authentication failed for,那么有可能是没有添加ssh key导致的,具体可以参考这文章git ssh key 的生成

2-5、把内容推送到远程库上

  $ git push -u origin master //由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令
  $ git push  origin master  // 后期,当然在分支的情况,另论

2-6、然后每次用之前

 $ git pull origin master // 每次使用之前最好刷新一下

实际上现在到目前为止就可以开始用起来啦,当然分支管理这个大部分我们后期在讨论。
常用的命令

 $ git status   // 查看每次变化的
 $ git  log  // 查看每次记录的 
 $ git log --pretty=oneline // 查看记录更清晰 $ git clone git@github.com:****.git // 从远程仓库克隆,常用

2-7、建立分支并切换

  $ git  branch  分支名字  // 建立分支
  $ git checkout 分支名字  // 切换到分支

  $ git branch // 查看分支

2-8、合并分支

  //先切换主支
 $ git checkout master
 $ git merge --no-ff  分支名字
三、理解

工作区-----仓库----远程仓库(git add; git commit ; git push )


屏幕快照 2015-10-09 上午11.30.31.png

屏幕快照 2015-10-09 上午11.30.43.png
四、常见问题(陆续增加中···)
4-1(问)、合并的时候遇到的冲突

error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.
E325: ATTENTION
Found a swap file by the name ".git/.MERGE_MSG.swp"
owned by: eladb dated: Tue Aug 20 10:52:03 2013
file name: ~eladb/MyWorkspace/Client/.git/MERGE_MSG
modified: no
user name: eladb host name: Elads-MacBook-Pro-2.local
process ID: 29959 (still running)
While opening file ".git/MERGE_MSG"
dated: Tue Aug 20 10:53:11 2013
NEWER than swap file!
(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r .git/MERGE_MSG"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".git/.MERGE_MSG.swp"
to avoid this message.
Swap file ".git/.MERGE_MSG.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)born:

4-1(答)解决方法:

找到".git/.MERGE_MSG.swp",之后删除即可,然后重新git add .;
git commit -m "";git push master 之后,再执行 git merge branchName 就好啦

4-2 git push 出现的警告问题

warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior,use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

解决:

git config --global push.default matching或者git config --global push.default simple命令,以后再push就不会有警告了。
下面说一下push.default matching和push.default simple的区别:
push.default设置maching的意思是:git push 会把你本地所有分支push到名称相对应的远程主机上。这意味着可能你会在不经意间push一些你原本没打算push的分支。
push.default设置成simple的意思是:git push仅仅把当前所在分支push到从当初git pull pull下来的那个对应分支上,另外,这个过程也会同时检查各个分支的名称是否相对应。

平常最好用 $ git push origin BRANCH #BRANCH是你远程分支的名字

相应的 git pull

  git pull origin BRANCH #BRANCH是你远程分支的名字
4-3 git merge 合并时遇到的冲突, Automatic merge failed; fix conflicts and then commit the result
$ git merge secondDev
Auto-merging myTest.txt
CONFLICT (content): Merge conflict in myTest.txt Automatic merge failed; fix conflicts and then commit the result.

自动合并失败。由于在同一行进行了修改,所以产生了冲突。

在冲突处

add 把变更录入到索引中
commit 记录索引的状态
pull 取得远端数据库的内容

然后重新提交

$ git add myTest.txt
$ git commit -m "合并secondDev分支" # On branch master nothing to commit (working directory clean)
五、一张很好用git 的图

2010072023345292.png
参考链接(安装中配置和使用的基本)

1、http://blog.csdn.net/matrixhero/article/details/8214156
// 基本的安装及配置
2、http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396287703354d8c6c01c904c7d9ff056ae23da865a000 // 详细的讲解和说明,?
3、http://www.ruanyifeng.com/blog/2015/08/git-use-process.html // git的使用规范

转载于:https://www.cnblogs.com/Camier-myNiuer/p/5556650.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值