git工作流程一览

Git是分布式版本控制系统,没有中央服务器,每个人的电脑就是一个完整的版本库,工作的时候不需要联网了,因为版本都在自己电脑上。协同的方法是这样的:比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

一、git常用术语:

1)、仓库(Repository)
受版本控制的所有文件修订历史的共享数据库

2)、工作空间(Workspace) 
本地硬盘或Unix 用户帐户上编辑的文件副本

3)、工作树/区(Working tree)
工作区中包含了仓库的工作文件。您可以修改的内容和提交更改作为新的提交到仓库。

4)、暂存区(Staging area)
暂存区是工作区用来提交更改(commit)前可以暂存工作区的变化。

5)、索引(Index)
索引是暂存区的另一种术语。

6)、签入(Checkin)
将新版本复制回仓库

7)、签出(Checkout)
从仓库中将文件的最新修订版本复制到工作空间

8)、提交(Commit)
对各自文件的工作副本做了更改,并将这些更改提交到仓库

9)、冲突(Conflict)
多人对同一文件的工作副本进行更改,并将这些更改提交到仓库

10)、合并(Merge)
将某分支上的更改联接到此主干或同为主干的另一个分支

11)、分支(Branch)
从主线上分离开的副本,默认分支叫master

12)、锁(Lock)
获得修改文件的专有权限。

13)、头(HEAD)
头是一个象征性的参考,最常用以指向当前选择的分支。

14)、修订(Revision)
表示代码的一个版本状态。Git通过用SHA1 hash算法表示的ID来标识不同的版本。

15)、标记(Tags)
标记指的是某个分支某个特定时间点的状态。通过标记,可以很方便的切换到标记时的状态。

二、git的工作流程一般:

1、在工作目录中添加、修改文件;

2、将需要进行版本管理的文件放入暂存区域;

3、将暂存区域的文件提交到git仓库。

三、工作区

Git本地有三个工作区域:

1、工作目录(Working Directory)

2、暂存区(Stage/Index)

3、资源库(Repository或Git Directory)

远程有一个工作区域:

1、git仓库(Remote Directory)

在push到远程git仓库之前,所有的操作都是在本地进行的。

四、git文件状态及互相转化:

1、Untracked:未跟踪

2、Unmodify:未修改

3、Modified:已修改

4、Staged:已暂存

Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.

Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件

Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改

Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified

五、git --help:

  These are common Git commands used in various situations:      这些都是各种情况下常见的Git命令:

  start a working area (see also: git help tutorial)              创建一个工作区(参见:Git帮助教程)  
    clone Clone a repository into a new directory                clone 克隆一个仓库到一个新目录
    init Create an empty Git repository or reinitialize an existing one    init 创建一个空的Git仓库或重新初始化现有仓库

  work on the current change (see also: git help everyday)          操作当前工作空间(参见:Git日常帮助)
    add Add file contents to the index                       add 向索引(暂存区)中添加文件内容
    mv Move or rename a file, a directory, or a symlink                      mv 移动或重命名文件、目录,或者符号关联
    reset Reset current HEAD to the specified state                          reset 复位HEAD(常用以指向当前选择的分支)到指定状态
    rm Remove files from the working tree and from the index                 rm 从工作树(包含了仓库的工作文件)和索引(暂存区)删除文件

  examine the history and state (see also: git help revisions)             查看历史和状态(参见:Git修订帮助)
    bisect Use binary search to find the commit that introduced a bug        bisect 使用二进制搜索找到出错的提交
    grep Print lines matching a pattern                                      grep 打印出匹配的行
    log Show commit logs                                                     log 显示提交日志
    show Show various types of objects                       show 显示各种类型的对象
    status Show the working tree status                                      status 显示工作树的状态

  grow, mark and tweak your common history                                 生成、标记和调整历史
    branch List, create, or delete branches                                  branch 查看、创建或删除分支
    checkout Switch branches or restore working tree files                   checkout 切换分支或恢复工作树文件
    commit Record changes to the repository                                  commit 记录更改到数据库
    diff Show changes between commits, commit and working tree, etc          diff 显示两次提交之间的变化,提交和工作树之间的变化,等等
    merge Join two or more development histories together                    merge 合并两个或更多个开发历史
    rebase Reapply commits on top of another base tip                        rebase 申请覆盖上一次提交
    tag Create, list, delete or verify a tag object signed with GPG          tag 用GPG创建,查看,删除或验证一个有符号的对象

  collaborate (see also: git help workflows)                               协作(参见:Git帮助工作流)
    fetch Download objects and refs from another repository                  fetch 从另一个库下载项目或文档
    pull Fetch from and integrate with another repository or a local branch  pull 将其他的库或本地分支整合
    push Update remote refs along with associated objects                    push 更新远程项目和文档

六、命令

1、配置

#添加配置项 
git config [--local|--global|--system]  section.key value

git config --system user.name wang  #系统级,配置信息适用当前系统的所有用户和项目, 配置文件位置Win:C:Program FilesGitmingw64etcgitconfig
git config --global user.name wang  #当前用户级,配置信息适用当前用户的所有项目, 配置文件位置Win:C:UsersAdministrator.gitconfig
git config
--local user.name wang #项目级,配置信息仅适用于当前项目, 配置文件位置Win:C:gitProject
注意:对于同一配置项,三个配置文件的优先级:system<global<local
#删除配置项 
git config [--local|--global|--system] --unset section.key

git config [--local|--global|--system] --unset user.name
#更多配置项
git config --global color.ui true   #打开所有的默认终端着色
git config --global alias.ci commit   #别名 ci 是commit的别名
[alias]  
co = checkout  
ci = commit  
st = status  
pl = pull  
ps = push  
dt = difftool  
l = log --stat  
cp = cherry-pick  
ca = commit -a  
b = branch 

user.name  #用户名
user.email  #邮箱
core.editor  #文本编辑器  
merge.tool  #差异分析工具  
core.paper "less -N"  #配置显示方式  
color.diff true  #diff颜色配置  
alias.co checkout  #设置别名
git config user.name  #获得用户名
git config core.filemode false  #忽略修改权限的文件  
#所有config命令参数
语法: git config [<options>]        
        
文件位置        
    --global                  #use global config file 使用全局配置文件
    --system                  #use system config file 使用系统配置文件
    --local                   #use repository config file    使用存储库配置文件
    -f, --file <file>         #use given config file    使用给定的配置文件
    --blob <blob-id>          #read config from given blob object    从给定的对象中读取配置
        
动作        
    --get                     #get value: name [value-regex]    获得值:[值]名[正则表达式]
    --get-all                 #get all values: key [value-regex]    获得所有值:[值]名[正则表达式]
    --get-regexp          #get values for regexp: name-regex [value-regex]    得到的值根据正则
    --get-urlmatch            #get value specific for the URL: section[.var] URL    为URL获取特定的值
    --replace-all             #replace all matching variables: name value [value_regex]    替换所有匹配的变量:名称值[ value_regex ]
    --add                     #add a new variable: name value    添加一个新变量:name值
    --unset                   #remove a variable: name [value-regex]    删除一个变量名[值]:正则表达式
    --unset-all               #remove all matches: name [value-regex]    删除所有匹配的正则表达式:名称[值]
    --rename-section          #rename section: old-name new-name    重命名部分:旧名称 新名称
    --remove-section          #remove a section: name    删除部分:名称
    -l, --list                #list all    列出所有
    -e, --edit            #open an editor    打开一个编辑器
    --get-color               #find the color configured: slot [default]    找到配置的颜色:插槽[默认]
    --get-colorbool           #find the color setting: slot [stdout-is-tty]    发现颜色设置:槽[ stdout是TTY ]
        
类型        
    --bool                    #value is "true" or "false"    值是“真”或“假”。
    --int                     #value is decimal number    值是十进制数。
    --bool-or-int             #value is --bool or --int    值--布尔或int
    --path                    #value is a path (file or directory name)    值是路径(文件或目录名)
        
其它        
    -z, --null                #terminate values with NUL byte    终止值与null字节
    --name-only               #show variable names only    只显示变量名
    --includes                #respect include directives on lookup    尊重包括查找指令
    --show-origin             #show origin of config (file, standard input, blob, command line)    显示配置(文件、标准输入、数据块、命令行)的来源

2、常用指令

#创建工作目录

git init 在当前目录新建一个Git代码库
git init [project-name] 新建一个目录,将其初始化为Git代码库
git clone [url] 克隆一个项目和它的整个代码历史(版本信息)
#操作文件
git status [filename] 查看指定文件的状态
git status 查看所有文件的状态 git add [file1] [file2] ... 添加指定文件到暂存区 git add [dir] 添加指定目录到暂存区,包括子目录 git add . 添加当前目录的所有文件到暂存区 git commit 提交,暂存区的目录树写到版本库(对象库)中,master 分支会做相应的更新。即 master 指向的目录树就是提交时暂存区的目录树。 git checkout .签出,用暂存区全部或指定的文件替换工作区的文件,汇总显示工作区、暂存区与HEAD的差异。这个操作很危险,会清除工作区中未添加到暂存区的改动。 git checkout HEAD .会用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改 动。 git checkout
-- filename 用暂存区中filename文件来覆盖工作区中的filename文件。相当于取消自上次执行git add filename以来(如果执行过)的本地修改。 git rm --cached <file> 直接从暂存区删除文件,工作区则不做出改变 git reset HEAD <file>... 通过重写目录树移除add文件,如果已经用add 命令把文件加入stage了,就先需要从stage中撤销 git clean [options] 移除所有未跟踪文件,一般会加上参数-df,-d表示包含目录,-f表示强制清除。 git rm --cached readme.txt 只从stage中删除,保留物理文件 git rm readme.txt 不但从stage中删除,同时删除物理文件 git mv a.txt b.txt 把a.txt改名为b.txt git diff [files] 查看文件修改后的差异 git diff --cached 比较暂存区的文件与之前已经提交过的文件 git diff HEAD~n 比较repo与工作空间中的文件差异

git log 显示当前分支的版本历史
git log --stat 显示commit历史,以及每次commit发生变更的文件
git commit -m "first commit" 提交到本地仓库并写日志

git remote add origin https://github.com/wangnl/test.git 添加远程主机,主机名为origin 地址为https://github.com/wangnl/test.git
git push -u origin master 本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push了,-u 参数指定一个默认主机

归纳如图:

七、分支(待)

原文地址:https://www.cnblogs.com/yinn/p/7641507.html