• Git 学习之 Git Basics


    最近在用git,但git学习曲线实在是有点高。
    好在找到一个文档 https://www.atlassian.com/git/tutorial/,以下就是学习笔记吧!
    git init
    git init  
    在当前目录初始化一个Git仓库,包含一个 .git 目录       .git 目录就是Git版本库 当前目录为工作区
    git init <directory> 
    在指定的目录初始化一个Git仓库, <directory> 下包含一个.git 目录
    git init --bare <directory> 
    在指定目录初始化一个版本库,仅包含".git"目录(记录版本历史),不含项目源文件拷贝。

    在使用Git初始化版本库的时候,使用"git init"命令和使用"git init --bare"命令有什么区别呢?

           用"git init"初始化的版本库(暂且称之为working repository)将会生成2类文件:“.git“版本库目录(记录版本历史)和实际项目文件的拷贝。你可以把这类版本库叫做“工作目录”。工作目录是一个包含有版本历史目录“.git"和源文件的目录。你可以在工作目录修改你的源文件并使用"git add"和"git commit"命令进行版本管理。

           用“git init --bare"初始化的版本库(暂且称之为bare repository)仅包含".git"目录(记录版本历史),不含项目源文件拷贝。如果你进入版本目录,你会发现仅有".git"目录,没有其他文件。版本库仅包含记录着版本历史的文件。

    什么情况下使用“git init"和"git init --bare"呢?

           working repository适合于实际编辑生产过程中,在工作目录下,你将会进行实际的编码、文件管理操作和保存项目在本地工作。如果你开始创建一个项目将包含有源代码和和版本跟踪记录的时候你可以使用"git init".或者,如果你克隆"git clone"一个已经存在的版本库的时候,你也可以得到一个working repository,它也将包含".git"目录和源文件的拷贝。

           bare repository主要是用作分享版本库。开发者使用bare repository可以向其他人分享存储在本地的版本库,以便于实时分享代码更新和团队协作 。通过使用"git push"命令,你可以将你的本地更新提交至“中心版本库”(其他开发者可访问的中心库)。其他开发者可以使用“git pull"命令者接受你提交的版本更新。如果你正在一个多人协作的项目团队或者同一个项目需要在不同电脑上面完成的时候,bare repository可以满足你的分布式开发需求。

           总结:“工作目录”是通过使用“git init“或“git clone”创建的本地项目拷贝。我们可以在工作目录下面修改和测试代码。通过测试后我们可以使用“git add“和”git commit“命令本地提交修改,然后使用“git push”命令向远程 bare repository库提交更新,通常bare repository指定其他服务器,其他开发者将可以及时看到你的更新。当我们想去更新本地工作目录的时候,我们可以使用“git pull”命令去接受其他开发者提交的更新。

    git clone 

    git clone <repo>

    克隆一个版本库到本地

    git clone <repo> <directory>

    克隆一个版本库到 <directory> 目录

    git config

    git config user.name <name>
    配置Git当前用户的姓名
    加 --global 参数表示这是全局变量,配置将会写入 ~/.gitconfig
     git config --global user.name <name>
    设置全局用户名 
    git config --global user.email <email>
    设置全局邮箱 
    git config --global alias.<alias-name> <git-command>
    给命令设置别名 比如
    git config --global alias.st status
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.up rebase
    git config --global alias.ci commit
    现在 git st == git status
            git co == git checkout
     
    git config --global color.ui true
    GIt 命令中开启颜色显示 
    git config --global core.editor <editor>
    设置git 使用的编辑器 比如 git commit 时会调用相应的编辑器 
    git config --global --edit
    打开全局配置文件
     
    <repo>/.git/config 版本库配置文件
    ~/.gitconfig   用户全局配置文件
     
    git add
    git add <file>

    将修改的这个文件添加到暂存区(包括修改删除 和 新添加的文件)

    git add <directory>

    将修改的这个目录添加到暂存区

    git add -p

    以交互的方式让你选择将要进行的操作 y 是提交修改到暂存区 n 是忽略 s splits it into smaller shunks e 编辑chunk q 退出

    git add -u 

    将修改添加到暂存区(包括修改删除 但不包括新添加的文件)

    git add -A

    把所有的修改添加到暂存区 

    git add -i

    我们可以通过git add -i [<path>]命令查看<path>中被所有修改过或已删除文件但没有提交的文件,

    并通过其revert子命令可以查看<path>中所有untracted的文件,同时进入一个子命令系统。
    比如:
     git add -i
               staged     unstaged path
      1:        +0/-0      nothing branch/t.txt
      2:        +0/-0      nothing branch/t2.txt
      3:    unchanged        +1/-0 readme.txt

    *** Commands ***
      1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
      5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp

    What now>
    这里的t.txtt2.txt表示已经被执行了git add,待提交。即已经添加到索引库中。
    readme.txt表示已经处于tracked下,它被修改了,但是还没有被执行了git add。即还没添加到索引库中。
     
    revert子命令
    可以通过git add -irevert子命令(3: [r]evert)把已经添加到索引库中的文件从索引库中剔除。
    3: [r]evert)表示通过3rrevert加回车执行该命令。执行该命令后,git会例出索引库中的文件列表.
    然后通过数字来选择。输入"1"表示git会例出索引库中的文件列表中的第1个文件。
    "1-15"表示git会例出索引库中的文件列表中的第1个文件到第15个文件.回车将执行。
    如果我们不输入任何东西,直接回车,将结束revert子命令,返回git add -i的主命令行。
    update子命令
    可以通过update子命令2: [u]pdate)把已经tracked的文件添加到索引库中。其操作和revert子命令类似。
    add untracked子命令
    通过add untracked子命令(
    4: [a]dd untracked)可以把还没被git管理的文件添加到索引库中。其操作和revert子命令类似。
    diff子命令
    可以通过diff子命令(6: [d]iff)可以比较索引库中文件和原版本的差异。其操作和revert子命令类似。
    status子命令
    status子命令(1: [s]tatus)功能上和git add -i相似
    quit子命令
    quit子命令(7: [q]uit)用于退出git add -i命令系统
    帮助
    我们可以通过git add -h命令来看git add命令的帮助文档。
     git add -h
    usage: git add [options] [--] <filepattern>...

        -n, --dry-run         dry run
        -v, --verbose         be verbose

        -i, --interactive     interactive picking
        -p, --patch           select hunks interactively
        -e, --edit            edit current diff and apply
        -f, --force           allow adding otherwise ignored files
        -u, --update          update tracked files
        -N, --intent-to-add   record only the fact that the path will be added later
        -A, --all             add changes from all tracked and untracked files
        --refresh             don't add, only refresh the index
        --ignore-errors       just skip files which cannot be added because of errors
        --ignore-missing      check if - even missing - files are ignored in dry run
     
    git commit
    git commit 

    提交修改到本地版本库,只使用这个命令会打开一个编辑器让你填入修改信息

    git commit -m "<message>"

    提交修改到版本库

    git commit -a

    提交修改到本地版本库,使用 -a 参数添加文件的修改到暂存区(省去 git add修改的文件的步骤)

     git status
    git status

    列出放入暂存区的,未放入暂存区的,和新建但没add 的文件

     
     
    git log
    git log

    显示提交的修改

    git log -n <limit>

    显示 limit 条修改信息

    git log --oneline 

    log 信息单行显示

    git log --stat

    显示log信息,包含文件修改(列出某次提交修改了哪些文件)

    git log -p

    显示log信息,包含文件内容的修改(列出某次提交修改了哪些文件,并显示内容的修改)

    git log --author="<author>"

    显示log信息,按照提交者过滤

    git log --grep="<pattern>"

    显示log信息,按照pattern(可以是正则表达式) 过滤提交信息中包含pattern的提交

    git log <since>..<until>

    Show only commits that occur between <since> and <until>. Both arguments can be either a commit ID, a branch name, HEAD, or any other kind of revision reference.

    git log <file>

    只显示包含<file>的提交

    git log --graph  --oneline 

    以图形的形式显示日志提交结构

    A few useful options to consider. The --graph flag that will draw a text based graph of the commits on the left hand side of the commit messages. --decorate adds the names of branches or tags of the commits that are shown. --oneline shows the commit information on a single line making it easier to browse through commits at-a-glance.
     
    参考链接
    1. https://www.atlassian.com/git/tutorial/git-basics
    2. http://hubingforever.blog.163.com/blog/static/171040579201231110371044/
    3. http://hi.baidu.com/aatfjctoytaefkr/item/00c693450a5b36af60d7b93f
     
  • 相关阅读:
    openssh升级
    Mysql基础学习_Windows版(一)
    centos7 取消Ctrl+Alt+Del重启功能
    linux shell数值比较和字符串比较
    linux 使用中括号进行条件判断
    VXLAN简介(摘抄)
    centos配置NTP服务器
    Centos配置网卡子接口
    组播___IGMP
    组播
  • 原文地址:https://www.cnblogs.com/cacique/p/3433608.html
Copyright © 2020-2023  润新知