Git Basics

Git 的好处还是看官方文档吧,我就不瞎扯了。我只是按照我个人的习惯简单走一下流程,记录一下。

1. 创建项目目录,建立 Git 仓库。

$ mkdir myproject

$ cd myproject

$ git init # 初始化 git 仓库
Initialized empty Git repository in /home/yuhen/myproject/.git/


从此,myproject 就是工作目录,而 git 创建的 .git 隐藏目录就是代码仓库了。

2. 建立忽略配置文件。

$ cat > .gitignore << end
> *.[oa]
> *.so
> *~
> !a.so
> test
> tmp/
> end


支持匹配符和正则表达式,支持 "#" 注释,支持用 "/" 结尾表示路径。还可以用 "!" 取反,比如前面的 "*.so" 规则忽略所有 .so 文件,然后用 "!a.so" 表示特例。balabala... 等等...

3. 好了,开始第一个 commit 吧。

$ git status # 查看当前 track 状态
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
nothing added to commit but untracked files present (use "git add" to track)

$ git add . # 添加所有新增文件

$ git commit -am "init" # 提交必须提供一个注释,否则无法执行。
[master (root-commit) 3725c8d] init
 1 files changed, 5 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore


4. 创建第一个工作分支。

$ git branch yuhen # 创建新的分支

$ git branch # 查看当前所有分支
* master
  yuhen

$ git checkout yuhen # 切换到新的工作分支
Switched to branch 'yuhen'

$ git branch # 确认一下
  master
* yuhen


也可以用 "git checkout -b yuhen" 一次完成创建和切换分支的工作。

$ git checkout -b yuhen
Switched to a new branch 'yuhen'

$ git branch
  master
* yuhen


5. 开始工作,添加或编辑文件。

$ cat > main.c << end
> #include <stdio.h>
> #include <stdlib.h>
> 
> int main(int argc, char* argv[])
> {
>     return EXIT_SUCCESS;
> }
> 
> end

$ mkdir doc
$ cd doc

$ cat > README << end
> readme file
> end

$ cat > INSTALL << end
> install...
> end

$ cd ..

$ git status # 我们可以查看所有被 git 跟踪的改变
# On branch yuhen
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    doc/
#    main.c
nothing added to commit but untracked files present (use "git add" to track)


没问题的话,就提交到代码仓库吧。

$ git add . # 添加新增文件

$ git commit -am "main.c, doc/" # 提交
[yuhen 42d7d10] main.c, doc/
 3 files changed, 10 insertions(+), 0 deletions(-)
 create mode 100644 doc/INSTALL
 create mode 100644 doc/README
 create mode 100644 main.c

$ git log # 查看提交日志
commit 42d7d10e008f34bb6b7c9824cbfa6b1c84c008a4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:03:06 2010 +0800

    main.c, doc/

commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 16:55:50 2010 +0800

    init


6. 合并工作分支到 master。

$ git checkout master # 切换回主分支
Switched to branch 'master'

$ git branch
* master
  yuhen

$ git merge yuhen # 将 yuhen 工作分支合并到主分支
Updating 3725c8d..42d7d10
Fast forward
 doc/INSTALL |    1 +
 doc/README  |    1 +
 main.c      |    8 ++++++++
 3 files changed, 10 insertions(+), 0 deletions(-)
 create mode 100644 doc/INSTALL
 create mode 100644 doc/README
 create mode 100644 main.c

$ git branch -d yuhen # 删除工作分支
Deleted branch yuhen (was 42d7d10).


今天的工作完成了,看看日志。

$ git log
commit 42d7d10e008f34bb6b7c9824cbfa6b1c84c008a4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:03:06 2010 +0800

    main.c, doc/

commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 16:55:50 2010 +0800

    init


用一个分支进行工作是个好主意,因为 git 有句话叫做 "丢掉一个烂摊子总比收拾一个烂摊子强"。

7. 如果我们发现某次提交有问题,我们可以恢复到以前的某个提交版本。

$ vim main.c # 编辑文件

$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    printf("Hello, World!/n");
    return EXIT_SUCCESS;
}

$ git status 
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   main.c
#
no changes added to commit (use "git add" and/or "git commit -a")

$ git commit -am "main.c changed" # 提交
[master a79baec] main.c changed
 1 files changed, 1 insertions(+), 0 deletions(-)

$ git log # 提交成功
commit a79baecfecbcedb8545a7ff4aeebba3fe653efa4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:15:51 2010 +0800

    main.c changed

commit 42d7d10e008f34bb6b7c9824cbfa6b1c84c008a4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:03:06 2010 +0800

    main.c, doc/

commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 16:55:50 2010 +0800

    init

$ git reset HEAD^ # 恢复到上次某个提交状态,可以是 HEAD^、HEAD~4、commit-id 的头几个字母,还可以是 tag。
main.c: locally modified
yuhen@yuhen-desktop:~/myproject$ git log
commit 42d7d10e008f34bb6b7c9824cbfa6b1c84c008a4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:03:06 2010 +0800

    main.c, doc/

commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 16:55:50 2010 +0800

    init

$ git status # 看到没有,默认 reset 模式是 --mixed,会保留文件修改。还可以用 --hard 放弃这些修改。
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   main.c
#
no changes added to commit (use "git add" and/or "git commit -a")

$ cat main.c # 修改还被保留着呢。
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    printf("Hello, World!/n");
    return EXIT_SUCCESS;
}


8. 工作了 n 天了,总算进入某个阶段性版本了。

$ git tag v0.9 # 创建简单标签

$ git tag # 显示所有标签
v0.9

$ git log v0.9 # 用标签显示提交状态
commit 42d7d10e008f34bb6b7c9824cbfa6b1c84c008a4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:03:06 2010 +0800

    main.c, doc/

commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 16:55:50 2010 +0800

    init

$ git show --stat v0.9 # 用标签显示提交基本信息
commit 42d7d10e008f34bb6b7c9824cbfa6b1c84c008a4
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date:   Tue Apr 20 17:03:06 2010 +0800

    main.c, doc/

 doc/INSTALL |    1 +
 doc/README  |    1 +
 main.c      |    8 ++++++++
 3 files changed, 10 insertions(+), 0 deletions(-)


-------------- 分隔线 ---------------------

推荐 《Pro Git》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值