小米运动ios版功能:GIT 的基本使用-常用命令

来源:百度文库 编辑:偶看新闻 时间:2024/05/03 03:16:28

2 简介GIT是一个分布式的版本控制系统。
分布式的意思是它不依赖一个中央服务器,每个开发者clone得到的仓库都包含了完整的变更记录。
 
3 GIT 的四种协议file: 只能访问本地repository
git:后台运行git-daemon,这种方式架设简单,运行速度快,但是传输的数据没有加密。
ssh:安全可靠的连接,如同访问本地文件一样,但是权限不好控制。
http/https:通过webdav实现,可以通过浏览器查看代码,权限管理与svn协议相同,https保证了数据传输的安全,但是配置复杂。
 
4 配置git告诉其他开发者你是谁,怎么联系你。
这些信息可以是全局的,也可以在某个项目中单独设置。
全局的配置文件位于~/.gitconfig
项目的配置文件位于/.git/config
 
5 配置git全局配置
git config --global user.name “yourname”
git config --global user.email you@site.com
单个项目配置
git config --local user.name “yourname”
git config --local user.email you@site.com
 
6 要把本地项目文件纳入GIT管理,直接在本地项目目录中执行
git init
git add *
git commit -a -m "sth."
新建GIT项目
在代码服务器上:
mkdir
cd
git init

 
7 把本地git的库导入代码服务器中
代码服务器新建GIT项目:
cd /work/projects
mkdir
cd
git init --bare

本地(必须是commit过了的):
cd
git remote add origin @server:/path/to/project
git push origin master
以后提交到代码服务器只需要:
git push

pull测试:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git pull
 
8 从repository中取出代码

使用ssh协议
git clone yourname@192.168.8.91:/work/projects/cerberus
上一条命令取出的代码,包含了该repository完整的修订记录,从项目的创建到目前的最新版本。分布式就表现在这个地方,远程服务器上是一个完整的repository,你取出的代码是完整的,另一名开发者取出的代码也是完整。
 
9 git的常用命令查看提交的日志信息
git log
查看项目中所有文件的状态
git status
增加了哪些文件
修改了哪些文件
哪些文件没有被git追踪(通常是自动生成的文件)

 
10 git的常用命令添加文件到项目中
git add
删除项目中的文件
git rm
重命名
git mv
可以是一个文件,也可以是一个目录。对于add,会自动添加该目录下的所有文件。对于rm,加上-r参数可以删除整个目录。
 
11 git的常用命令只提交新增的文件
git commit
提交新增的文件,以及修改过的文件
git commit -a
git commit 需要填写日志,如果为空,git放弃本次提交。git 的日志对格式有一定的要求,但不是强制的。
 
12 git 的日志格式short description (lesser than 50 letters)

detail description

* file1: yourchanges
* file2: yourchanges

 
13 更新和提交更新代码
git pull

错误:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull ').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "master"]
    remote =
    merge =

    [remote ""]
    url =
    fetch =

See git-config(1) for details.
解决:
Under [branch "master"], try adding the following to the repo's Git config file (.git/config):

[branch "master"]
    remote = origin
    merge = refs/heads/master
或者:
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
http://stackoverflow.com/questions/658885/how-do-you-get-git-to-always-pull-from-a-specific-branch
把修改后的代码提交到远程repository
git commit -a #提交 到本地git repository
git pull #取出远程repository中最新的代码
可能需要合并冲突
git push #提交到远程 repository
git push出错:
fatal: No destination configured to push to.
解决:
$ git remote add origin yourname@192.168.8.91:/work/projects/cerberus
// to push the master branch to the origin remote we added above:
$ git push origin master
// after that you can just do:
$ git push
http://blog.csdn.net/brave_heart_lxl/archive/2010/04/20/5507099.aspx

 
14 git checkoutgit checkout可以取出以下对象
一个版本
一个分支
一个文件
 
15 git 的 版本号git 使用一个40位数字来表示版本号;
HEAD 表示本地仓库的当前版本号
HEAD^表示本地仓库的上一个版本号
取出某个特定版本:
git checkout e9e986b7f1b7b1a0acf4b919e23e929705a8a209
git check “HEAD^”
 
16 放弃修改放弃某个文件或目录的修改
git checkout
放弃所有修改,回到某一个版本
git reset HEAD
git reset --hard HEAD
 
17 版本回退有时候会发现新的代码根本不能工作,我们可能需要回退到一个旧的版本,有两种方式:
git revert HEAD
这种方式会记录下错误的修改,以及回滚的历史
git reset “HEAD^”
这种方式直接放弃当前的版本,退回上一个版本,不会记录所做的修改。
 
18 分支管理查看分支列表
git branch
git branch -a #查看包括远程repository在内的所有分支
创建并切换到新的分支
git branch
git checkout
或者
git checkout -b
 
19 删除分支:

如果分支已经合并到master
git branch -d
如果分支还没有被合并,可以强制删除
git branch -D
不能删除当前的分支
 
20 合并分支git merge
git 会尝试自动合并分支中冲突的内容
如果合并失败,会在文件中以diff的形式显示两者的差异,用户需要手动解决冲突
 
21 当合并失败git会提示那些冲突无法自动合并,有两种解决方法:
手工修改这些文件,合并其中的内容,然后commit。
使用当前分支,或者要合并分支的内容,然后提交
git checkout --ours
git checkout --theirs