[2] Git repository

First, Git repositories

Initialization warehouse

##基于当前目录初始化仓库
$ git init
##指定demo目录初始化仓库
$ git init demo

Cloning an existing warehouse

##克隆现有的仓库,默认目录名:libgit2
##git clone <repo>
$ git clone https://github.com/libgit2/libgit2
##克隆现有的仓库,指定目录名:mylibgit
##git clone <repo> <directory>
$ git clone https://github.com/libgit2/libgit2 mylibgit

Note: Git clone is almost every version of every file on the Git repository server, rather than just copying your work done by the required documents, which is an important characteristic different from other Git version control system.

Second, the staging area operation

##直接从暂存区删除file文件工作区则不做出改变
$ git rm --cached <file>
##暂存区的目录树会被 master 分支指向的目录树所替换,工作区不受影响
$ git reset HEAD
##用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件,同时清除工作区和暂存区中未提交的改动
git checkout HEAD .
git checkout HEAD <file>
##用暂存区全部或部分文件替换工作区的文件,会清除工作区中未添加到暂存区的改动
$ git checkout .
$ git checkout -- <file>

Third, add version control

commit document flow

##进入仓库目录创建文件
$ cd /demo
$ touch demo_01.c
$ touch demo_02.c
$ mkdir LICENSE
##添加文件追踪track,即添加文件至暂存区
$ git add *.c
$ git add LICENSE
##提交文件至版本库,-m 参数指定提交log
$ git commit -m 'initial project version'

Note: Use a file or directory path git add command as an argument. If the argument is the path to the directory, the command will recursively keep track of all files in that directory.

File Status
New File status: ??

$ cd /home/super/gitDemo
$ touch a.txt
$ git status -s

After adding track, file status is: A

git add a.txt
git status -s

Before not commit, modify the file, view the file status: AM

echo "a" > a.txt
git status -s

After the track again, state: A

git add a.txt
git status -s

After performing commit, file synchronization, stateless

git commit a.txt -m "init a.txt"
git status -s

After performing commit, modify the file again, the status is: M (Right M)

echo "b" > a.txt
git status -s

After the track again, the state is: M (left M)

git add a.txt
git status -s

Before not commit, modify the file again, the status is: MM

echo "d" > a.txt
git status -s

After the track again, the state is: M (left M)

git add a.txt
git status -s

Perform commit, file synchronization, stateless

git commit a.txt -m "init a.txt 0.1"
git status -s

BRIEF DESCRIPTION OF git status Status

mark Explanation
?? No trace files newly added
A  The new file is added to the staging area
AM After the new file is added to the staging area, the former did not commit, modified in the workspace
 M (M on the right) has been over-commit the file has been modified but not yet add temporary storage area in the work area
M  (M on the left) has been over-commit the file is modified in the workspace and has add the staging area
MM Had been commit file is modified and submitted to the staging area in the work area and then be modified in the workspace

Ignore tracking configuration file
Git allows you to specify not need to be incorporated into the management of documents, so they do not appear in the list of untracked files.

##创建文件.gitignore
cd <dir>
touch .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

.Gitignore file format specification is as follows:

  • All blank lines or lines beginning with # will be ignored Git
  • You can use the standard pattern matching glob
  • Match mode may start with (/) to prevent recursion
  • Match mode may end in a specified directory (/)
  • To ignore the file or directory other than the specified mode, you can add an exclamation point in the current mode (!) Negated

说明: glob 模式指 shell版简化的正则表达式。
  星号 * 匹配零个或多个任意字符;
  [abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);
  问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字);
  使用两个星号 * 表示匹配任意中间目录,比如 a/**/z 可以匹配 a/z , a/b/z 或 a/b/c/z 等。

参考资料

参考网站:https://git-scm.com/book/zh/v1/起步

  • 作者: DeepInThought
    出处: https://www.cnblogs.com/DeepInThought
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • Guess you like

    Origin www.cnblogs.com/DeepInThought/p/11108179.html