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"命令有什么区别呢?html

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

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

什么状况下使用“git init"和"git init --bare"呢?服务器

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

       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 ide

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
相关文章
相关标签/搜索