初识Git

本地创建一个Git版本库简单了解Git的一些必要的配置,配置昵称用户名和邮箱。

简单复习 - 专栏 Git原理详解与实操指南

1、创建Git版本库

首先创建个文件夹git,用于版本管理。

Linux或Mac系统

mkdir git
cd git

进入文件夹,可以使用git init命令初始化一个仓库

➜  git git init
Initialized empty Git repository in /home/liuawen/git/.git/
➜ git git:(master)

初始化版本库会在当前目录中创建一个.git的文件夹,我们可以进入版本库根目录,然后通过命令ls -al进行查看,如下命令所示:

➜  git git:(master) ls
➜ git git:(master) pwd
/home/liuawen/git
➜ git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 20 15:23 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 20 15:23 ..
drwxr-xr-x 1 root root 4096 Mar 20 15:24 .git
➜ git git:(master)

执行过的命令:

➜  liuawen pwd
/home/liuawen
➜ liuawen mkdir git
mkdir: cannot create directory ‘git’: File exists
➜ liuawen rm -rf git
➜ liuawen ls
sources.list
➜ liuawen mkdir git
➜ liuawen ls
git sources.list
➜ liuawen cd git
➜ git git init
Initialized empty Git repository in /home/liuawen/git/.git/
➜ git git:(master) ls
➜ git git:(master) pwd
/home/liuawen/git
➜ git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 20 15:23 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 20 15:23 ..
drwxr-xr-x 1 root root 4096 Mar 20 15:24 .git
➜ git git:(master)

认识.git

git init初始化生成的.git目录

.git目录包含了整个版本库的信息,那怎么查看这个目录有哪些文件呢?

可以使用如下命令

cd .git
ls -al

cd .git是进入.git目录,ls -al是显示当前目录下的所有文件及文件夹包括隐藏文件详细信息。

windows:

Linux:

➜  git git:(master) cd .git
➜ .git git:(master) pwd
/home/liuawen/git/.git
➜ .git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 20 15:38 .
drwxr-xr-x 1 root root 4096 Mar 20 15:23 ..
-rw-r--r-- 1 root root 23 Mar 20 15:23 HEAD
drwxr-xr-x 1 root root 4096 Mar 20 15:23 branches
-rw-r--r-- 1 root root 92 Mar 20 15:23 config
-rw-r--r-- 1 root root 73 Mar 20 15:23 description
drwxr-xr-x 1 root root 4096 Mar 20 15:23 hooks
drwxr-xr-x 1 root root 4096 Mar 20 15:23 info
drwxr-xr-x 1 root root 4096 Mar 20 15:23 objects
drwxr-xr-x 1 root root 4096 Mar 20 15:23 refs
➜ .git git:(master)

简单介绍下这些文件夹的作用:

  • HEAD 文件指示目前被检出的分支

  • branches 新版本已经废弃,我们不必关注

  • description用来显示对仓库的描述信息

  • config 文件包含项目特有的配置选项

  • info 目录包含一个全局性排除文件

  • hooks 目录包含客户端或服务端的钩子脚本

  • index 文件保存暂存区信息

  • objects 目录存储所有数据内容

  • refs 目录存储分支的提交对象的指针

Q:ls ls -a ls -l ls -al 有什么区别呢?

A:ls 是显示不隐藏的文件和文件夹 ls -a 是显示所有文件和文件夹(包括隐藏的文件)

ls -l是显示不隐藏的文件和文件夹的详细信息,ls -al是显示目录下的所有文件及文件夹包括隐藏文件详细信息。

➜  git git:(master) ls
➜ git git:(master) pwd
/home/liuawen/git
➜ git git:(master) ls
➜ git git:(master) ls -a
. .. .git
➜ git git:(master) ls -l
total 0
➜ git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 20 15:23 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 20 15:23 ..
drwxr-xr-x 1 root root 4096 Mar 20 15:37 .git
➜ git git:(master) cd .git
➜ .git git:(master) pwd
/home/liuawen/git/.git

2、基础配置

在建立完项目的版本库之后,

mkdir git
cd git
git init

我们之后对代码的管理操作会要求要有一个身份,所以我们现在来配置一个昵称和邮箱。可以在查看改动记录时候用到的。

2.1、查看配置信息

我们在配置一个昵称和邮箱之前,先来检查下之前没有用已经配置过了什么昵称邮箱。

➜  git git:(master) pwd
/home/liuawen/git
➜ git git:(master) git config user.name
➜ git git:(master) git config user.email
➜ git git:(master)

啥也没有,那就是没有配置过咯,我们来配置下吧。

2.2、配置昵称邮箱信息

配置昵称&邮箱的命令如下:

Setting your Git username for every repository on your computer.

配置全局的

git config --global user.name "昵称"
git config --global user.email "邮箱"

执行命令如下:

➜  git git:(master) git config --global user.name "liuawen"
➜ git git:(master) git config --global user.name
liuawen
➜ git git:(master) git config --global user.email "willowawen@foxmail.com"
➜ git git:(master) git config --global user.email
willowawen@foxmail.com
➜ git git:(master)

Setting your Git username for a single repository

局部的,单个仓库

git config  user.name "昵称"
git config user.email "邮箱"

执行命令如下:

➜  git git:(master) pwd
/home/liuawen/git
➜ git git:(master) git config user.name
➜ git git:(master) git config user.email
➜ git git:(master) git config user.name "liuawen"
➜ git git:(master) git config user.name
liuawen
➜ git git:(master) git config user.email "willowawen@foxmail"
➜ git git:(master) git config user.email
willowawen@foxmail
➜ git git:(master)

2.3、修改配置信息

我们在配置过程中如果不小心配置错了或者后面想修改配置,那怎么办呢?

试一试可不不可以通过重复执行上面的设置昵称邮箱命令,来修改昵称邮箱的呢?

我之前的邮箱输错了… 少了.com 来修改一下吧

➜  git git:(master) git config user.email "willowawen@foxmail"
➜ git git:(master) git config user.email
willowawen@foxmail
➜ git git:(master) git config user.email "willowawen@foxmail.com"
➜ git git:(master) git config user.email
willowawen@foxmail.com
➜ git git:(master) git config user.name "liuawen2"
➜ git git:(master) git config user.name
liuawen2
➜ git git:(master)
➜ git git:(master) git config user.name "liuawen3"
➜ git git:(master) git config user.name
liuawen3

对局部的可以,那我们试一试全局的global。

➜  git git:(master) git config  --global user.name
liuawen
➜ git git:(master) git config --global user.email
willowawen@foxmail.com
➜ git git:(master) git config --global user.name "git"
➜ git git:(master) git config --global user.name
git
➜ git git:(master) git config --global user.name "willow"
➜ git git:(master) git config --global user.name
willow
➜ git git:(master) git config --global user.name "liuawen"
➜ git git:(master) git config --global user.name
liuawen
➜ git git:(master) git config --global user.email "willowawen@gmail.com"
➜ git git:(master) git config --global user.email
willowawen@gmail.com
➜ git git:(master) git config --global user.email "willowawen@foxmail.com"
➜ git git:(master) git config --global user.email
willowawen@foxmail.com
➜ git git:(master)

也可以的,是可以通过重复执行上面的设置昵称命令,来修改昵称和邮箱的。

如果多次设置昵称,它会在命令执行后提示无法重复配置或者可能不给提示,但是这种情况会导致一个 key 配置了多个 value 的问题。(我的这里可以多次设置昵称和邮箱的,应该是可能是版本不同略有差异吧),建议使用特定的方法去修改配置信息。

1、通过命令行

通过命令行修改的方式比较简单,直接执行以下的命令即可

修改昵称邮箱命令如下:

git config --global --replace-all user.name "your user name"
git config --global --replace-all user.email"your user email"

执行操作:

➜  git git:(master) git config --global user.name
liuawen
➜ git git:(master) git config --global user.email
willowawen@foxmail.com
➜ git git:(master) git config --global --replace-all user.name "haha"
➜ git git:(master) git config --global user.name
haha
➜ git git:(master) git config --global --replace-all user.email "hehe@qq.com"
➜ git git:(master) git config --global user.email
hehe@qq.com
➜ git git:(master) git config --global --replace-all user.user "liuawen"
➜ git git:(master) git config --global user.name
haha
➜ git git:(master) git config --global --replace-all user.name "liuawen"
➜ git git:(master) git config --global user.name
liuawen
➜ git git:(master) git config --global --replace-all user.email "willowawen@foxmail.com"
➜ git git:(master) git config --global user.email
willowawen@foxmail.com
➜ git git:(master)

2、通过修改配置文件。

修改文件的方式,主要是修改位于主目录下.gitconfig 文件。在 Linux 和 Mac 中,可以通过 vim 命令进行直接编辑,比如vim ~/.gitconfig

修改全局的

操作如下

➜  git git:(master) pwd
/home/liuawen/git
➜ git git:(master) cd git
cd: no such file or directory: git
➜ git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 20 15:23 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 20 15:23 ..
drwxr-xr-x 1 root root 4096 Mar 20 16:22 .git
➜ git git:(master) cd .git
➜ .git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 20 16:22 .
drwxr-xr-x 1 root root 4096 Mar 20 15:23 ..
-rw-r--r-- 1 root root 23 Mar 20 15:23 HEAD
drwxr-xr-x 1 root root 4096 Mar 20 15:23 branches
-rw-r--r-- 1 root root 148 Mar 20 16:07 config
-rw-r--r-- 1 root root 73 Mar 20 15:23 description
drwxr-xr-x 1 root root 4096 Mar 20 15:23 hooks
drwxr-xr-x 1 root root 4096 Mar 20 15:23 info
drwxr-xr-x 1 root root 4096 Mar 20 15:23 objects
drwxr-xr-x 1 root root 4096 Mar 20 15:23 refs
➜ .git git:(master) vim ~/.gitconfig
➜ .git git:(master) ➜ .git git:(master) vim ~/.gitconfig
➜ .git git:(master) cd ..
➜ git git:(master) vim ~/.gitconfig
➜ git git:(master) cd ..
➜ liuawen vim ~/.gitconfig

vim ~/.gitconfig 都可以去.gitconfig里面修改

Windows 系统同样位于用户主目录下,假设你当前的用户是administrator,那么对应的配置文件的路径应该是 C:\Users\administrator\.gitconfig,可以直接使用记事本修改里边的 name 或者 email。

我这里没找到

找到了 ,我以为是文件夹。。。 配置信息

如果之前已经配置过昵称和邮箱的情况下,当使用 vim 或者记事本打开配置文件之后,可以看到如下配置:

[user]
name = liuawen
email = willowawen@foxmail.com

修改完,通过 git bash 输入 git config –list可以查看是否修改成功了。

Ctrl + Z 结束查看

➜  git git:(master) git config -list
error: did you mean `--list` (with two dashes ?)
➜ git git:(master) vim ~/.gitconfig
➜ git git:(master) git config -list
error: did you mean `--list` (with two dashes ?)
➜ git git:(master) git config --list

修改局部的

直接当当前文件仓库进行修改的

window

Linux也一样

3、小结

使用过的命令

mkdir git
cd git
git init
pwd
ls -al
cd .git
ls -al
ls -a
ls -l
ls
cd ..
git config --global user.name "昵称"
git config --global user.email "邮箱"
git config user.name "昵称"
git config user.email "邮箱"
git config --global --replace-all user.name "your user name"
git config --global --replace-all user.email"your user email"
vim ~/.gitconfig
git config –-list

学习了如何创建一个本地版本库,以及了解了版本库的基本结构,会一些简单基础的配置。

在空文件夹中,可以通过命令git init创建一个本地版本库;

每个版本库的根目录下,都存放着一个.git的隐藏文件夹,里面包含版本库的全部信息;

管理版本库必须有一个身份,需要设置昵称和邮箱。

【Git】3、创建Git版本库、配置Git仓库用户邮箱信息的更多相关文章

  1. 安装git,创建本地版本库

    安装 由于我使用的是Ubuntu,因此安装很简单,输入:sudo apt-get install git 如果是其他Linux版本,可以直接通过源码安装.先从Git官网下载源码,然后解压,依次输入:. ...

  2. GIT使用—创建一个版本库

    一.GIT命令行 [root@localhost ~]# git usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] ...

  3. git 创建远程版本库(亲测有效)

    一.github远程版本库 1.创建SSH Key(windows)   ssh-keygen -t rsa -C "youremail@example.com"   2.连接版本 ...

  4. 『现学现忘』Git基础 — 8、Git创建本地版本库

    目录 1.Git版本库介绍 2.创建本地版本库 场景一:创建一个空的本地版本库. 场景二:项目中已存在文件时,创建该项目的本地版本库. 场景三:在GitHub网站上创建仓库,克隆到本地. 1.Git版 ...

  5. Git的使用(1) —— 版本库

    1. 简介 Git作为一个分布式版本控制系统,其优点是不需要一直连接远端版本库就可以使用. 故其为实现分布版本控制专门设计了一整套的存储区间和语句,用来实现. (1) 本地版本库:建立在本机磁盘上的文 ...

  6. git学习2:版本库

    创建版本库 版本库,又称仓库,英文名为repository,版本库内的所有文件都可以被Git管理起来,即每个文件的修改.删除,Git都能跟踪. 1,在目录中创建版本库 在目录中有两种创建版本库的方法, ...

  7. git怎么创建本地版本仓库

    git怎么创建本地版本仓库 安装git我就不用说了吧!下载地址:https://github.com/msysgit/msysgit/releases/download/Git-1.9.4-previ ...

  8. 【Git】(1)---工作区、暂存区、版本库、远程仓库

    工作区.暂存区.版本库.远程仓库 一.概念 1.四个工作区域 Git本地有四个工作区域:工作目录(Working Directory).暂存区(Stage/Index).资源库(Repository或 ...

  9. linux 创建svn版本库,并在svn上配置checkstyle做代码风格检查

    一.创建SVN版本库 1.安装svn服务器 yum install subversion 2.查看版本 svnserve --version 3.建立SVN版本库目录(即你的SVN服务器里面的文件存放 ...

随机推荐

  1. Vue项目上线环境部署,项目优化策略,生成打包报告,及上线相关配置

    Node.js简介 Node.js是一个基于Chrome V8引擎的JavaScript运行环境,用来方便快速地搭建易于扩展的网络应用.Node.js使用了一个事件驱动.非阻塞式I/O的模型,使其轻量 ...

  2. Struts2 S2-061漏洞复现(CVE-2020-17530)

    0x01 漏洞描述 Struts2 会对某些标签属性(比如 `id`,其他属性有待寻找) 的属性值进行二次表达式解析,因此当这些标签属性中使用了 `%{x}` 且 `x` 的值用户可控时,用户再传入一 ...

  3. swiper4使用教程-填坑

    本篇博客用于记录使用swiper插件中的一些关键点: 首先附上官网地址:https://www.swiper.com.cn/ ios中使用swiper的坑: /*解决swiper中苹果点击变暗,在cs ...

  4. oracle 11g打补丁错误(Missing command :fuser)

    在给oracle 11g数据库打补丁的时候出现以下错误: [oracle@node01 31537677]$ $ORACLE_HOME/OPatch/opatch apply Oracle Inter ...

  5. Shiro实现Basic认证

    前言 今天跟小伙伴们分享一个实战内容,使用Spring Boot+Shiro实现一个简单的Http认证. 场景是这样的,我们平时的工作中可能会对外提供一些接口,如果这些接口不做一些安全认证,什么人都可 ...

  6. Missing Private key解决方案——IOS证书 .cer 以p12文件以及配置方案

    一个苹果证书怎么多次使用--导出p12文件 为什么要导出.p12文件 因为苹果规定 .cer证书只能存在于一台机器上,因此 如果另一台电脑想要用的话,需要导出为.p12 file ,安装到另一台没有安 ...

  7. Flutter AS设备连接显示loading解决方案

    看了网上很多解决方案,基本都是要杀dart进程后,删除lockfile 文件,然后运行检查命令flutter doctor. 这个方式有一定的意义,但是确实不一定解决这个问题. 今天就遇到了这样的问题 ...

  8. Android 7.0应用之间共享文件

    原文首发于微信公众号:躬行之,欢迎关注交流! 开发中经常需要将某个文件向另一个应用程序传递,如图片上传到另一个应用程序.文件在不同存储路径之间的复制粘贴等都需要共享文件,可以这样理解接收文件的应用是在 ...

  9. CSS鼠标指针cursor样式

    参考来源:W3SCHOOL 有时我们需要在CSS布局时设定特定的鼠标指针样式,这时可以通过设定cursor来实现: url: 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一种普通的光 ...

  10. ID3很不错的讲解(matlab程序实现)

    1)决策树之ID3 决策树算法是分类算法的一种,基础是ID3算法,C4.5.C5.0都是对ID3的改进.ID3算法的基本思想是,选择信息增益最大的属性作为当前的分类属性. 看Tom M. Mitche ...