强大的版本管理工具 Git

Git 简介

git 是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。是 linux 创始人 Linus Torvalds 为了帮助管理 linux 内核开发而开发的一个开放源码的版本控制软件。最初 git 的开发是为了辅助 linux 内核开发的过程,由于 git 免费而又超级好用,如今已经成为最流行的分布式版本控制系统,尤为是 2008 年, github 网站上线了,它为开源项目免费提供 git 存储,无数开源项目开始迁移至 github 。

Git 安装

提供了各类平台的安装方法,跟着说明安装便可。html

以 windows 系统为例,安装完后打开 git bash 命令行工具,输入 git --version 查看安装的 git 版本node

alsy@home-alsy MINGW64 /
$ git --version
git version 2.11.0.windows.3

打印出版本信息说明你已经成功安装 git 了linux

 Git 的相关设置

1. 自报家门,设置你的姓名和 email 

$ git config --global user.name "your_name"
$ git config --global user.email "your_email"

注意: git config 命令的 --global 参数,这时你这台电脑上全部的 git 仓库都会使用这个配置,固然你也能够针对某个仓库指定不一样的用户名和 email 地址。git

2. 设置 git 输出显示颜色,看起来更醒目

$ git config --global color.ui true

3. 关于 git config

查看仓库级的config,命令:git config --local -lgithub

查看全局级的config,命令:git config --global -lwindows

查看系统级的config,命令:git config --system -l缓存

注意配置文件的优先级是: 仓库 > 全局 > 系统bash

查看当前生效的配置,命令:git config -l,这个时候会显示最终三个配置文件计算后的配置信息服务器

$ git config --local -l
$ git config --system -l
$ git config --global -l
$ git config -l

Git 工做流

你的本地仓库由 git 维护的三棵“树”组成。ssh

第一个是你的工做区, 它持有实际文件,就是你在文件资源管理器中看到的。

第二个是暂存区(stage),它像个缓存区域,临时保存你的改动。

最后是HEAD,它指向你最后一次提交的结果。

Git 相关操做

1. 建立新仓库 

在一个空的工做目录中开始,建立一个名为 learngit 的空目录, 而后建立一个名为 a.txt 的文件。

$ mkdir learngit
$ cd learngit
$ touch a.txt
$ ls
a.txt

你如今有一个包含单个文件的目录,执行 git init 命令,从该目录建立 git 仓库。git 会自动建立的一个分支叫 master。

$ git init
Initialized empty Git repository in G:/learngit/.git/

2. 检查仓库的状态

使用 git status 命令检查当前仓库的状态,这是个比较经常使用的命令。

$ git status
On branch master
Initial commit
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    a.txt
nothing added to commit but untracked files present (use "git add" to track)

3. 将更改提到暂存区

使用 git add <file> 命令将文件更改提交到暂存区,当有多个文件时使用 git add . 命令提交全部文件的修改包括新建的文件。

$ git add a.txt
$ git add .

使用 git status 查看仓库的状态

$ git status
On branch master
Initial commit
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   a.txt

4. 把暂存区的内容提交到当前分支(默认为 master)

这里会有一个 HEAD 指针指向 master 分支,而且 HEAD 指针老是指向你的当前分支。

$ git commit
[master (root-commit) 91c0959] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

执行 git commit 命令,会调出文本编辑器,提示你输入一段本次提交的描述。

你也可使用 git commit -m 'first commit' 来快速的进行描述提交,若是发现提交的描述有误或者不恰当可使用 git commit --amend 命令来对最近提交的描述信息进行替换

使用 git status 查看仓库的状态

$ git status
On branch master
nothing to commit, working tree clean

5. 查看历史提交记录

使用 git log 命令查看

$ git log
commit 91c0959fab89f5a612db84f047bf13afb1162a26
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:10:44 2017 +0800

    first commit

这时列出了咱们第一次提交的记录

下面咱们来更改 a.txt 的内容, 再次提交来查看记录

$ echo 'some modified' > a.txt //向a.txt写入'some modified'

$ git status //查看仓库的状态
On branch 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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git commit -a -m 'second commit' //这是暂存和提交一块儿操做的缩写,后面跟描述
[master e9df1e3] second commit
 1 file changed, 2 insertions(+)

$ git log //查看记录

commit e9df1e351fc321d4c63bfe9f76773759a03012e2
Author: alsy <2944927590@qq.com>
Date: Sat Mar 25 14:50:36 2017 +0800

second commit

commit 91c0959fab89f5a612db84f047bf13afb1162a26
Author: alsy <2944927590@qq.com>
Date: Sat Mar 25 14:10:44 2017 +0800

first commit
View Code

咱们能够看到咱们这两次提交的记录

6. 撤销修改

文件的撤销修改能够分为三个时间点的撤销

a. 修改了文件,没有 add 到暂存区

$ echo 'some modified ~' >> a.txt //往 a.txt 追加 'some modified ~'

$ cat a.txt //输出 a.txt 的内容
some modified
some modified ~

$ git status
On branch 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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
View Code

能够看到咱们修改了 a.txt 文件,此时查看状态,git 会提示你是要 git add <file> 去提交更改仍是 git checkout -- <file> 去丢弃更改,固然这里咱们是要丢弃更改

$ git checkout -- a.txt

$ git status
On branch master
nothing to commit, working tree clean

$ cat a.txt
some modified
View Code

此时 a.txt 有恢复到原来的状态

b. add 到暂存区,没有 commit 到分支

$ echo 'some modified ~' >> a.txt

$ git add a.txt

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        modified:   a.txt
View Code

能够看到咱们修改了 a.txt 文件而且把它提交到暂存区,此时查看状态,git 会提示你 git reset HEAD <file> 把暂存区的修改撤销掉(unstage),从新放回工做区

$ git reset HEAD a.txt
Unstaged changes after reset:
M       a.txt

$ git status
On branch 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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
View Code

执行 git reset HEAD a.txt 命令后,咱们查看状态,能够看到暂存区是干净的,工做区的 a.txt 有修改,和第 1 种状况同样

c. commit 到分支

$ echo 'some modified ~' >> a.txt

$ git commit -a -m 'add some modified'
[master 533f16a] add some modified
 1 file changed, 1 insertion(+)

$ git st
On branch master
nothing to commit, working tree clean

$ git log
commit 533f16a192ff11cbc5fa92643780155e50dcab60
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 15:48:52 2017 +0800

    add some modified

commit 71b53372b30d69648a4c2516d73fa3563197ec08
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:50:36 2017 +0800

    second commits

commit 91c0959fab89f5a612db84f047bf13afb1162a26
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:10:44 2017 +0800

    first commit
View Code

能够看到咱们已经把修改 commit 到分支了,这时咱们能够采用两种方式来撤销

(1) git revert [ HEAD | commit-ish ]

$ git revert HEAD
[master e1f8ce3] Revert "add some modified"
 1 file changed, 1 deletion(-)

$ git log
commit e1f8ce3351bd1bf0e2532c8f1e227c995bbc48e0
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 15:59:21 2017 +0800

    Revert "add some modified"

    This reverts commit 533f16a192ff11cbc5fa92643780155e50dcab60.

commit 533f16a192ff11cbc5fa92643780155e50dcab60
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 15:48:52 2017 +0800

    add some modified

commit 71b53372b30d69648a4c2516d73fa3563197ec08
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:50:36 2017 +0800

    second commits

commit 91c0959fab89f5a612db84f047bf13afb1162a26
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:10:44 2017 +0800

    first commit
View Code

能够看到 git revert 是生成一个新的提交来撤销某次提交,这次提交以前的 commit 都会被保留

(2) git reset [ --soft | --mixed | --hard ]  [ HEAD^ | commit-sh ]

  • --soft

保留源码,只回退到 commit 信息到某个版本,不回退暂存,若是还须要提交,直接commit便可。 

  • --mixed

保留源码,回退 commit 和暂存信息到某个版本。

git reset 默认是 --mixed 模式  

git reset --mixed  等价于  git reset

  • --hard

源码也会回退到某个版本,commit 和 index 都会回退到某个版本。

$ echo 'some modified ~' >> a.txt

$ git commit -a -m 'git reset test'

$ git log
commit 7c7c69f2b891b4368fd4d59c31827793e41ceac5
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 16:31:08 2017 +0800

    git reset test

commit e1f8ce3351bd1bf0e2532c8f1e227c995bbc48e0
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 15:59:21 2017 +0800

    Revert "add some modified"

    This reverts commit 533f16a192ff11cbc5fa92643780155e50dcab60.

commit 533f16a192ff11cbc5fa92643780155e50dcab60
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 15:48:52 2017 +0800

    add some modified

commit 71b53372b30d69648a4c2516d73fa3563197ec08
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:50:36 2017 +0800

    second commits

commit 91c0959fab89f5a612db84f047bf13afb1162a26
Author: alsy <2944927590@qq.com>
Date:   Sat Mar 25 14:10:44 2017 +0800

$ git reset --hard HEAD^
HEAD is now at e1f8ce3 Revert "add some modified"

$ cat a.txt
some modified
View Code

git revert 是用一次新的 commit 来回滚以前的 commit,git reset 是直接删除指定的 commit。

7. 分支的管理

a. 建立分支

使用 git branch <branch_name> 来建立一条新的分支 

$ git branch dev

$ git branch
  dev
* master

咱们在 master 基础上建立了一条 dev 分支,git branch 命令能够帮助咱们查看全部的本地分支,而且在当前分支前带有 * 号标记

b. 切换分支

使用 git checkout <branch_name> 来切换分支

$ git checkout dev
Switched to branch 'dev'

$ git branch
* dev
  master

看到咱们已经处于 dev 分支上了

c. 合并分支

如今咱们在 dev 分支上作一些修改

$ echo 'some modified on dev' >> a.txt

$ git commit -a -m 'some modified on dev'

咱们要将 dev 分支上的代码合并到master,使用 git merge <branch_name> 来合并分支

$ git checkout master
Switched to branch 'master'

$ git merge dev
Updating e1f8ce3..0209ed1
Fast-forward
 a.txt | 1 +
 1 file changed, 1 insertion(+)

$ cat a.txt
some modified

some modified on dev
View Code

能够看到此时 dev 上的修改已经到 master 上了

8. 远程仓库

远程仓库其实和本地仓库同样,只不过是做为一个代码提交、共享的平台,通常也不会有人直接在上面作提交修改。github 是免费 git 仓库的托管平台,咱们能够在 github 建立一个仓库做为咱们的远程仓库。固然咱们也能够本身搭建 git服务器,来建立咱们的远程仓库。以 github 为例:

a. 建立ssh key :

$ cd ~/.ssh/
$ ls
github_rsa  github_rsa.pub  id_rsa  id_rsa.pub

看看这个目录下有没有 id_rsa 和 id_rsa.pub 这两个文件,若是没有则执行如下命令来建立:

$ ssh-keygen -t rsa -C "youremail"

id_rsa 是私钥, id_rsa.pub 是公钥,咱们须要把公钥添加到 github , 把 id_rsa.pub 里面的内容添加到 github 上:

这样咱们才有权限去推送代码

b. 建立远程仓库

c. 本地关联远程仓库

咱们能够根据 github 上给出的提示来与远程仓库进行关联,即:

$ git remote add origin https://github.com/2944927590/node-practice.git

$ git push origin master
Counting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (13/13), 1.03 KiB | 0 bytes/s, done.
Total 13 (delta 0), reused 0 (delta 0)
To https://github.com/2944927590/node-practice.git
 * [new branch]      master -> master
View Code

咱们可使用 git push --set-upstream origin master 让本地的 master 分支与远程仓库的 master 分支创建链接,这样咱们能够直接使用 git push 来将本地 master 分支代码推送到远程 master 分支

$ git push --set-upstream origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date

$ git push
Everything up-to-date
View Code

d. 拉取最新代码

git pull 命令来拉取远程仓库的最新代码到本地,这里须要指出 git pull 至关于 git fetch(取下更改) 和 git merge (合并到本地分支)两条命令

$ git pull
Already up-to-date.
View Code

9. Git 设置别名

$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch
$ git config --global alias.st status
$ git config --global alias.lg log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

这样 git checkout 等命令能够用 git co 等代替了,固然你能够对其余命令设置别名。

10. 实际开发中的分支

通常咱们在项目开发中,会有一个 master 分支做为整个项目的主分支,也就是实际上线的代码分支;同时可能还会有一个 dev 分支,做为开发环境的代码分支;有的还会有一个 qa 分支,做为测试环境的代码分支;咱们都是在为这三条分支服务。好比咱们接到新的需求,开始建立分支,首先切到 master 分支,git pull 拉取最新的代码,建立一个本身的开发分支 task , 咱们在 task 分支上开始写咱们的代码,merge 到 dev 分支进行开发调试,merge 到 qa 分支进行测试,最后 merge 到 master 分支上线。

拓展阅读

相关文章
相关标签/搜索