Git foundation and entry

A, Git based on the concept

Git features a brief overview

  • Any time you can roll back to previous versions of the code ( git reset --hard );

  • It will not cover someone else's code (branch) when co-development;

  • Leaving revision history ( git log);

  • You can easily manage different version made version;

1, Git operating mode

operating

  • 90% or more operations on a personal computer

  • Add files, modify files, submit changes, view version history

  • Repository synchronization

  • The locally modified version pushed to the server

Version control system:


v2-f4ee2a2fa168608a17c7ba7823db69e5_hd.jpg


2, Git file storage

Note the different file storage and SVN:


v2-976c16828ec1f1c415a0f6c18cfe2534_hd.jpg


Git does not treat or save data in accordance with SVN that. Conversely, Git more like a data snapshot as a set of small file system. Every time you submit updates, or save the state of the project in Git, it was mainly to make a snapshot of all files and saves a snapshot of the index. For efficient, if files have not changed, Git does not re-store the file, but retain only files stored before a link to . Git more like a snapshot of the data to the stream.

Features summary:

  • Direct recording snapshots snapshoot. Rather than differences;

  • Almost all operations are performed locally;

  • Time maintain data integrity ;

  • Many operations add data only;

  • Three states file (only in three states)

    • Modified ( modified)

    • Has been staging ( staged)

    • Submitted ( committed)


3, Git file status

  • Git file: management has been the repository of documents;

  • Modified: the working directory ( working directorymodified Git file);

  • It has been staging: Staging Git perform operations on the modified file, the file is stored in temporary storage area ( staging area); (SVN does not pay attention to the concept of temporary storage area)

  • 已提交: 将已暂存的文件执行Git提交操作,将文件存入版本库(git directory);


v2-f6bcf219594929b4c172faa3eb33ce7f_hd.jpg


Git 仓库目录是 Git 用来保存项目的元数据和对象数据库的地方。 这是 Git 中最重要的部分,从其它计算机克隆仓库时,拷贝的就是这里的数据。

工作目录是对项目的某个版本独立提取出来的内容。 这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改。

暂存区域是一个文件,保存了下次将提交的文件列表信息,一般在 Git 仓库目录中。 有时候也被称作‘索引’,不过一般说法还是叫暂存区域。

基本的 Git 工作流程如下:

  1. 在工作目录中修改文件。

  2. 暂存文件,将文件的快照放入暂存区域git add

  3. 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 仓库目录git commit

git add 放到暂存区;
git commit 从暂存区放到版本仓库中;

4、本地版本库与服务器版本库

Git是分布式的。


v2-623d7f53fb001399e7df96c8ceea93ef_hd.jpg


二、Git常用命令

1、config和email


v2-86f6e19ae0d1656094005825c5229239_hd.jpg


关于配置user.nameuser.email:

可以有三个地方配置:

比如输入git config命令, 会出现下面三个信息,即系统级别system,全局global,和局部local。其中优先级不断升高。

zxzxin@zxzxin:~/Git/gitlearn$ git config
usage: git config [<options>]

Config file location
    --global              use global config file
    --system              use system config file
    --local               use repository config file
    -f, --file <file>     use given config file
    --blob <blob-id>      read config from given blob object

这里展示一下配置我们局部的gitlearn仓库:


v2-146412d308c346b1d0a3d776204b3c77_hd.jpg


git 提交的id( commit id)是一个摘要值,这个摘要值实际上是一个 sha1计算出来的。

2、git rm 和rm的区别

git rm :

  • 1、删除了一个文件

  • 2、将被删除的文件纳入到了暂存区(stage);(可以直接调用git commit来提交)

若想恢复被删除的文件,需要进行两个动作:

  • 1、git reset HEAD test2.txt,将待删除的文件从暂存区恢复到工作区;

  • 2、git checkout -- test2.txt, 将工作区的修改丢弃掉;

**rm **:

  • 只是将文件删除;注意: 这时,被删除的文件并未纳入到暂存区当中。

  • 这时是提交(git commit)不了的。要想纳入暂存区,必须要再调用一次git add

实战对比:


v2-33d6ed4e2b2bc93ec7c31cc61ae56d4b_hd.jpg


3、git mv 和 mv的对比

git mv(和git rm 类似):

  • 先完成重命名;

  • 然后提交到暂存区;

git mv演示:


v2-dfcbbef62c722144a15c03f02c2e8921_hd.jpg


而 mv:

  • 只是完成重命名;

  • 需要自己调用git add提交到暂存区;


v2-21a6733375e292d63058042a605f9980_hd.jpg



使用技巧: 可以通过 git commit --amend -m '修正内容'更改上次错误的提交消息。(就是 -m '')后面的提交信息写错了。

4、git log查看日志

git log -n 可以查看最近的n条日志。

git log --pretty=oneline以一行一行的方式显示出来。

查看git命令帮助:


v2-5c1efe17a4c5e67371071cfaef246e8e_hd.jpg


5、.gitignore文件

在开发中,如果我们不想将所有的文件都放到git当中,而是要忽略少数一些文件(比如jar包、配置文件等),我们可以在我们的目录创建一个.gitignore文件,git就不会将这些文件放入暂存区,也不会提交(不会加入版本控制系统)。


v2-00ef2c422ed94f7bcf364dcacabab5ed_hd.jpg


此时我们可以创建一个.gitignore文件:


v2-68e993c5bc2e4b6087d297c430a00956_hd.jpg


简而言之,放在.gitignore中的文件会直接被git无视

其中,放在 .gitignore中的文件也支持正则表达式。
例如:
  • *.a,会忽略所有.a结尾的文件;

  • !lib.a,不要忽略lib.a(排除这个忽略);

  • /test.txt,仅仅忽略项目根目录下的TODO文件,不包括子目录下的test.txt文件;

  • dir1/,忽略dir1/目录下的所有文件;

  • dir1/*.txtIgnored dir1/a.txtbut will not be included dir1/dir2/a.txt.

  • For example dir1/*.txtignores dir1all under the .txtdocuments, dir1/*/*.txtwill be ignored dir1at all layers under .txt, and dir1/**/*.txtignores dir1/any layer in the .txtdocument.


In .gitignoreusing #indicates a comment.


Original: the Java architecture notes

Free Java needs its own advanced data collection, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo distributed high concurrency and other tutorials, a total of 30G.
Portal: https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q


Guess you like

Origin blog.51cto.com/13672983/2416194