由于以前工作一直使用SVN,这次做RN,客户端使用的Git来管理源码,所以今天花了点时间来研究git。
目的:以最短的时间上手git,不说原理性的东西,让从未使用过git的人能快速上手。so,让我们开始吧。
1、安装 (略) 百度一大把
2、配置账号
- git cofig --global user.name "xiaodao"
- git config --global user.email "xiaodao@gmail.com"
3、克隆一个git仓库,并查看日志(git log)
- git clone http://xxxx/test.git
- cd test
- git log
4、使用git管理代码流程(这个是重点)
git还是主要使用命令行来操作,感觉在Mac上用还是不错的,下面详细的说下命令,看官请看:
1) 与远程仓库同步 :git pull
- F:\gitworkspace\Test>git pull
- Already up-to-date.
如果是远程仓库项目没有变化,也就是说其他的开发者没有对项目进行变更,会显示这样的信息:
Already up-to-date.
2) 修改文件
这个就不用说了吧,自己随便改点什么文件吧。
3) 查看变更: git status
有2种情况吧
Untracked files 未载入stage unstaged
- F:\gitworkspace\Test>git status
- On branch master
- Your branch is up-to-date with 'origin/master'.
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
-
- test.txt
-
- nothing added to commit but untracked files present (use "git add" to track)
changes to be committed
已经载入(stage)了
- F:\gitworkspace\Test>git status
- On branch master
- Your branch is up-to-date with 'origin/master'.
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
-
- new file: test.txt
4) 载入变更 : git add <file>
实际上上面已经看到了,就是将unstaged 状态变更为 changes to be committed
- F:\gitworkspace\Test>git add test.txt
5) 提交载入的变更 git commit -a
- F:\gitworkspace\Test>git commit -a
- [master 6610af9] test
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 test.txt
再次查看 状态 nothing to commit 实际上这也是一种状态,上面忘记说了。本机已经提交,但是还没有push到服务器。
- F:\gitworkspace\Test>git status
- On branch master
- Your branch is ahead of 'origin/master' by 1 commit.
- (use "git push" to publish your local commits)
- nothing to commit, working directory clean
显示我们的工作目录clean了(同时提示我们现在的分枝上有1个提交还没有上传)
6)上传到服务器 git push
- F:\gitworkspace\Test>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.
-
- Since Git 2.0, Git defaults to the more conservative 'simple'
- behavior, which only pushes the current branch to the corresponding
- remote branch that 'git pull' uses to update the current branch.
-
- See 'git help config' and search for 'push.default' for further information.
- (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
- 'current' instead of 'simple' if you sometimes use older versions of Git)
-
- Username for 'https://github.com': xiaodao
- Password for 'https://xiaodao@github.com':
- Counting objects: 3, done.
- Delta compression using up to 4 threads.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (3/3), 301 bytes | 0 bytes/s, done.
- Total 3 (delta 0), reused 0 (delta 0)
- To https://github.com/xiaodao/Test.git
- e3479a5..6610af9 master -> master
查看状态,对比 Your branch is ahead of 'origin/master' by 1 commit. 可以看到没有需要commit的文件了。
- F:\gitworkspace\Test>git status
- On branch master
- Your branch is up-to-date with 'origin/master'.
- nothing to commit, working directory clean
到目前为止,我们看到了新数据都已经上传完毕,服务器上的主分支也已经更新了. 搞定!
还有些其他命令可以参考:
想和详细的了解git,可以参考:http://fsjoy.blog.51cto.com/318484/244397 ,分为几篇文章讲解,非常不错。