git basic theory and operation

git basic theory

  1. git work area

    Local Git has three work areas: working directory (Working Directory), the staging area (Stage / Index), resource library (Repository or Git Directory). If you add a remote git repository (Remote Directory) can be divided into four work areas. File conversion relationship between these four areas as follows:

    • Workspace: the workspace is where you usually store the project code
    • Index / Stage: staging area for temporary storage of your changes, in fact it's just a file, save the file to the list of information to be submitted
    • Repository: warehouse district (or local warehouse), where the data is secure, and there are you committed to all versions of the data. HEAD which points to the latest version into the warehouse
    • Remote: remote repository, managed code server, you can simply considered the project team in a computer for remote data exchange

    The three local area precisely, it should be pointed git repository HEAD version

    • Directory: Use a Git directory management, which is a warehouse that contains our work space and Git management space.
    • WorkSpace: the need for directory and file version control via Git, these directories and files in the workspace.
    • .git: Git storage directory management information, initialization is automatically created when the warehouse.
    • Index / Stage: staging area, or call the update area to be submitted, before submitting to enter repo, we can put all the updates in the staging area.
    • Local Repo: local warehouse, stored in a local repository; HEAD will be just the current development branch (branch).
    • Stash: hidden, it is a working state saving stack used to save / restore temporary state of WorkSpace.
  2. git workflow

    git workflow is generally like this:

    1, in the working directory to add, modify files;

    2, will need to be versioned file into the staging area;

    3, will be submitted to the staging area file git repository.

    Therefore, git managed file has three states: modified (modified), has been staging (staged), has been submitted (committed)

  3. Graphic tutorials

Personally think that the principle of Git compared to other versions of the controller or complex, there is a more intuitive graphical tutorial:

git basic operation of the operation (operation command the bash git)

  1. Create a working directory with commonly used commands

    Working directory (WorkSpace) in general is that you want to manage your Git help file folders, can be a directory of your project, it can be an empty directory, it is recommended not to have Chinese.

    Just remember the next chart daily use six commands:

  2. Get GIT repository

    Way to create a local repository in two ways: one is to create a new warehouse, the other is cloning a remote repository.

    • Creating a new warehouse

      Enter git bash create a new repository:

      # 在当前目录新建一个Git代码库
      $ git init

      git repositories .git directory you just created as follows:

      After the execution can be seen only in more of a project directory .git directory, all the information about the version, etc. are in this directory.

      Of course, if you use the following command, you can put together to create a complete catalog and warehouse:

      # 新建一个目录,将其初始化为Git代码库
      $ git init [project-name]

    • Clone remote repository

      You can clone a remote repository from github and gitee.

      Fastjson code as cloud remote repository Address: https://gitee.com/wenshao/fastjson

      In git bash command line clone git https://gitee.com/wenshao/fastjson code on the remote repository can be pulled down .

  3. git file operation

Version control is the version control for files that you want to modify the file, submitted to other operations, you must first know what the current state of the file, or may be submitted now do not want the documents submitted, or not submitted the documents to be submitted on. GIT does not care about the specific differences between the two versions of the file, but about the whole file if there is a change, if the file is changed, the new version of the file is generated when you add a snapshot of submission, and the method to determine whether to change the whole document is to use checksum algorithm SHA-1 file.

3.1 File 4 states

  • Untracked : untracked, this file in the folder, but has not added to the git repository, version control is not involved through. git addStatus changes Staged.

  • Unmodify : file has been put in storage, unmodified, that is a snapshot of the contents of the file and the file repository folder exactly this type of files in two places, if it is modified and changed. ModifiedIf you are using. git rmOut of the repository, It has become the Untrackedfile

  • Modified : file has been modified, to modify only, and no other operations in this document also has two places through. git addEnter temporary stagedstate, using git checkoutthe modified discarded, returned to the unmodifystate, the git checkouti.e. removed from the library file, the covering current modification

  • Staged :. BSR execution git commitwill modify the synchronization to the library, then the library and local files and become consistent file Unmodify. Execution state git reset HEAD filenamecanceled the temporary file statusModified

 3.2 查看文件的状态

 ```
 # 查看所有文件的状态
 git status
 # 查看指定文件的状态
 git status filename
 ```

 3.3  添加文件与目录

 工作区(Working Directory)就是你在电脑里能看到的目录。

 版本库(Repository)工作区有一个隐藏目录`.git`,这个不算工作区,而是Git的版本库。

 Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支`master`,以及指向`master`的一个指针叫`HEAD`。

 将untracked状态的文件添加到暂存区,语法格式如下:

 ```linux
 # 添加指定文件到暂存区
 $ git add [file1] [file2] ...
 
 # 添加指定目录到暂存区,包括子目录
 $ git add [dir]
 
 # 添加当前目录的所有文件到暂存区
 $ git add .
 ```

 3.4 移除文件与目录

![](https://img2018.cnblogs.com/blog/1389886/201911/1389886-20191116145850119-1894528153.png)

 当执行如下命令时,会直接从暂存区删除文件,工作区则不做出改变

 ```linux
 #直接从暂存区删除文件,工作区则不做出改变
 git rm --cached <file>
 ```

 通过重写目录树移除add文件:

 ```
 #如果已经用add 命令把文件加入stage了,就先需要从stage中撤销
 git reset HEAD <file>...
 ```

 当执行 “git reset HEAD” 命令时,暂存区的目录树会被重写,被 master 分支指向的目录树所替换,但是工作区不受影响。

 移除所有未跟踪文件

 ```
 #移除所有未跟踪文件
 #一般会加上参数-df,-d表示包含目录,-f表示强制清除。
 git clean [options] 
 #只从stage中删除,保留物理文件
 git rm --cached readme.txt 
 
 #不但从stage中删除,同时删除物理文件
 git rm readme.txt 
 
 #把a.txt改名为b.txt
 git mv a.txt b.txt 
 ```

When performing a commit (git commit), the directory tree in the temporary area writing to the repository (object library) in, master branch will be updated accordingly. Temporary directory tree area ie master point of the directory tree is submitted.

When performing "git reset HEAD" command, tree staging area will be rewritten, is replaced by the master branch points of the tree, but the work area is not affected.

When performing "git rm -cached "Command will delete files directly from the staging area, the work area is not to make a change.

When performing "git checkout." Or "git checkout - "Command will be replaced with all or specified file temporary storage area to work area. This operation is very dangerous, it clears the workspace changes are not added to the staging area.

When performing "git checkout HEAD." Or "git checkout HEAD "Command will be replaced with all the master branch HEAD points or part of the file as well as the staging area and workspace files. This command is very dangerous, because the changes will not only clear the work area uncommitted, also clears changes to the staging area in uncommitted.

File Compare 3.5

git diff [filename]: the workspace file and the staging area to compare
git diff [local library version of history] [filename]: file libraries and local history workspace compare
without a file Compare multiple file name

3.6 files checked out

If the file already exists in the warehouse f4.txt, in the work area to f4 modified, can be used if you want to undo checkout, check out coverage

The detection command git git checkout is one of the most commonly used commands, but also a very dangerous command, because this command will rewrite the work area

grammar:

# Use a
git checkout [-q] [ ] [--] ...
# Usage of Two
git checkout [ ]
# Uses three
git checkout [-m] [[-b ] - orphan] ] [ ]

Git branch Checkout $
# detection branch branch. To complete the three step diagrams updated to point to the branch HEAD branch, and updates the tree and branch points of temporary storage area and work area.

Checkout git $
# summary show differences workspace, the staging area and HEAD.

Checkout the HEAD git $
# ibid.

Checkout git $ - filename
# file filename with a scratch pad to cover the filename file workspace. Equivalent to cancel since the last git add filename (if performed) local modifications.

Checkout Branch git $ - filename
# HEAD points to maintain unchanged. With branch pointed to submit filename replace the respective files staging area and workspace. Note that the file filename will be staging and work areas directly covered.

$ Git checkout -.. Or writing checkout git
# Note git checkout command parameters of a point ( "."). This command is the most dangerous! # Will cancel all local modifications (with respect to the staging area). The equivalent of direct overwriting the local file with all the files in the temporary area, do not give the user any opportunity to confirm!

Git Checkout commit_id $ - file_name
# if not commit_id, then git checkout - file_name represents restore files to a local repository up to date.

3.7 The author

Just add files or directories via git add to the index buffer, it can be achieved using commit to submit documents to the local staging area warehouse.

# 提交暂存区到仓库区
$ git commit -m [message]

# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区,跳过了add,对新文件无效
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...

Submit revised

If we find after submitting documents make mistakes, or just want to modify commit, then you can make changes to the file, the modified file via "git add" to add to the staging area, and then execute the following command:

#修订提交
git commit --amend

Undo submit (commit)

The principle is to give up the work area and the index changes, while the former HEAD pointer to a commit object

#撤销上一次的提交
git reset --hard HEAD~1

To view the logs submitted by git log, you can also specify submitted directly or serial numbers

撤销提交
git revert <commit-id>
这条命令会把指定的提交的所有修改回滚,并同时生成一个新的提交。

3.8 log

View commit log can use git log command syntax is as follows:

#查看提交日志
git log [<options>] [<revision range>] [[\--] <path>…?]

"Git log --graph" to graphically display the relationship between history submitted, which can easily view the information submitted by the branch history, of course, the console is painted with the character graphics.

"Git log -1" indicates a display line.

You can view your command history using entered under bash:

View all branches of 3.9 log

"Git reflog" will record all updates records of all branches of the warehouse, including the dismissed updated.

3.10 View File List

Use git ls-files can check the status of the instruction list file, the following format:

#查看指定状态的文件
git ls-files [-z] [-t] [-v] (--[cached|deleted|others|ignored|stage|unmerged|killed|modified])* (-[c|d|o|i|s|u|k|m])*

3.11 undo the update

  • Undo staging area update

    Use "git add" to update submitted to the staging area. Then output "git status" of tips we can "git reset HEAD ... "to update the temporary area to be removed in WorkSpace

  • Undo the local repository update

    Use git log view commit logs, undo submitted in two ways: using pointers and using HEAD commit id

    In Git, there is a pointer to the latest HEAD branch submitted in the current. The current version, we use the "HEAD ^", then money and then you can use a version of "HEAD ^^", if you want to roll back to an earlier submission, you can use the "HEAD ~ n". (I.e., HEAD ^ = HEAD ~ 1, HEAD ^^ = HEAD ~ 2).

    git reset --hard HEAD^
    git reset --hard HEAD~1
    git reset --59cf9334cf957535cb328f22a1579b84db0911e5(commit id)

3.12 Delete file

  • Delete untracked files

    If the file still does not track status, delete the file will be enough, bash use rm can delete files, rm filename

  • Delete submission

    git rm -f filename -f force delete, delete the physical, and delete files and workspaces in the staging area

  • Undelete

    git checkout filename

  1. Branch

Branch in the GIT relatively difficult to

Branch is inside the parallel universe of science fiction movies, when you are at the computer trying to learn Git, and the other another parallel universe you are trying to learn SVN.

If two parallel universes interfere with each other, it also lacks the influence to you right now. However, at some point in time, two parallel universes merge as a result, you not only learn Git learned to SVN!

Branch of what use is it in practice? Suppose you ready to develop a new feature, but two weeks to complete, the first week you wrote 50% of the code, if submitted at once, because the code is not finished, not a complete code base will lead to others not to work. If the code is all finished and so once again submit, and there is great risk of loss progress every day.

Now, with a branch, do not be afraid. You have created a branch of your own, others can not see, but also continue to work on the original branch, while you work on your own branch, would like to submit submitted until after the completion of the development, and then to one-time merger the original branch, so that safe and do not affect the work of others.

Git branch speed very quickly.

Up to now, only one timeline, in Git, this branch is called the main branch, that is, the master branch. HEAD strictly speaking is not directed to submit, but point to the master, master is the point of submission, so, pointing to the current branch HEAD

Branch frequently used commands:

# 列出所有本地分支
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支和远程分支
$ git branch -a

# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]

# 新建一个分支,并切换到该分支
$ git checkout -b [branch]

# 新建一个分支,指向指定commit
$ git branch [branch] [commit]

# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch] [remote-branch]

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 切换到上一个分支
$ git checkout -

# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

The combined branch (git merge):

过程描述:
(1) 创建一个远程仓库;
(2) 本地拉取远程仓库的代码到master分支,并在本地创建develop分支用于开发.
(3) 将本地develop分支修改git commit到本地的master分支;
(4) 如果此时远程仓库的代码进行了修改,而我们本地的master未同步远程仓库就会出现代码冲突问题,因为本地的master和远程的master都进行了修改,此时就应该进行合并分支;
(5) 将远程仓库的最新代码拉取到本地master,本地master进行git merge操作,解决代码冲突问题,合并分支,进行提交到远程仓库;

Guess you like

Origin www.cnblogs.com/startway/p/11871875.html