taichiyi / git-document-tcy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo

git

init clone remote fetch pull push config add rm

pic

init

创建一个空的git仓库或重新初始化一个已有的仓库

语法

git init [-q | --quiet] [--bare] [--template=<template_directory>]
    [--separate-git-dir <git dir>]
    [--shared[=<permissions>]] [directory]

实例

$ git init

clone

克隆一个版本库到新目录

语法

git clone [--template=<template_directory>]
          [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
          [-o <主机名>] [-b <主机名>] [-u <upload-pack>] [--reference <repository>]
          [--dissociate] [--separate-git-dir <git dir>]
          [--depth <depth>] [--[no-]single-branch]
          [--recursive | --recurse-submodules] [--[no-]shallow-submodules]
          [--jobs <n>] [--] <repository> [<directory>]

实例

<repository>的种类

$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git 
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

克隆版本库的时候,所使用的远程主机自动被Git命名为origin。如果想用其他的主机名,需要用git clone命令的-o选项指定。

$ git clone -o jQuery https://github.com/jquery/jquery.git

remote

管理查询版本库

语法

git remote [-v | --verbose]
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <主机名> <网址>
git remote rename <old> <new>
git remote remove <主机名>
git remote set-head <主机名> (-a | --auto | -d | --delete | <branch>)
git remote set-branches [--add] <主机名> <branch>…
git remote get-url [--push] [--all] <主机名>
git remote set-url [--push] <主机名> <newurl> [<oldurl>]
git remote set-url --add [--push] <主机名> <newurl>
git remote set-url --delete [--push] <主机名> <网址>
git remote [-v | --verbose] show [-n] <主机名>…
git remote prune [-n | --dry-run] <主机名>…
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)…]

实例

列出所有远程主机

$ git remote

列出所有远程主机的网址

$ git remote -v

查看指定远程主机的详细信息

$ git remote show <主机名>
$ git remote show origin

添加远程主机

$ git remote add <主机名> <网址>
$ git remote add jQuery https://github.com/jquery/jquery.git

删除远程主机

$ git remote rm <主机名>
$ git remote rm jQuery

修改远程主机名

$ git remote rename <原主机名> <新主机名>
$ git remote rename jQuery jQuery1

fetch

取回本地

一旦远程主机的版本库有了更新(Git术语叫做commit),需要将这些更新取回本地,这时就要用到git fetch命令。

语法

git fetch [<options>] [<repository> [<refspec>…​]]
git fetch [<options>] <group>
git fetch --multiple [<options>] [(<repository> | <group>)…​]
git fetch --all [<options>]

实例

将某个远程主机的更新,全部取回本地。

$ git fetch <远程主机名>
$ git fetch origin

取回远程主机某个分支的更新

$ git fetch <远程主机名> <分支名>
$ git fetch origin master

pull

取回本地+合并

git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。

语法

git pull [options] [<repository> [<refspec>…​]]

实例

取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。

$ git pull origin next:master

如果远程分支是与当前分支合并,则冒号后面的部分可以省略。 取回origin/next分支,再与当前分支合并。实质上,等同于先做git fetch,再做git merge

$ git pull origin next

push

推出本地

git push命令用于将本地分支的更新,推送到远程主机。它的格式与git pull命令相仿。

语法

git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
         [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
         [-u | --set-upstream] [--push-option=<string>]
         [--[no-]signed|--sign=(true|false|if-asked)]
         [--force-with-lease[=<refname>[:<expect>]]]
         [--no-verify] [<repository> [<refspec>…​]]

实例

$ git push <远程主机名> <本地分支名>:<远程分支名>

本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建。

$ git push origin master

删除origin主机的master分支。

$ git push origin :master
# 等同于
$ git push origin --delete master

如果当前分支与多个主机存在追踪关系,则可以使用-u选项指定一个默认主机,这样后面就可以不加任何参数使用git push

$ git push -u origin master

还有一种情况,就是不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机,这时需要使用--all选项。

$ git push --all origin

如果远程主机的版本比本地版本更新,推送时Git会报错,要求先在本地做git pull合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用--force选项。

$ git push --force origin

config

配置 git

所有的命令都存在三个配置级别:system(系统级), global(全局级), local(局部级)。可以通过指定一个参数:--system,--global,--local 来指定是对哪一级别的设置

配置信息的优先级: local > global > system

语法

git config [<file-option>] [type] [--show-origin] [-z|--null] name [value [value_regex]]
git config [<file-option>] [type] --add name value
git config [<file-option>] [type] --replace-all name value [value_regex]
git config [<file-option>] [type] [--show-origin] [-z|--null] --get name [value_regex]
git config [<file-option>] [type] [--show-origin] [-z|--null] --get-all name [value_regex]
git config [<file-option>] [type] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
git config [<file-option>] [type] [-z|--null] --get-urlmatch name URL
git config [<file-option>] --unset name [value_regex]
git config [<file-option>] --unset-all name [value_regex]
git config [<file-option>] --rename-section old_name new_name
git config [<file-option>] --remove-section name
git config [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
git config [<file-option>] --get-color name [default]
git config [<file-option>] --get-colorbool name [stdout-is-tty]
git config [<file-option>] -e | --edit

实例

查看配置信息

# 查看所有配置信息
$ git config --list
# 等同于
$ git config -l

# 系统级(mac下此配置文件默认是没有的)
$ git config --system -l

# 全局级
$ git config --global -l

# 局部级
$ git config --local -l

查看-某个配置项

# 查看username
$ git config user.name

# 查看email
$ git config user.email

添加-配置项

$ git config --global user.name "taichiyi"
$ git config --global user.email "taichiyi@foxmail.com"

编辑-配置

git config --global -e

alias

我们知道我们执行的一些Git命令其实操作很频繁的类似有:

git status git add git commit git push git checkout git branch

这些操作非常频繁,每次都要输入完全是不是有点麻烦,有没有一种简单的缩写输入呢?完整输入 => 缩写输入:

git status => git s git add => git a git commit => git c git push => git p git checkout => git co git branch => git br

对应配置如下:

$ git config --global alias.s status
$ git config --global alias.a add
$ git config --global alias.c commit
$ git config --global alias.p push
$ git config --global alias.co checkout
$ git config --global alias.br branch

当然以上别名不是固定的,你完全可以根据自己的习惯去定制,除此之外还可以设置组合,比如:

git push origin master => git pom

对应配置如下:

$ git config --global alias.pom 'push origin master'

删除别名

$ git config --global --unset alias.pom

add

把文件添加到index

语法

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
        [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
        [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing]
        [--chmod=(+|-)x] [--] [<pathspec>…​]

实例

添加所有文件

$ git add .

只把有变动的文件添加到index里(update)

$ git add -u

rm

删除远程仓库文件

语法

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​

实例

-n查看要删除的文件, -r递归删除

$ git rm -r -n --cached .vscode

删除.vscode文件

$ git rm -r --cached .vscode

参考链接

https://git-scm.com/docs/
http://stormzhang.com/github/2016/06/16/learn-github-from-zero5/
http://www.ruanyifeng.com/blog/2014/06/git_remote.html

About

License:MIT License