GIT - 日常操作(1)

这篇博客将开始介绍一些GIT的常用操作与命令,从clone项目,init项目,pull/push 等操作开始

万里长征第一步 – GIT项目的来源

第一招 - 拿来主义

Github上面如繁星多的项目,随便取一个下来就行。如何取呢? Clone来也! 打开命令行,跳转到你想要的目录,执行clone的命令:

$ git clone https://github.com/notechsolution/gitDemo.git mygitdemo

以上命令告诉GIT:用https协议从github.com/notechsolution/gitDemo.git 把代码克隆下来,并且放在本地的 mylibgit 目录下面。

C:\workspace\git>git clone https://github.com/notechsolution/gitDemo.git mygitdemo
Cloning into 'mygitdemo'...
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 11 (delta 0), reused 8 (delta 0), pack-reused 0
Unpacking objects: 100% (11/11), done.
Checking connectivity... done.
Checking out files: 100% (6/6), done.

从上面的clone时GIT详尽的log可以看到步骤大致如下:
1. 创建mylibgit目录
2. 计算https://github.com/libgit2/libgit2 有多少个对象。共有11个GIT对象
3. 下载这11个对象到mylibgit/.git目录下面
4. 从压缩对象里面 解压出master branch下的6个文件(检出分支,checkout branch master)

关于checkout分支,clone的时候如果不指明,则默认checkout master分支,如果需要,则可以在clone命令的最后用-b参数 指明:

$ git clone https://github.com/notechsolution/gitDemo.git mygitdemo -b test

第二招 - 自给自足

如果你不想拿别人的代码,而是想自己从头开始,或者从已有的文件夹开始GIT项目,那么你可以用init命令进行GIT项目的初始化:

$ git init

这个命令会为你创建.git的目录,并生成一些基本的信息。

Initialized empty Git repository in C:/workspace/git/startuproject/.git/

但是没有生成master branch,也没有任何commit的信息。需要你自己做第一个commit(root commit),这时GIT会为你自己生成master branch。

C:\workspace\git\startuproject>git add README.txt

C:\workspace\git\startuproject>git commit -m "initialization"
[master (root-commit) 6b999bf] initialization
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.txt

万里长征第二步 – 贡献代码

  • 配置码农信息
    基本在所有的版本控制系统中,每一个commit都有4W 要素(Who-When-Why-What)
    • Who: 谁做了这些代码改动,并提交了commit (正常情况下,做出代码改动,跟提交代码是同一个人,而且这也算是一个Best Practise), 如下例子中,”notechsolution [email protected]”就是这个who
    • When: 什么时候做的这个commit (请注意:这个时间是做”git commit” 命令的时候,而不是你push的时间)。例子中,”Tue Jan 12 22:32:06 2016 +0800” 就是commit的when
    • Why: 这个commit是为了什么而做的。也就是commit message,在下面例子中,这个commit就是为了 “upload script files”
    • What: 列出这个commit究竟做了哪些改动,增删改了哪些文件的内容。下面例子中就添加了5个文件。
   C:\githome\mygitdemo>git show a2354235501b99a929d6579ff4e18880a9a97e7d -1 --name-only
    commit a2354235501b99a929d6579ff4e18880a9a97e7d
    Author: notechsolution <notech@126.com>
    Date:   Tue Jan 12 22:32:06 2016 +0800

        upload script files

    script/appveyor-mingw.sh
    script/cibuild.sh
    script/coverity.sh
    script/install-deps-osx.sh
    script/toolchain-mingw32.cmake

以上4W要素中,when是系统生成的,why是用户自己写的,what是GIT 从暂存区detect出来的,而who是从配置文件读取的。
从哪个文件呢?首先GIT会查看你项目里面 “.git\config”这个文件,如果没有配置,则再读取用户目录(%HOMEPATH%)下的.gitconfig文件
我们可以直接把码农的用户名跟email配置写到这些文件里面,也可以通过命令行写进去。通过”git config –add”添加的配置参数时,该参数加到项目里面的”.git\config”,而如果是通过”git config –add –global”的话,那么将添加到”%HOMEPATH%.gitconfig”里面。”%HOMEPATH%/.gitconfig”里面的配置是全局生效的,也就是所有的项目都共享这个config。

C:\Users\notechsolution>git config --global --add user.name notechsolution

C:\Users\notechsolution>git config --global --add user.email [email protected]

C:\Users\notechsolution>git config --list
core.symlinks=false
......
user.name=notechsolution
[email protected]
  • 拿到最新代码
    建议大家都养成这个习惯,在你开始贡献代码之前,先拿到最新代码,这样会给你减少很多代码冲突的问题。
    拿到最新代码一般用pull的命令就可以了:
git pull origin

这里的origin其实是可以省的,因为大多数人的git repository里面只有一个remote,所以即使不写也木有关系的。

C:\workspace\git\mygitdemo>git pull
From https://github.com/notechsolution/gitDemo
 * [new branch]      test       -> origin/test
Already up-to-date.
  • 修改代码
    拿到最新代码之后,我们就可以开始代码修改了。
    首先我们添加一个文件”demoCommit.txt”,里面只有一句话

    C:\workspace\git\mygitdemo>echo "this is demo commit" >>demoCommit.txt
  • 加到暂存区
    到了这里,可能你修改的代码也差不多到一段落了,你想看看做了哪些改动,并想暂时保存起来。
    首先是看看有哪些改动:
   C:\workspace\git\mygitdemo>git status
On branch master
Your branch is up-to-date with 'origin/master'.
changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
         modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")

GIT告诉我们说,修改了一个叫”README.md”的文件,并叫我们可以通过”git add file”的命令把修改的文件加到暂存区,或者通过”git checkout file”取消该文件的修改。

现在我们就来将文件加到暂存区里面去,加完之后查看一下当前GIT代码库的状态:

  C:\workspace\git\mygitdemo>git add README.md

  C:\workspace\git\mygitdemo>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md
  • commit
    再三思考之后,我们需要改动的文件都已经改好了,这时我们可以给代码做个commit。commit的命令很简单”git commit -m ‘message’”。
  C:\workspace\git\mygitdemo>git commit -m "Common-Readme-add dummy txt"
    [master 023ab32] Common-Readme-add dummy txt
     1 file changed, 1 insertion(+)

命令跟提示信息都很简单明了,就不做过多介绍。但关于commit的message想多说几句。建议commit的message要做到能让别人一看就知道 你做了什么事,比如上面的message可以看到:在Common模块的Readme里面添加了一个dummy的文本。因为大多数情况下,开发人员更多的是使用GIT的GUI工具进行工作的,而从GUI工具看到项目commit信息就是message信息。如果这些commit的message信息足够清楚的话,那么也就省去别人点进去看明细的功夫。

这里写图片描述

  • 提交代码
    好啦,现在万事俱备,只差提交了。push的命令也是很简单的:
  C:\workspace\git\mygitdemo>git push origin
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 364 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/notechsolution/gitDemo.git
   a235423..023ab32  master -> master

其中origin是指我们的代码将提交到哪里去。默认我们clone一下项目下来的时候,GIT会帮我们生成一个origin的配置参数,并且将clone的URL作为它的值,我们可以通过下面这个命令看到origin指的是什么

C:\workspace\git\mygitdemo>git remote -v
origin  https://github.com/notechsolution/gitDemo.git (fetch)
origin  https://github.com/notechsolution/gitDemo.git (push)

当然,由于我们的本地代码库只配了一个远程的代码库,所以push的时候我们不指明origin也是可以的。

从上面push命令的提示信息看,我们已经成功地将master branch的代码提交的github上gitDemo 代码库的master 分支上了。

至此,我们就完成了贡献代码的动作了!打完收工!

关于pull/commit命令等还有很多比较复杂的用法,我们留待后面专题的时候再展开来讨论   –Notechsolution

猜你喜欢

转载自blog.csdn.net/notechsolution/article/details/50513120
今日推荐