git使用心得

1.在本地可以建任意分支,只要不往服务器git push就不影响服务器的内容。

2.在使用git push的时候,如下:

git push file:////home/hebo/work/testgit/ master

这样对本地的修改,只会master分支推送到服务器上,其它的不影响

但是如果需要把其它的本地维护的分支推送到服务器,需要

git push file:////home/hebo/work/testgit/ local_hebo,本地也必须存在local_hebo的分支

 

疑问:

1.为什么在自己建的git工程中,可以使用git branch查看分支,而在android的工程中使用git branch查看的分支为空?

2.为什么自己无法建立remote/umg/froyo这样子的分支,而android可以

3.repo init -u git://android.intel.com/manifest -b froyo -m prod-bb表示什么?

 

使用以上例子做如下实验:

1.  git init m2doc
创建一个空的m2doc库,以做实验用ll查看,可以看到只有一个.git目录。

 

2. git remote add hb /home/hebo/work/test_hebo_git/m2doc.git/

添加一个远程仓库的标签,用此标签进行管理,可以使用git remote show查看,生成了一个hb的远程仓库在本地的标签,使用git remote show hb查看,生成如下信息,此时show命令会通过网络与远程仓库通信:
* remote hb
  Fetch URL: /home/hebo/work/test_hebo_git/m2doc.git/
  Push  URL: /home/hebo/work/test_hebo_git/m2doc.git/
  HEAD branch: master
  Remote branches:
    local_sub_local new (next fetch will store in remotes/hb)
    master          new (next fetch will store in remotes/hb)


此信息表示, 抓取的远程的地址,HEAD分支,相当于master分支,远程存在local_sub_local分支,以及存在master分支,如果使用git clone会自动创建一个original的标签

 

3. git fetch hb

从远程仓库同步抓数据到本地的仓库,本地建立了一个和远程仓库完全一样的镜象。

remote: Counting objects: 686, done.
remote: Compressing objects: 100% (663/663), done.
Receiving objects: 100% (686/686), 1020.29 KiB, done.
remote: Total 686 (delta 132), reused 0 (delta 0)
Resolving deltas: 100% (132/132), done.
From /home/hebo/work/test_hebo_git/m2doc
 * [new branch]      local_sub_local -> hb/local_sub_local
 * [new branch]      master     -> hb/master

此时,才真正从远程仓库取数据下来同步到本地的仓库,可以看到 local_sub_local对应远程的 hebo/local_sub_local,master对应的远程下的hb/master

 

4.使用git checkout hb/local_sub_local

会出现如下提示Note: checking out 'hb/local_sub_local'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

表示当前处于"detached HEAD"的状态,在此状态可能查看,实验,用git branch
可以看到出现如下提示:* (no branch),表示本地还没有确定的分支。

 

5. git checkout local_sub_local

从本地库拉出一个local_sub_local分支,使用 git branch
* local_sub_local

可以看到当前处于 local_sub_local的分支。

 

6. git checkout master

从本地拉出一个master的分支,使用git branch
  local_sub_local
* master
可以看到当前处于master的分支,本地有local_sub_local以及master两个分支

 

7.  git checkout hb/master

从本地拉出一个空分支,此分支是临时的,本地的修改无法保存。

再次使用git checkout hb/master,然后通过git branch
* (no branch)
  local_sub_local
  master
可以看到当前双处于远程的no branch分支。

 

8. git diff的使用

git diff是一个强大的比较版本的工具,可以对本地与本地缓存,远程分支,tags标签进行比较

例如: git diff --stat remotes/umg/froyo-stable remotes/umg/froyo-prod-bb

             别外可以使用git diff sha1 sha2 path比较某个目录下的不同,也可以在某个目录下使用 git diff  --relative比较当前目录下的不同文件。

            可以在一个工作树中使用git diff进行比较,也可以在一个仓库中直接使用git diff branch_name1 branch_name2进行比较,比较是基于

            仓库的,其本质是都是基于HASH值,如果本地的仓库没有更新,是无法比较本地创库没有,而远程创库有的更新的节点的。

 由此引申开来,此质的remote/umg/branch_name其实对应的是本地的仓库,只要在使用git remote show orginal的时候,才会发起网络连接,可以看到远程是否有更新。如果有更新,会显示 (next fetch will store in remotes/umg)

 

9. 对于存在分支与本地目录同名的的信息

可以使用git diff master test3 --

在test3后面加一个--表示这是一个分支,而不是一个路径

 

10. git rev-parse 命名是一个底层命令,我们偶尔要将某些称谓翻译成对象名的时候非常有用。 

$ git rev-parse origin
e05db0fd4f31dde7005f075a84f96b360d05984b

用此可以得到分支分支,tag对应的HASH值。

 

11. git diff的使用

Git 比较不同版本文件差异的常用命令格式:

  • git diff                                       查看尚未暂存的文件更新了哪些部分
  • git diff filename 查看尚未暂存的某个文件更新了哪些
  • git diff –cached                    查看已经暂存起来的文件和上次提交的版本之间的差异
  • git diff –cached filename 查看已经暂存起来的某个文件和上次提交的版本之间的差异
  • git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9 b8e7b00c02b95b320f14b625663fdecf2d63e74c 查看某两个版本之间的差异
  • git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9:filename b8e7b00c02b95b320f14b625663fdecf2d63e74c:filename 查看某两个版本的某个文件之间的差异

12. git checkout

      在git branch new_branch之后,git checkout new_branch相当于从本地当前分支上建立分支,工作树中的内容与当前的分支一致,如果要更新内容,需要从远程分支中merge到当前的分支,git merge origin/branch_name, 如果new_branch名与remote中的一致,在git remote show origin的时候,可以看到

 Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    hb     pushes to hb     (up to date)
    master pushes to master (up to date)

如果当前的new_branch名与remote中的不一致,在git remote show origin的时候,可以看到:

  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
可以看到,此时新的分支不能直接push到服务器上

如果是通过git checkout -track origin/branch_name,此命令相当于git checkout -b branch_name origin/branch_name,

  Local branches configured for 'git pull':
    hb     merges with remote hb
    master merges with remote master
  Local refs configured for 'git push':
    hb     pushes to hb     (up to date)
    master pushes to master (up to date)

如果 git checkout -b new_bracn origin/branch_name,此时会显示,another_branch不会直接push到服务器。

 Local branches configured for 'git pull':
    another_branch merges with remote hb
    hb             merges with remote hb
    master         merges with remote master
  Local refs configured for 'git push':
    hb     pushes to hb     (up to date)
    master pushes to master (up to date)

posted @ 2015-04-25 18:15  虫虫乐乐  阅读(237)  评论(0编辑  收藏  举报