[关闭]
@zhengyuhong 2016-02-04T03:21:52.000000Z 字数 6063 阅读 1086

git

posts


title: git
categories: git

toc: true

git add

  1. git add index.xml #把刚刚修改过的(新建的)index.xml添加索引

git add -p

  1. git add -p交互式添加(Stage this hunk [y,n,q,a,d,/,s,e,?]?)。
  2. # 一般使用y,n,q,a,d,具体含义如下
      y - stage this hunk
      n - do not stage this hunk
      q - quit; do not stage this hunk nor any of the remaining ones
      a - stage this hunk and all later hunks in the file
      d - do not stage this hunk nor any of the later hunks in the file
      g - select a hunk to go to
      / - search for a hunk matching the given regex
      j - leave this hunk undecided, see next undecided hunk
      J - leave this hunk undecided, see next hunk
      k - leave this hunk undecided, see previous undecided hunk
      K - leave this hunk undecided, see previous hunk
      s - split the current hunk into smaller hunks
      e - manually edit the current hunk
      ? - print help

git commit

  1. git commit -m "index.xml的提交留言"
  2. # 简明扼要总结index.xml的修改原因,提交

git push

  1. git push -u remote_repo lcoal_branch:remote_branch
  2. # 将本地分支local_branch推送到远程仓库remote_repo中的远程分支remote_branc,
  3. # 可以省略remote_branch,即推送到与本地分支同名的远程分支
  4. # 关于-u选项,只需要在第一次时推送时加选项-u
  5. # 作用理解为track branch,在往后git pull的时候知道从哪一个远程仓库、哪一个远程分支,合并到哪一个本地分支,第一次加-u就是做一个登记记录,以后就不需要了。

The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do
当一天的工作结束后,需要把代码同步到远程服务器的版本库,譬如需要把本地的master分支同步到origin指示的服务器地址的master分支。可以如下

  1. git push -u origin master:master #或者
  2. git push -u origin master #remote_branch默认为与local_branch同名的远程版本库的分支

删除远程分支

  1. git push origin :test
  2. # 推送一个空白分支覆盖远程分支test,就是删除远程分支test

关于origin,git remote -v可以查看到当前本地版本库的远程服务器链接。想象一下,一些公司有自己的服务器,平时开发的时候直接把代码同步自己的服务器,但是有一些项目代码公司想公开给大众,所以同步到想github一些代码托管社区,如此大家都可以获取到公开的源码,这时候remote就代码分支需要同步到远程的地址位置。如需要把代码同步到公司服务器可以设置一个新的远程服务器地址关于远程主机设置等可以参考阮一峰的Git远程操作详解

  1. git remote -v //使用-v选项,可以参看远程主机的网址
  2. git remote add <主机名> <网址> //命令加上主机名,可以查看该主机的详细信息。

  对于一般使用github,coding.net code.oschina.net等公共代码托管服务,我一般是在网页创建一个空的版本库,然后使用git clone git@github.com:jquery/jquery.git克隆到本地,已经有一个远程服务器地址origin默认设置好了,所以对于普通个人开发,一般都是使用默认的origin即可。简单来说,知道origin代表一个远程地址,代码就托管在那就行了。

git rm

删除当前分支索引中文件

  1. git rm index.xml #将index.xml 移除索引暂存区,但是index.xml还在当前目录,只是没有加入分支

git mv

  有时候想把某一个文件重命名,一般重命名之后git就捕捉到一个新的未添加文件,同时原来那么文件就消失了,虽然可以重新git add把重命名后的文件添加到分支,但是提交记录没有转移到重命名后的文件。git rm提供一个同步的方法

  1. git mv index.xml hello.xml

将分支索引中的index.xml重命名为hello.xml,同时工作区的index.xml也被重命名为hello.xml

git fetch

  每一天开始工作,需要把远程版本库的项目同步到本地工作区,使用git fetch,git merge。

  1. git fetch origin remote_branch_name:local_branch_name

  fetch的作用就是把远程分支remote_branch_name同步到本地的一个分支lcoal_branch_name,譬如想将远程的master分支同步到本地的master分支,fetch再merge

  1. git fetch origin master:origin_master
  2. git branch #可以查到到多了一个origin_master分支,使用git checkout origin_master 切换分支查看分支内容

  git fetch origin remote_branch_name:local_branch_name就可以把远程分支同步到本地,再将origin_master分支与本地的master分支merge,merge之后再push到服务器的master分支。

git merge

  merge两分支可能并不太顺利,如果修改了同一个文件的同一部分,Git 就无法干净地把两者合到一起(译注:逻辑上说,这种问题只能由人来裁决)。假设现在已经git fetch origin master:origin_master,可以理解origin_master就是服务器的master的一个拷贝。当什么时候有冲突产生呢?
  本地的master不是origin_master的一个子集,那git怎么判断master是origin_master的子集?记得每一次git commit都会产生一个commit SHA-1标记这一次的提交记录,所以整一个分支由commit SHA-1码组成的序列。git就是通过commit SHA-1来判断两个分支是否有子集关系。只有当本地的master对应的commit SHA-1序列与origin_master的commit SHA-1序列公共位置的commit SHA-1一一相同,此时master是origin_master的子集。
  git在git merge前需要把当前已经修改过的文件提交使得工作区干净,具体原因在上面怎么判断子集就可以看到了。
  了解了冲突的产生原因之后,看看怎么解决冲突。切换到master分支

  1. git diff origin_master //比较当前分支与origin_master得到不同

  当有冲突的时候先显示那些文件有冲突,否则就是空白无冲突,直接使用git merge origin_master合并到当前分支。
当有冲突时,同样使用git merge origin_master,只是会产生如下信息提示用户去修改解决冲突。

  1. Auto-merging index.xml
  2. CONFLICT (content): Merge conflict in index.xml
  3. Automatic merge failed; fix conflicts and then commit the result.

  打开index.xml冲突文件,文件由======= 隔开的上半部分,是 HEAD(HEAD指向当前版本库的分支,即 master 分支)中的内容,下部分是origin_master内容。人工解决冲突方法,就是自行编辑index.xml再git add index.xml git commit,就像修改普通文件,会弹出一个界面,提示编写提交记录留言,写完退出即可。使用git log会看到git merge,但是此提交留言并不会同步到远程服务器地址,供本地查看。而origin_master的提交记录则会合并到当前分支。

  1. <<<<<<< HEAD
  2. <title>Hello World in Git@coding.net!!!</title>
  3. =======
  4. <title>Hello World in Git@coding.net!</title>
  5. >>>>>>> origin_master

  关于git merge --squash another --squash 选项的含义是:本地文件内容与不使用该选项的合并结果相同,但是不提交、不移动HEAD,因此需要一条额外的commit命令。其效果相当于将another分支上的多个commit合并成一个,放在当前分支上,原来的commit历史则没有拿过来。git merge --squash origin_master不能与git fetch origin master:origin_master搭配共用。具体原因以后再写,现在我还没搞懂,总是隐约感到不能这样子做。
  最后将临时分支origin_master删除,git branch -d origin_master
  关于分支合并可以参考Git 分支 - 分支的新建与合并

git checkout

git checkout branche_name

  切换当前分支或者在当前分支的时间点下创建分支

  1. git checkout RB_1.0 //在当前分支下,创建发布分支release branch
  2. //做最后的修改,譬如测试
  3. git add "README.md"
  4. git commit -m "README.md的提交留言"

git checkout -- file

  1. git checkout -- index.xml

  将index.xml 恢复到上一个提交状态,就是把上一次commit之后的全部都撤销。

git tag

  现在准备发布1.0版本,要给它打上一个标签,意味着版本库的历史中标记处特定的点(全局的,不限定在哪一个分支下)。标签不同于SHA值,tag是人类可以理解的记号。如下例子,这个tag 1.0是全局的,不限定在RB_1.0分支下,以后即便是删除了RB_1.0分支。tag 1.0依然是有效的。

  1. git tag 1.0 RB_1.0 #将分支RB_1.0打上tag 1.0
  2. git tag #输出最近tag 1.0

git push -u origin --tags

  1. git push -u origin --tags #提交本地tags到远程服务器

git tag -d

  1. git tag -d <tagname> #删除本地tag

git push origin --delete tag

  1. git push origin --delete tag <tagname> #删除远程服务器的tag

git branch

git branch new_branch tag

根据全局标签tag创建新的分支

  1. git branch new_branch tag

  如上所述,tag不依赖分支,是全局的tag。所以直接使用
example

  1. git branch RB_1.0.1 1.0 //创建新的分支,用于给1.0.x打补丁

git branch new_branch remote/remote_branch

根据远程版本库的分支创建新分支

  1. git branch new_branch remote/remote_branch
  2. #exmaple
  3. git branch origin_master origin/master #等价于
  4. git fetch origin master:origin_master

git branch -d

删除已经合并后的分支

  1. git branch -d branch_name

如果使用上述去删除尚未合并的分支,会有提示让用户使用—D选择强制删除。

  1. git branch -D branch_name

更多帮助查看,git branch --help

git branch -m

  1. git branch -m old_branch_name new_branch_name

git reset

版本回滚

  1. git log # 查看 commit日志
  1. commit 24af57111298aa37dc9d2e2e5ab6b15c5ecca791
  2. Author: 江林羽 <371582812@qq.com>
  3. Date: Mon Feb 1 15:04:31 2016 +0800
  4. STDERR_LOG->FATAL_LOG
  5. commit c0056caf2eb26452f22d58028d9d4018b46f0000
  6. Author: 江林羽 <371582812@qq.com>
  7. Date: Sat Jan 30 22:51:04 2016 +0800
  8. Makefile

回滚到24af57111298aa37dc9d2e2e5ab6b15c5ecca791,执行

  1. git reset --hard 24af57111298aa37dc9d2e2e5ab6b15c5ecca791

使用git log时会发现HEAD指向了回滚指定的commit。如果回滚错了,还可以再复原,使用git log -g可以查到包含回滚的commit(回滚也算一次commit),再一次指定commit即可。

git archive

  1. git archive --format=tar --prefix=mydir-1.0/ 1.0 |gzip > mydir-1.0.tar.gz

  将分支打包,1.0是指明为归档的标签名称,mydir-1.0/是用户自定义的文件夹,记得添加斜杠,就是解压之后的文件夹名字,mydir-1.0.tar.gz也是用户自定义生成的压缩比名字

git stash

git stash 可用来暂存当前正在进行的工作。目前工作才进行到一半,又不想提交一个新的commit(随便提交一个commit会显得log比较混乱,)所以一般的话先把目前工作暂存,然后再转到其他工作如,git pull拉去最新代码命令,git subtree更新子目录命令,这些都要求工作区是干净的。git stash作用就是把当前修改的先暂存之前,回滚到最近一个提交的commit版本下,这时候就是一个干净的工作区,然后就可以执行git pullgit subtree等命令了。现在想恢复工作,执行git stash pop即可。

  1. # 正在写码,中途有突发事情需要一个干净的工作区才能执行
  2. git stash # 保存修改暂存栈,还原到到最近一个提交的commit版本,得到一个干净工作区
  3. # 处理突发事情
  4. git stash pop # 从暂存栈中还原工作环境
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注