Git牛比指南

关于什么是Git:任何一种入门指南,其开篇通常是给出定义,但关于什么是Git,网络上相关文档早已是汗牛充栋,在此重复造轮当然是个big no-no。只谈谈我对Git的最大感受,就是其兼具本地应用的敏捷,又足具云端应用的方便。本文将直接从实践角度开始,记录关于Git的Newbie’s Guide of Best Practices。

上手Git前,最好需要具备关于Build和Diff的前置知识,为此,可以看看阮一峰写的两篇文章:《Make 命令教程》与《读懂diff》以及进一步的《编译器的工作过程》。此外,鸟哥的相关教程也可重点细读。由此,可以看看msysGit GitHub项目上的wiki中所说的build environment与development environment。

如何获取Git软件

git-scm

gitglossary – A Git Glossary页面可以看到“SCM”的含义:Source code management (tool)

Git官方发布了官方的Git for Windows,但其只负责release git source code,需要用户自己去compile(编译)、build(构建),在windows下,这并非易事。

msysGit

参考StackOverflow上的解释,msysGit就是专门在windows下compile/build git source code的项目,其工作成果是可供windows用户直接下载安装的git binaries。而现在http://git-scm.com/downloads上下载的windows git其实也是msysGit的成果,这一点可以从http://git-scm.com/downloads上所下载的Git和从msysGit在GitHub或Google Code或上的项目主页上下载的git一致而得到验证(连文件名都是一样的)。值得一提的是Google Code的陷落:msysGit最初托管在Google Code,但Google Code于2015年中停止服务,因此msysGit将其项目迁移托管至GitHub。此外,msysGit项目也release msysGit binaries,这个应该就是在windows下用户自行编译build git for windows source code的工具。

  • 因此还想测试一下msysGit和git-scm的binaries安装之后都有什么区别。
  • 可以考虑去蒋鑫的git权威指南书里看看他的阐述
  • 感觉还是要在windows上的microsoft visual studio上看看编译等全过程,在linux上体会一下编程、编译的全过程(可在mac?xcode上练练)。

Git GUI

就像在GUI Clients页面上说的:

Git comes with built-in GUI tools for committing (git-gui) and browsing (gitk), but there are several third-party tools for users looking for platform-specific experience.

这就是说:

  • git scm释出git source
  • msysGit项目进一步在Windows环境下Build Git for Windows,其中还提供了git-gui、gitk等GUI工具;
  • GitHub for Windows之类的第三方GUI工具,则整合了Git for Windows,并提供了其他相关功能,打造了一个功能全面的图形化工具。

GitHub Desktop是个什么鬼:GitHub Desktop本身只是第三方GUI Client中的一种,这个可以在GUI Clients上的页面中可以看到。

Git中如何新建一个repo

local repo

直接git init

注意,在某文件夹下执行git init后,即在该文件夹下生成.git子文件夹,即可git bash中cd到此文件夹,运用各种git命令。

git clone

这个适用于将业已存在的remote repo clone至本地来。

remote repo

Graphical Web Interface

最初的Google Code和现在GitHub默认的方式都是这样的。

在GitHub上使用Graphical Web Interface新建repo时,有两种情况:

  1. 在GitHub上新建并init一个repo,然后git clone至本地。
  2. 在GitHub上新建一个空repo(注意不要勾选“Initialize with README”),然后根据页面提示,将本地repo与之关联,并将本地repo push过去。

Command Line Interface

关于如何在本地计算机上(当然是只能在本地计算机上啦,不然,你能跑到GitHub机房亲自操作它们的计算机?)运用命令行工具在GitHub服务器上新建repo,可以看看这篇这篇这篇

Git中Local repo与Remote repo的连接与Sync时的身份验证

关联

一言以蔽之——git remote add

git remote add命令只是将远程已存在的仓库与本地仓库关联起来的命令,并不能通过本地Bash凭空建立一个远程仓库。可以参看前述网页的回答:

The command git remote add origin https://github.com/lucycloud/NewRepo.git does not add a new repository on the remote machine. What it does is tell git on your current machine that it should expect a remote repository at the location you specified.

由于git remote add确实只是将本地repo与远程repo关联起来(这两个repo的名字都可以不一样),因此不会要求身份验证等。关联后,使用git push -u origin master推送时,才会要求身份验证。

在Git官方reference中,对git remote add的阐述是这样的:

Adds a remote named for the repository at . The command git fetch can then be used to create and update remote-tracking branches /.

突然想到,当年在Google Code上时,好像也是现在Web端新建了Codexercise repo,然后才用git remote add关联上来的

Sync时的身份验证

这个主要是与远程主机的要求有关。

最初我的连接是在Google Code上的,而在与Goolge Code通讯时,采取的验证方法是询问Google用户名以及Generated Key。

GitHub既可以使用SSH,也可以直接使用https的方式。其中,在使用SSH时,需要在本地生成公私密钥对,并将公钥上传至GitHub。而使用https方式时,只需询问GitHub用户名和密码就行了,并且可以使用credential helper来帮助记住密码,避免每次都输入。当在GitHub上使用了Two-factor Authentication时,则需要在setting页面生成Personal access token,本地push时,只需要把GitHub用户名换成Personal Token即可。

Git命令

Reference

  • git scm的官方doc,包括Reference、Pro Git等
  • Git Reference:Every page will also link to more in-depth Git documentation such as the official manual pages and relevant sections in the Pro Git book

值得一提的是GitHub Help,这当是最齐备的Git文档了,任何关于Git的有名的材料都能从这里链接得到。

常用命令

以下都是从廖雪峰老师的Git教程中看到的。

  • git config –global user.name “Your Name”
  • git config –global user.email “email@example.com”
  • git init
  • git add
  • git commit -m “message”
  • git status
  • git reset –hard commit_id
  • git log [–pretty=oneline]
  • git reflog
  • git checkout — file
  • git reset HEAD file
  • git rm
  • git remote add origin git@server-name:path/repo-name.git
  • git push -u origin master
  • git push origin master

常用命令辨析

git add . vs git add *

git add .是“递归添加”;git add *是“全部添加”。这两者的区别可以看这里

It is also common to recursively add all files in a new project by specifying the current working directory like this: git add .. Since Git will recursively add all files under a directory you give it, if you give it the current working directory, it will simply start tracking every file there. In this case, a git add . would have done the same thing as a git add README hello.rb, or for that matter so would git add *, but that’s only because we don’t have subdirectories which the * would not recurse into.

git pull vs git fetch

git pull=git fetch + git merge

GitHub名词解释补遗:

  • release
  • pulse
  • graphs
  • Github上的build/pass图标

折腾实录

复盘我在Google Code上的折腾:

$ cd F:/MyProjectFolder 
$ git init 
$ git config --global user.name "Your Name" 
$ git config --global user.email youremail@yourdomain.com 

$ git clone path-to-repository (git clone https://code.google.com/p/yet-another-test/) 

$ git add . 
$ git commit -m 'My First Commit' 

$ git push origin master

添加Google Code远程仓库
git remote add origin https://code.google.com/p/yet-another-test/
注意如果以前添加过的话,就会显示fatal: remote origin already exists.

如何比较远程仓库与本地仓库的异同?