For detailed acquaintance Git Git commands and automated deployment of --Git

A, Git principle

1.1 What is Git

Git is a worldwide are popular Distributed version control systems. It has no central server, each person's computer is a complete repository, do not work when networking, because the versions are on your own computer. Git was originally developed to assist the Linux kernel. Git was written by the father of Linux Linus Torvalds.

  • What is version control it? Version Control (Revision control) is a method for managing the development process we revise history and other files, directories or engineering content for easy viewing change history, backup to restore a previous version of the software engineering technology.It simply is the technology used to manage more than collaborative development projects.
  • Advantage : suitable for distributed development, emphasis on the individual; can easily resolve any conflict between two developers; offline work; fast, flexible; public pressure on the server and the amount of data that will not be too large.

1.2 Git workflow

The general workflow is as follows:

  1. Git clone resources as a working directory.
  2. Add or modify files on resources clones.
  3. If someone else changed, you can update the resource.
  4. Check changes before submission.
  5. Submitted.
  6. After modifications are complete, if an error is found, you can withdraw and submit revised and submitted again.

Here Insert Picture Description

Two, Git is simple to install and use

2.1 Installing Git

yum install git -y

Enter git --version, you can view the git version
Here Insert Picture Description
Here Insert Picture Description

2.2 Creating Warehouse

  • warehouseThis all files in the repository can be Git to manage, modify each file, delete, Git can track order history can be traced any time, or at some point you can "restore" in the future.

1、创建一个目录 mkdir repository
2、通过git init命令把这个目录变成Git可以管理的仓库。
Here Insert Picture Description
此时,repository版本库里会多一个 隐藏目录.git,此目录是 Git来跟踪管理版本库的
Here Insert Picture Description
Here Insert Picture Description

2.3 添加文件到版本库

  • 所有的版本控制系统,只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外。版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词“Linux”,在第8行删了一个单词“Windows”。而图片、视频这些二进制文件,虽然也能由版本控制系统管理,但没法跟踪文件的变化,只能把二进制文件每次改动串起来,也就是只知道图片从100KB改成了120KB,但到底改了啥,版本控制系统不知道,也没法知道。

1、用命令git add告诉Git,把文件添加到仓库

  • 命令git add ,可反复多次使用,添加多个文件

Here Insert Picture Description
2、用命令git commit告诉Git,把文件提交到仓库

git commit -m "test.txt submit"
  • 命令git commit,可以一次提交很多文件,-m后面输入的是本次提交的说明,可以输入任意内容,最好是有意义的,这样就能从历史记录里方便地找到改动记录。

Here Insert Picture Description
3、修改test.txt文件内容,运行git status,告诉我们文件被修改过。

  • git status命令:查看仓库当前的状态
    Here Insert Picture Description
  • git commit -a : 跳过使用暂存区域,自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤

4、查看文件test.txt,如何被修改,运行git diff

  • 命令git diff:查看difference,查看文件具体的改变

Here Insert Picture Description
5、重新添加修改后的文件,且提交。

Here Insert Picture Description
在Git中,每当你文件修改完成时,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作。

6、我们可以用git log命令,查看文件更改的历史记录:

  • git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是chenged test.txt,second(我自己又提交了一次),最远一次test.txt submit

Here Insert Picture Description

2.4 版本回退

现在,我们要把当前版本chenged test.txt,second回退到上一个版本chenged test.txt

1、使用git reset --hard命令,回退:

回退到上一版:	git reset --hard HEAD^
回退到倒数第二版: git rest --hard HEAD^^
回退到commit id为xxxxxxx的版本 : git rest --hard xxxxxxx
  • 在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

Here Insert Picture Description

  • git reset 和 git reset --hard区别:
    git reset(git reset –-soft)回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可。
    git reset -–hard彻底回退到某个版本,本地的源码也会变为上一个版本的内容,撤销的commit中所包含的更改被冲掉

2、我们现在又想回到之前的版本chenged test.txt,second,只要上面的命令行窗口还没有被关掉,找到之前版本chenged test.txt,secondcommit id即可回到之前的版本。

git reset --hard <commit id>

Here Insert Picture Description
当然,还可以使用git reflog查看命令历史,回到未来的某个版本。

Here Insert Picture Description

2.5 工作区和暂存区

Here Insert Picture Description

  • 工作区:在电脑里能看到的目录,我的repository文件夹就是一个工作区
  • 版本库:工作区的一个隐藏目录.git,是Git的版本库。其中,index就是暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫 HEAD

Here Insert Picture Description

前面,我们把文件往Git版本库里添加的时候,是分两步执行的:

  • 第一步: git add把文件添加进去,本质就是 把文件修改添加到暂存区
  • 第二步: git commit提交更改,本质就是 把暂存区的所有内容提交到当前分支

总的来说,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。
1、我们先对 test.txt进行修改,在新编辑一个test2在工作区。查看状态git statustest2从来没有被添加过,所以它的状态是Untracked

Here Insert Picture Description

2、使用两次命令git add,将两个文件都添加后,用git status再查看一下
Here Insert Picture Description
此时,暂存区的状态
Here Insert Picture Description
3、执行git commit 就可以一次性把暂存区的所有修改提交到分支

一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的
Here Insert Picture Description
此时,暂存区就没有任何内容了
Here Insert Picture Description
每次修改,如果不用 git add 到暂存区,那就不会加入到commit中,除非使用 git commit -a -m "xxx"

2.6 撤销修改和删除文件

1、 仅丢弃工作区的修改

git checkout -- file

2、 要丢弃已添加到暂存区的文件,但未再更改过

git reset HEAD <file>
	##把暂存区的修改撤销掉,重新放回工作区,用HEAD时,表示最新的版本
git checkout -- file
	##丢弃工作区的修改

3, to be discarded have been added to the staging area, it has been modified files :

git checkout -- file
	##撤销修改就回到添加到暂存区后的状态

4, delete the file
command git rmto delete a file.

  • If a file has been submitted to the repository, you never have to worry about mistakenly deleted, but can only restore files to the latest version, you will lose the contents of the most recent commit your changes.
Published 102 original articles · won praise 21 · views 5316

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/103484543
Git