一、git简介
git(读音为/gɪt/。)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
二、配置git
1.安装git
[root@foundation66 ~]# yum install git
[root@foundation66 ~]# rpm -q git
git-1.8.3.1-5.el7.x86_64
2.创建版本库
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。所以,创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录:
通过git init
命令把这个目录变成Git可以管理的仓库,.git
的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了
3.把文件添加到版本库
1)编写一个readme.txt文件
2)用命令git add
告诉Git,把文件添加到仓库:
3)用命令git commit
告诉Git,把文件提交到仓库
通过上面的提示,修改用户信息
当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址。 这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改:
[kiosk@foundation66 git]$ git config --global user.name "yxx"
[kiosk@foundation66 git]$ git config --global user.email yxx123@westos.org
[kiosk@foundation66 git]$ cd
[kiosk@foundation66 ~]$ l.
. .bashrc .gnome .ipython .mozilla
.. .cache .gnome2 .java .PyCharmCE2016.3
.bash_history .config .gnome2_private .kingsoft .ssh
.bash_logout .esd_auth .gnupg .local .viminfo
.bash_profile .gitconfig .ICEauthority .matplotlib
[kiosk@foundation66 ~]$ cat .gitconfig
[user]
name = yxx
email = yxx123@westos.org
如果使用了 --global
选项,那么该命令只需要运行一次,因为之后无论你在该系统上做任何事情, Git 都会使用那些信息。 当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global
选项的命令来配置。
??
标记:新添加的未跟踪文件
A
标记 :新添加到暂存区中的文件 修改过的文件前面有。
M
标记 :文件被修改过。M
有两个可以出现的位置,出现在右边的 M
表示该文件被修改了但是还没放入暂存区
出现在靠左边的 M
表示该文件被修改了并放入了暂存区
[kiosk@foundation66 git]$ vim readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
[kiosk@foundation66 git]$ git status -s ##查看当前文件状态
M readme.md ##文件被修改,但还没放入暂存区
[kiosk@foundation66 git]$ git add readme.md ##把已跟踪的文件放到暂存区
[kiosk@foundation66 git]$ git status -s
M readme.md ##文件被修改并放入了暂存区
[kiosk@foundation66 git]$ vim test.txt
[kiosk@foundation66 git]$ git status -s
M readme.md
?? test.txt ##新添加的未跟踪文件
[kiosk@foundation66 git]$ vim hello.txt
[kiosk@foundation66 git]$ git add hello.txt
[kiosk@foundation66 git]$ git status -s
A hello.txt ##新添加到暂存区中的文件
M readme.md
?? test.txt
[kiosk@foundation66 git]$ vim file.txt
[kiosk@foundation66 git]$ git add file.txt
[kiosk@foundation66 git]$ git status -s
A file.txt
A hello.txt
M readme.md
?? test.txt
[kiosk@foundation66 git]$ git commit -m "add file.txt"
[master 1033be6] add file.txt
3 files changed, 4 insertions(+), 1 deletion(-)
create mode 100644 file.txt
create mode 100644 hello.txt
[kiosk@foundation66 git]$ git status -s
?? test.txt
[kiosk@foundation66 git]$ vim readme.md
[kiosk@foundation66 git]$ git status -s
M readme.md
?? test.txt
[kiosk@foundation66 git]$ vim hello.txt
[kiosk@foundation66 git]$ git add hello.txt
[kiosk@foundation66 git]$ git status -s
M hello.txt
M readme.md
?? test.txt
[kiosk@foundation66 git]$ vim file.txt
[kiosk@foundation66 git]$ git add file.txt
[kiosk@foundation66 git]$ git status -s
M file.txt
M hello.txt
M readme.md
?? test.txt
[kiosk@foundation66 git]$ vim file.txt
[kiosk@foundation66 git]$ git status -s
MM file.txt ##在工作区被修改并提交到暂存区后又在工作区中被修改了
M hello.txt
M readme.md
?? test.txt
[kiosk@foundation66 git]$ git commit -m "v4"
[master 6fb7b92] v4
2 files changed, 2 insertions(+)
[kiosk@foundation66 git]$ git status -s
M file.txt
M readme.md
?? test.txt
[kiosk@foundation66 git]$ git add file.txt readme.md
[kiosk@foundation66 git]$ git commit -m "v5"
[master a37f1de] v5
2 files changed, 3 insertions(+)
[kiosk@foundation66 git]$ git status -s
?? test.txt
忽略文件
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore
的文件,列出要忽略的文件模式。
[kiosk@foundation66 git]$ vim .gitignore
[kiosk@foundation66 git]$ cat .gitignore
*.[oa]
*~
第一行告诉 Git 忽略所有以 .o
或 .a
结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。 第二行告诉 Git 忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副本。
版本回退
git log
命令显示从最近到最远的提交日志,以便确定要回退到哪个版本。如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline
参数:
你看到的一大串类似a37f...
的是commit id
(版本号),是一个SHA1计算出来的一个非常大的数字,用十六进制表示。
好了,现在我们启动时光穿梭机,准备把v5
回退到上一个版本,也就是v4
的那个版本,怎么做呢?
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD
表示当前版本,上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。
回到原来的v5版本
git reflog
查看命令历史,以便确定要回到未来的哪个版本
git reset --hard a37f1de
再看文件内容:
Git的撤消修改
如果你现在的工作目录(work tree)里搞的一团乱麻, 但是你现在还没有把它们提交; 你可以通过下面的命令, 让工作目录回到上次提交时的状态(last committed state)
[kiosk@foundation66 git]$ cat readme.md
hello
hello
##在无意间添加了一行
[kiosk@foundation66 git]$ vim readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
cndsns
[kiosk@foundation66 git]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
# (use "git push" to publish your local commits)
#
# 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: readme.md
在你准备提交前,你猛然发现了这行错误,
就可以很容易地纠正它。你可以删掉最后一行,手动把文件恢复到上一个版本的状态
[kiosk@foundation66 git]$ vim readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
[kiosk@foundation66 git]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
# (use "git push" to publish your local commits)
#
或者根据提示,使用 git checkout -- file
可以丢弃工作区的修改:
[kiosk@foundation66 git]$ git checkout -- readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
文件内容果然复原了。
命令git checkout -- readme.txt
意思就是,把readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit
或git add
时的状态。
现在我们实验第二种状态:
我无意间添加了一行,还git add 到暂存区,然后还做了修改:
[kiosk@foundation66 git]$ cat readme.md
hello
hello
[kiosk@foundation66 git]$ vim readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
cndjncdn
[kiosk@foundation66 git]$ git add readme.md
[kiosk@foundation66 git]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.md
[kiosk@foundation66 git]$ vim readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
cndsns
cnidsncjnsdis
[kiosk@foundation66 git]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.md
#
# 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: readme.md
现在用命令git checkout --撤销修改,文件就回到了添加到暂存区后的状态,再用命令git reset HEAD <file>
可以把暂存区的修改撤销掉(unstage),重新放回工作区
[kiosk@foundation66 git]$ git checkout -- readme.md ##撤销修改
[kiosk@foundation66 git]$ cat readme.md ##文件以恢复
hello
hello
cndsns
[kiosk@foundation66 git]$ git status ##查看状态
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.md
#
[kiosk@foundation66 git]$ git reset HEAD readme.md
Unstaged changes after reset:
M readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
cndsns
再用git status
查看一下,现在暂存区是干净的,工作区有修改:
[kiosk@foundation66 git]$ git status
# On branch 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: readme.md
还记得如何丢弃工作区的修改吗?
[kiosk@foundation66 git]$ git checkout -- readme.md
[kiosk@foundation66 git]$ cat readme.md
hello
hello
文件内容恢复到了最初的状态
Git的删除文件
在Git中,删除也是一个修改操作,我们先添加一个新文件test.txt
到Git并且提交:
[kiosk@foundation66 git]$ ls
gitskills hello.txt readme.md
[kiosk@foundation66 git]$ vim test.txt
[kiosk@foundation66 git]$ git add test.txt
[kiosk@foundation66 git]$ git commit -m "add test.txt"
[master 0612651] add test.txt
1 file changed, 2 insertions(+)
create mode 100644 test.txt
这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status
命令会立刻告诉你哪些文件被删除了:
[kiosk@foundation66 git]$ rm test.txt
[kiosk@foundation66 git]$ ls
gitskills hello.txt readme.md
[kiosk@foundation66 git]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
# (use "git push" to publish your local commits)
#
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: test.txt
#
现在有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm
删掉,并且git commit
:
[kiosk@foundation66 git]$ git rm test.txt
rm 'test.txt'
[kiosk@foundation66 git]$ git commit -m "remove test.txt"
[master e635287] remove test.txt
1 file changed, 2 deletions(-)
delete mode 100644 test.txt
现在,文件就从版本库中被删除了。
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本.
$ git checkout -- test.txt
命令git rm
用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
利用github搭建远程仓库
1)注册github帐号
2)点击创建一个项目,起名为demo
由于我们的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以需要点击右上角人头像,点击设置,找到ssh and gpg keys
选择ssh keys
为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。当然,GitHub允许你添加多个Key。假定你有若干电脑,你一会儿在公司提交,一会儿在家里提交,只要把每台电脑的Key都添加到GitHub,就可以在每台电脑上往GitHub推送了。
shell里执行ssh-keygen,一路回车,生成公钥和私钥
cat id_rsa.pub查看公钥内容,将其复制到如下图地方:
完成后点击你刚创建的demo
将远程库和本地库进行关联
[kiosk@foundation66 git]$ git remote add origin git@github.com:Owlcity-123/demo.git
[kiosk@foundation66 git]$ git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (19/19), 1.54 KiB | 0 bytes/s, done.
Total 19 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/Owlcity-123/demo/pull/new/master
remote:
To git@github.com:Owlcity-123/demo.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样:
从现在起,只要本地作了提交,就可以把最新修改推送至GitHub
在浏览器中查看:
从远程库克隆
上面我们实验是先有本地库,后有远程库的时候,如何关联远程库。假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆
1)首先,登陆GitHub,创建一个新的仓库,名字叫gitskills
:
我们勾选Initialize this repository with a README
,这样GitHub会自动为我们创建一个README.md
文件。创建完毕后,可以看到README.md
文件:
2)现在,远程库已经准备好了,下一步是用命令git clone
克隆一个本地库: