关于git:Git-笔记1

0次阅读

共计 2153 个字符,预计需要花费 6 分钟才能阅读完成。

Git 的 origin 和 master 剖析

http://lishicongli.blog.163.c…

首先要明确一点,对 git 的操作是围绕 3 个大的步骤来开展的(其实简直所有的 SCM 都是这样)

  1. 从 git 取数据(git clone)
  2. 改变代码
  3. 将改变传回 git(git push)

这 3 个步骤又波及到两个 repository,一个是 remote repository,再近程服务器上,一个是 local repository,再本人工作区上。其中

1, 3 两个步骤波及到 remote server/remote repository/remote branch,

2 波及到 local repository/local branch。git clone 会依据你指定的 remote server/repository/branch,拷贝一个正本到你本地,再 git push 之前,你对所有文件的改变都是在你本人本地的 local repository 来做的,你的改变 (local branch) 和 remote branch 是独立(并行)的。Gitk 显示的就是 local repository。

在 clone 实现之后,Git 会主动为你将此近程仓库命名为 origin(origin 只相当于一个别名,运行 git remote –v 或者查看.git/config 能够看到 origin 的含意),并下载其中所有的数据,建设一个指向它的 master 分支的指针,咱们用(近程仓库名)/(分支名) 这样的模式示意近程分支,所以 origin/master 指向的是一个 remote branch(从那个 branch 咱们 clone 数据到本地),但你无奈在本地更改其数据。

同时,Git 会建设一个属于你本人的本地 master 分支,它指向的是你刚刚从 remote server 传到你本地的正本。随着你一直的改变文件,git add, git commit,master 的指向会主动挪动,你也能够通过 merge(fast forward)来挪动 master 的指向。

$git branch -a (to show all the branches git knows about)

  • master

remotes/origin/HEAD -> origin/master

remotes/origin/master

$git branch -r (to show remote branches git knows about)

origin/HEAD -> origin/master

origin/master

能够发现,master 就是 local branch,origin/master 是 remote branch(master is a branch in the local repository. remotes/origin/master is a branch named master on the remote named origin)

$git diff origin/master master(show me the changes between the remote master branch and my master branch).

须要留神的是,remotes/origin/master 和 origin/master 的指向是雷同的

$git diff origin/master remotes/origin/master

git push origin master

origin 指定了你要 push 到哪个 remote

master 其实是一个“refspec”,失常的“refspec”的模式为”+<src>:<dst>”,冒号前示意 local branch 的名字,冒号后示意 remote repository 下 branch 的名字。留神,如果你省略了 <dst>,git 就认为你想 push 到 remote repository 下和 local branch 雷同名字的 branch。听起来有点拗口,再解释下,push 是怎么个 push 法,就是把本地 branch 指向的 commit push 到 remote repository 下的 branch,比方

$git push origin master:master (在 local repository 中找到名字为 master 的 branch,应用它去更新 remote repository 下名字为 master 的 branch,如果 remote repository 下不存在名字是 master 的 branch,那么新建一个)

$git push origin master(省略了 <dst>,等价于“git push origin master:master”)

$git push origin master:refs/for/mybranch (在 local repository 中找到名字为 master 的 branch,用他去更新 remote repository 上面名字为 mybranch 的 branch)

$git push origin HEAD:refs/for/mybranch(HEAD 指向当前工作的 branch,master 不肯定指向当前工作的 branch,所以我感觉用 HEAD 还比 master 好些)

$git push origin :mybranch(再 origin repository 外面查找 mybranch,删除它。用一个空的去更新它,就相当于删除了)

正文完
 0