腾讯课堂python
git是一个分布式的版本控制软件。版本控制是一种记录一个或若干文件内容变化,以便未来查阅特定版本修订状况的系统。linux
最初由林納斯·托瓦茲創做,於2005年以GPL釋出。最初目的是為更好地管理Linux內核開發而設計。git
2005年,安德鲁·垂鸠写了一个简单程序,能够链接BitKeeper的存储库,BitKeeper著做权拥有者拉里·麦沃伊认为安德鲁·垂鸠对BitKeeper内部使用的协议进行逆向工程,决定收回免费使用BitKeeper的许可。Linux内核开发团队与BitMover公司进行磋商,但没法解决他们之间的歧见。林纳斯·托瓦兹决定自行开发版本控制系统替代BitKeeper,以十天的时间,编写出第一个git版本.github
集中化的版本控制系统(Centralized Version Control Systems
,简称 CVCS
)应运而生。 这类系统,诸如 CVS
、Subversion
以及 Perforce
等,都有一个单一的集中管理的服务器,保存全部文件的修订版本,而协同工做的人们都经过客户端连到这台服务器,取出最新的文件或者提交更新。web
集中式管理的缺点:shell
分布式版本控制系统(Distributed Version Control System
,简称 DVCS
).分布式就是每一个人都有一份完整的仓库版本,提交和管理都是在本地进行,固然也能够进行远程的仓库之间进行合并提交等操做。windows
若是你想在 Linux 上用二进制安装程序来安装 Git,可使用发行版包含的基础软件包管理工具来安装。 若是 以 Fedora 上为例,你可使用 yum:ruby
$ sudo yum install git
若是你在基于 Debian 的发行版上,请尝试用 apt-get:bash
$ sudo apt-get install git
要了解更多选择,Git 官方网站上有在各类 Unix 风格的系统上安装步骤,网址为 http://git-scm.com/download/linux。服务器
方法一:
最简单的方法是安装 Xcode Command Line Tools
,而后在 Terminal 里尝试首次运行 git 命令便可。 若是没有安装过命令行开发者工具,将会提示 你安装。
方法二:(推荐)
使用 homebrew
$ brew install git
直接下载安装包:https://gitforwindows.org/
windows请从开始菜单程序中打开gitbash
,其余系统请打开终端工具或者shell命令行工具。
$ git --version
# 输出,仅供参考 git version 2.18.0
当安装完 Git 应该作的第一件事就是设置你的用户名称与邮件地址。 这样作很重要,由于每个 Git 的提交都会 使用这些信息,而且它会写入到你的每一次提交中。
windows请从开始菜单程序中打开gitbash
,其余系统请打开终端工具或者shell命令行工具。
$ git config --global user.name "aicoder" $ git config --global user.email laoma@aicoder.com
请把用户名和邮箱换成你本身的
若是使用了 --global 选项,那么该命令只须要运行一次,由于以后不管你在该系统上作任何事 情, Git 都会使用那些信息。 当你想针对特定项目使用不一样的用户名称与邮件地址时,能够在那个项目目录下运 行没有 --global 选项的命令来配置。
以上配置完成后,检测是否配置成功:
$ git config --list
# 输出如下内容,仅供参考 credential.helper=osxkeychain user.name=aicoder user.email=laoma@aicoder.com color.status=auto ...
Git
服务器大部分都使用 SSH
公钥进行认证,其余认证方式都不方便,建立SSH
公钥的方法。
请打开用gitbash或者终端工具运行如下命令
默认状况下,用户的 SSH
密钥存储在其 ~/.ssh
目录下。 进入该目录并列出其中内容,你即可以快 速确认本身是否已拥有密钥:
$ cd ~/.ssh $ ls authorized_keys2 id_dsa known_hosts config id_dsa.pub
cd命令能够帮助咱们进入系统的某个目录。 ls命令能够列出当前目录下面的全部文件和文件夹的信息。
若是已经存在id_dsa
、id_dsa.pub
,说明就已经生成果,后面的步骤能够省略。
id_dsa 或 id_rsa 命名的文件,其中一个带有 .pub 扩展名。 .pub 文件是你的公钥,另 一个则是私钥。
$ ssh-keygen
# 一路回车回车便可,如下为输出内容,仅供参考 Generating public/private rsa key pair. Enter file in which to save the key (/home/schacon/.ssh/id_rsa): Created directory '/home/schacon/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/schacon/.ssh/id_rsa. Your public key has been saved in /home/schacon/.ssh/id_rsa.pub. The key fingerprint is: d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 schacon@mylaptop.local
首先 ssh-keygen 会确认密钥的存储位置(默认是 .ssh/id_rsa),而后它会要求你输入两次密钥口令。如 果你不想在使用密钥时输入口令,将其留空便可。
$ cd /path/to/init $ git init
此时在目录中将建立一个名为 .git
的子目录,这里面存放当前仓库的全部的跟踪的信息。
此时当前文件夹下面的全部的文件都没有被跟踪,若是须要跟踪变化,必须添加到跟踪的参考中。
git add 文件
命令能够帮祝咱们让git帮忙跟踪具体的文件。而后执行git commit
提交信息,至关于确认跟踪。
$ git add ./*.js
$ git add a.txt
$ git commit -m 'first commit'
提交记录的时候必须添加消息,并且添加的消息还有必定的规范,每一个公司的提交消息规范不同,视状况而定。
工做区,也称为工做目录,也就是要进行版本管理的文件夹。
暂时存放将要记录修改版本的文件的区域。
工做目录下的每个文件都不外乎这两种状态:已跟踪或未跟踪。
git add
能够把文件加入暂存区。 git commit
命令能够把暂存区的文件更新变化记录到版本库中永久保存。
不在暂存区的文件,不会被追踪。
暂存区和版本库存放在 .git目录中。
要查看哪些文件处于什么状态,能够用 git status 命令。 若是在克隆仓库后当即使用此命令,会看到相似这 样的输出:
$ git status
如下是没有任何修改和暂存的状况:
On branch master
Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
如下是有修改未提交,有新增长为加入暂存区的状况:
On branch master
Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: docs/pages/vip_3git.md Untracked files: (use "git add <file>..." to include in what will be committed) docs/images/0.jpeg docs/images/zc.png no changes added to commit (use "git add" and/or "git commit -a")
git status
命令的输出十分详细,但其用语有些繁琐。 若是你使用 git status -s
命令或 git status --short
命令,你将获得一种更为紧凑的格式输出。运行git status -s
,状态报告输出以下:
$ git status -s M README MM Rakefile A lib/git.rb M lib/simplegit.rb ?? LICENSE.txt
git log
命令帮助咱们输出git的全部操做日志。
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com> Date: Mon Mar 17 21:52:11 2008 -0700 changed the version number commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 Author: Scott Chacon <schacon@gee-mail.com> Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary test commit a11bef06a3f659402fe7563abf99ad00de2209e6 Author: Scott Chacon <schacon@gee-mail.com> Date: Sat Mar 15 10:31:28 2008 -0700 first commit
按q键能够退出查看日志,回车键查看更多。
git log -p
git log -p -2
正如你所看到的,--stat
选项在每次提交的下面列出全部被修改过的文件、有多少文件被修改了以及被修改过 的文件的哪些行被移除或是添加了。 在每次提交的最后还有一个总结。
git log --stat
git log --oneline
git log --graph
其余参数
选项 | 说明 |
---|---|
-p | 按补丁格式显示每一个更新之间的差别。 |
--stat | 显示每次更新的文件修改统计信息。 |
--shortstat | 只显示 --stat 中最后的行数修改添加移除统计。 |
--name-only | 仅在提交信息后显示已修改的文件清单。 |
--name-status | 显示新增、修改、删除的文件清单。 |
--abbrev-commit | 仅显示 SHA-1 的前几个字符,而非全部的 40 个字符。 |
--relative-date | 使用较短的相对时间显示(好比,“2 weeks ago”)。 |
--graph | 显示 ASCII 图形表示的分支合并历史。 |
--pretty | 使用其余格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式) |
若是你不当心添加了一个文件,但本不想让它进行跟踪管理,仅仅是临时使用,那么怎样才能从暂存区取消呢?
# 进入项目目录 $ cd /path/to # 添加一个文件 $ touch a.txt # 添加到暂存区 $ git add a.txt
此时查看git的状况:
$ git status
# 查看当前文件,在暂存区等待提交。 On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: a.txt
取消暂存:
$ git reset HEAD a.txt
$ git status
# 能够看到a.txt是未跟踪状态了。
若是一个文件已经被跟踪过了,并且已经修改后被add到了暂存区如今想取消修改暂存状态。
$ git reset HEAD a.txt $ git checkout -- a.txt
git checkout -- [file]是一个危险的命令。你对那个文件作的任何修改都会消失 - 你只是拷贝了另外一个文件来覆盖它。
所有回滚:
git reset --hard HEAD
--hard
会让工做目录也会回滚到未修改以前的状态。
让某个文件回滚到某个版本的状态。
$ git checkout -- filepath
通常咱们总会有些文件无需归入 Git 的管理,也不但愿它们总出如今未跟踪文件列表。 一般都是些自动生成的文 件,好比日志文件,或者编译过程当中建立的临时文件等。 在这种状况下,咱们能够建立一个名为 .gitignore
的文件,列出要忽略的文件模式。
咱们再看一个 .gitignore 文件的例子: '
# no .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in the build/ directory build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory doc/**/*.pdf
通常此文件会放到工做目录的根目录下,此文件中匹配的全部文件都会被git全部的命令忽略。
<pre v-pre="" data-lang="sh" Roboto Mono", Monaco, courier, monospace; line-height: 1.5rem; margin: 1.2em 0px; overflow: auto; padding: 0px 1.4rem; position: relative; overflow-wrap: normal; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Copy to clipboardErrorCopied </pre>
![<pre v-pre="" data-lang="sh" Roboto Mono", Monaco, courier, monospace; line-height: 1.5rem; margin: 1.2em 0px; overflow: auto; padding: 0px 1.4rem; position: relative; overflow-wrap: normal; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Copy to clipboardErrorCopied </pre>