Git入门

来源:互联网 发布:极限挑战第三季 知乎 编辑:程序博客网 时间:2024/06/08 07:07

Git入门

简介

Git是一个免费开源的分布式版本控制系统,用于管理项目的方方面面,能够高速、高效地用于从小项目到非常大的项目。

工作流程及术语

  • 工作空间(workspace):当前项目文件夹
  • 索引(index):工作空间索引
  • 本地仓库(localrepo):本地保存历史版本内容的数据库
  • 远程仓库(remoterepo):远程保存历史版本内容的数据库
Created with Raphaël 2.1.0workspaceworkspaceindexindexlocalrepolocalreporemotereporemoterepoaddcommit -mcommit -ampushpullfetchcheckout HEADcheckoutdiff HEADdiff

本地工作流演示

  • 设置个人身份
git config --helpgit config --global user.name "<user name>"git config --global user.email <email>
  • 初始化本地仓库
[titi@mine test]$ git initInitialized empty Git repository in /home/titi/test/.git/
  • 新建文件
[titi@mine test]$ echo 1 > text[titi@mine test]$ git status -s?? text
  • 添加至索引
[titi@mine test]$ git add text[titi@mine test]$ git status -sA  text
  • 添加至本地仓库
[titi@mine test]$ git commit -m "The first commit"[master (root-commit) 9513892] The first commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 text[titi@mine test]$ git logcommit 9513892bd948ebaa7c12cb0c6c5d4a7814a77433Author: Dazhi Jiang <ejdazhi@163.com>Date:   Sun Oct 30 15:23:32 2016 +0800    The first commit
  • 直接添加至本地仓库
[titi@mine test]$ echo 111 > text[titi@mine test]$ git status -s M text[titi@mine test]$ git commit -am 'the second commit'
  • 更改文件并添加至索引
[titi@mine test]$ echo 222 >> text[titi@mine test]$ cat text111222[titi@mine test]$ git add text
  • 再次更改文件
[titi@mine test]$ echo 333 >> text[titi@mine test]$ cat text111222333
  • 本地仓库与工作空间差异
[titi@mine test]$ git diff HEADdiff --git a/text b/textindex 58c9bdf..641d574 100644--- a/text+++ b/text@@ -1 +1,3 @@ 111+222+333
  • 索引与工作空间差异
[titi@mine test]$ git diffdiff --git a/text b/textindex a30a52a..641d574 100644--- a/text+++ b/text@@ -1,2 +1,3 @@ 111 222+333
  • 重置工作空间至索引
[titi@mine test]$ git checkout text[titi@mine test]$ cat text111222
  • 重置工作空间至本地仓库最后版本
[titi@mine test]$ git checkout HEAD text[titi@mine test]$ cat text111
0 0