Git tutorial (Four) used on its own scene when Git

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/young2415/article/details/89528421

First, file operations

Adding together Git repository being managed files to the staging area

git add -u

Directly to the contents of the workspace to submit Version History Library:

git commit -am "commit message"

For the warehouse rename the file:

git mv 原文件名 新文件名

Delete Files:

git rm 文件名

Second, look for different

2.1 HEAD and compare different staging area file

git diff --cached #比较全部的文件
git diff --cached -- PATH #这里PATH需要换成你想比较的路径或者路径下的文件

2.2 Comparison of different work areas and staging area file

git diff #比较所有文件的不同
git diff -- 文件名 #比较指定文件的不同,可以是一个文件,也可以是多个文件
#比如:
git diff -- style.css #比较工作区和暂存区中的style.css这个文件的不同
git diff -- style.css readme.md #比较工作区和暂存区中的style.css和readme.md这两个文件的不同

2.3 specify the file to see the differences of different commit

2.3.1 Comparison of two different branches of all files

git diff 分支1 分支2

2.3.2 Comparison of two different branches of the specified file

git diff 分支1 分支2 -- 文件名

The file name can be one or may be plural.

2.3.3 compare any two different submissions

git diff 哈希值 哈希值 #比较任意两次提交的不同
git diff HEAD HEAD^1 #比较当前头指针指向的提交与其父节点的不同
git diff HEAD HEAD^ #与上一条命令等价
git diff head head~1 #与上一条命令等价
git diff HEAD HEAD^1^1 #比较当前头指针指向的提交与其父节点的父节点的不同
git diff HEAD HEAD^^ #与上一条命令等价
git diff head head~2 #与上一条命令等价

Third, the recovery

3.1 workspace file restored to the staging area and the same

git checkout -- 文件名

Files 3.2 staging area and restored to the same HEAD

All the files in the temporary area and restored to the same HEAD:

git reset HEAD

The part of the file in the staging area and restored to the same HEAD:

git reset HEAD -- 文件名 #文件名可以是一个,也可以是多个,多个文件名用空格分开

Fourth, the elimination of several recent submission

git reset --hard commit_hash

Here Insert Picture Description
For example, if you want to return temp branch that commit red box turns out, can be written:

git checkout temp #首先确保当前分支是temp
git reset --hard 3eab3c1

At this time, HEAD, staging area, the contents of the warehouse turned into a work area that commit Add the contents of the test.

Fifth, the development of temporary stopper urgent task of how to deal with?

If you are doing development work area has uncommitted changes, suddenly came an urgent task that requires repair a bug, then how should we do?

Of course the best is to write code before being put aside, and other finished repair bug, made after the submission, before the code then get it back, continue to develop.

git stash It can help us to achieve such an operation.
Here Insert Picture Description

git stash #将当前工作区的内容放到stash里面
git stash list #查看stash里面的内容
git stash apply #把stash里面的内容恢复到工作区(保留stash里面的内容)
git stash pop #把stash里面的内容恢复到工作区(同时删除stash里面的内容)

Here Insert Picture Description

stash is actually a stack structure, each execution git stashoperations are equivalent to a stack, when using git stash applyor git stash popwhen, in the stash will be to restore the contents of the work area to the top of the stack. It is not difficult to understand why this git stash popwill delete the contents stash inside while the recovery of the (because pop is the stack).

Sixth, Git does not need to specify a file management

In development, there are some files that we might not wish to be included in the management Git, such as links to documents produced in the code compilation process. Then you can configure the ".gitignore" file.

".Gitignore" project file to be placed in the root directory. Matching rules are as follows:

*.obj #不管理后缀名是obj的文件
build #不管理build目录下的文件,如果有名为build的文件,也不纳入管理。
build/ #不管理build目录下的文件

For projects in different languages may have different configurations, specific reference GitHub related to this project, which lists configuration ".gitignore" documents in most languages of the project: https://github.com/ github / gitignore

Seven, to remove unwanted branches

git branch -d 分支名 #删除分支,但是如果该分支没有被合并,那么使用这条命令无法删除该分支
git branch -D 分支吗 #强制删除分支。如果你确定删除该分支对整个项目没有影响,可以使用这条命令强制删除该分支

Eight, modify commit the message

8.1 modify the message of the latest commit

First, you must first switch to that branch commit you want to modify, and then execute the following command:

git commit --amend

Then enter the vim editor mode, the following interface:
Here Insert Picture Description
You need to understand the basic operation of vim. It can save out after you modify.

8.2 modify the old commit message of

git rebase -i 你要修改的commit的父节点的哈希值

After the above-described command input, there will be the following interface:
Here Insert Picture Description

The pick into the first row r, save and exit. Then will appear the following interface:
Here Insert Picture Description
the message finished modifying save out.

Guess you like

Origin blog.csdn.net/young2415/article/details/89528421