Home >Development Tools >git >Case plus analysis! Let you summarize common Git operation commands
This article brings you relevant knowledge about Git. It mainly summarizes the commonly used operating commands of git through cases and analysis. Git is an open source distributed version control system. Used to handle any project, small or large, quickly and efficiently. I hope it will be helpful to everyone.
Recommended study: "Git Tutorial"
Git is an open source distributed version control system, used to handle any small or large project quickly and efficiently. It is currently the most popular version management tool.
SVN is a centralized version control system, and the version library is centralized on the central server , and when working, you use your own computer, so you first need to get the latest version from the central server, and then work. After finishing, you need to push the work you have done to the central server. The centralized version control system must be connected to the Internet to work. If it is on a local area network, it is okay, the bandwidth is large enough, and the speed is fast enough. If it is on the Internet, if the network speed is slow, it will be confusing.
Git is a distributed version control system, so it does not have a central server. Everyone's computer is a complete version library, so you don't need to be connected to the Internet when working. Because the versions are all on your own computer. Since everyone's computer has a complete version library, how can multiple people collaborate? For example, if you have modified file A on your computer, and someone else has also modified file A on your computer, you only need to push your modifications to each other, and you can see each other's modifications.
Git was first developed on Linux. For a long time, Git could only run on Linux and Unix systems. . However, slowly someone ported it to Windows. Now, Git can run normally on the major platforms of Linux, Unix, Mac and Windows.
To use Git
, the first step is of course to install Git
. Download it from https://git-for-windows.github.io (if the Internet speed is slow, please move to the domestic mirror), and then install it according to the default options.
After the download is complete, open it for installation (configure as shown below).
Then you just need to wait quietly for the installation to complete, After completion, right-click on the desktop or a blank location in any folder. When the two menu bars shown in the figure below appear, the installation is successful.
After installation, a command box will pop up, we still need to do the last step In one step, set the identifier. Since git is a distributed management tool, you need to set the user name and email address as identification. Just enter the following code in the pop-up box.
git config --global user.name "Your Name"git config --global user.email "email@example.com"
Note: git config --global parameter. With this parameter, it means that all Git repositories on your machine will use this configuration. Of course, you You can also specify different user names and emails for a certain warehouse.
Before operating Git You have to create a Git repository first and create an empty folder in the location you need. Then enter the folder, right-click on the blank space, and click Git Bash Here to perform Git operations on the current folder.
git init
cd:进入某个目录 mkdir:创建一个文件 pwd:显示当前的目录路径 鼠标选中就是复制,粘贴可以右键粘贴,也可以用使用快捷键:Shift+INS
git add a.txt
git add .
git commit -m "双引号里面是注释——你的提交说明"
原因:commit
可以一次提交很多文件,所以你可以多次add
不同的文件
例如:
git add file1.txt #单个添加文件到暂存区git add file2.txt file3.txt #多个添加文件到暂存区git add . #添加当前文件夹下所有文件到暂存区git commit -m "add 3 files." #提交所有暂存区的文件
使用下面命令检查当前文件状态
git status
结果:没有需要提交的文件了;
创建一个新的文件 b.txt,内容为 bbb,再来检查文件状态
结果:存在未跟踪文件没有添加到暂存区和提交到版本库;
添加 b.txt 到暂存区之后,再来检查文件状态
结果:暂存区中有一个新的 b.txt 文件没有添加到版本库中;
提交 b.txt 到版本库之后,然后把 b.txt 内容从 bbb 修改为 bbba,再来检查文件状态
结果:被改变的文件 b.txt 没有添加到暂存区且没有提交
git add b.txtgit commit -m "提交修改的 b.txt 文件"
git log
注意:使用上面命令信息多的话会进入 log 模式,想要退出,在英文输入法的前提下按 q 就可以退出了
git log --pretty=oneline
git log -1
git log #查看全部历史提交记录git log --pretty=oneline #精简显示所有历史提交记录git reflog #可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)git log -p #查看全部提交历史并展示每次修改的内容git log -2 #查看最近2次提交历史(注意:后面的数字是可以自定义的,也就是说,这种写法是 git log -n 的体现)git log -p -2 #查看最近2次提交历史并展示修改的内容git log --stat #查看提交历史,并展示摘要内容(摘要会列出修改的文件以及每个文件中修改了多少行)
Git
必须知道当前版本是哪个版本,在Git
中,用HEAD
表示当前版本,上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
。git reset --hard HEAD^
git reset --hard
git reset --hard 6ec9373d22d1a869b67681a39dc04df34292133b
结果:从查看的历史版本结果可以看出我们回退到了 “提交 a.txt” 版本
git reflog
结果:reflog 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)
git reset --hard 7a42e7b
结果:历史版本信息可以看出我们已经回退到了我们想要的版本
例如:我们把 b.txt 文件内容修改为为 bbbaaa,然后用下面代码查看,可以看出我们修改了什么
git diff # 查看不同版本之间的文件差异
推荐使用:第一次修改 -> git add -> 第二次修改 -> git add -> git commit
注意:建议每次 commit 之前先检查是否有文件没有被 add
git checkout -- filename
git checkout -- filename
可以丢弃工作区的修改:– 后面是一个空格
命令 git checkout -- readme.txt
意思就是,把 readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
一:readme.txt
自修改后还没有被放到暂存区(git add
),现在,撤销修改就回到和版本库一模一样的状态;
二:readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次 git commit
或 git add
时的状态。
注意:git checkout -- file
命令中的 --
很重要,没有 --
,就变成了**“切换到另一个分支”**的命令,我们在后面的分支管理中会再次遇到 git checkout
命令
通常直接在文件管理器中把没用的文件删了,或者用rm
命令删除,例如:删除 b.txt
git rm b.txt
删除步骤
git rm b.txt --cached
b.txt 处于未跟踪状态,也就是从暂存区删除。
特别说明:处于未跟踪状态只是没有存在于暂存区,历史提交记录中的记录依然存在。
所谓的暂存区仅仅是.git目录下的一个index文件罢了,这也是为了什么被称为index(索引),当删除暂存区内容的时候,其实就是删除index文件中的内容,.git/objects目录中的内容不会被删除。
rm .git/index
git branch #查看分支 git branch <name> #创建分支git checkout <name> #切换分支git checkout -b <name> #创建 + 切换分支git merge <name> #将某分支合并到当前分支git branch -d <name> #删除分支</name></name></name></name></name>
命令 | 作用 |
---|---|
git config --global user.name “Your Name” | 设置用户名 |
git config --global user.email “email@example.com” | 设置邮箱 |
2. 初始化命令
命令 | 作用 |
---|---|
git init | 初始化 git,创建 .git 文件 |
3. 常用命令
命令 | 作用 |
---|---|
cd | 进入某个目录 |
mkdir | 创建一个文件 |
pwd | 显示当前的目录路径 |
鼠标选中就是复制 | 复制 |
直接鼠标右键粘贴 / 快捷键:Shift+INS | 粘贴 |
4. 添加到暂存区
命令 | 作用 |
---|---|
git add a.txt | 添加 a.txt 到暂存区 |
git add . | 添加当前根目录下的所有文件到暂存区 |
git commit -m “双引号里面是注释——你的提交说明” | 把暂存区的文件提交到版本库(一次全部提交) |
5. 查看文件状态
命令 | 作用 |
---|---|
git status | 查看文件状态(检查是否有未提交文件) |
6. View submission history
Command | Function |
---|---|
git log | View all historical commit records |
git log --pretty=oneline | Concise display of all historical commit records |
git reflog | You can view all operation records of all branches (including Deleted commit records and reset operations) |
git log -p | View all submission history and display the contents of each modification |
git log -2 | View the last 2 commit history (note: the following numbers can be customized, that is, this way of writing is git log The embodiment of -n) |
git log -p -2 | View the last 2 submission history and display the modified content |
git log --stat | View the commit history and display the summary content (the summary will list the modified files and how many lines were modified in each file) |
7. Version rollback and recovery
Function | |
---|---|
git reset --hard HEAD^ | Go back to the previous version|
git reset --hard HEAD~N (not -, it is a wavy line) | Return to the previous N versions|
git reset --hard | Roll back to the specified version|
git reflog and git reset -- hard | to restore the rolled back version
8. View the differences Version differences
Function | |
---|---|
git diff | View file differences between different versions
9. Undo changes
Function | |
---|---|
1: | readme.txt has not been placed in the temporary storage area (git add ) since the modification. Now, undo the modification and return to the The repository is in exactly the same state; 2: readme.txthas been added to the staging area and modified. Now, undoing the modification will return to the state after being added to the staging area.
|
effect | |
---|---|
rm Delete the file | rm will be automatically added to the temporary cache after deletion area, the manual add command is omittedFinally commit, the file is deleted |
Delete a single file in the cache area, b.txt is in an untracked state, that is, delete | |
from the temporary storage area Clear all cache |
View branch | |
Create branch | |
Switch branch | |
Create and switch to a new branch | |
Merge a branch to the current branch | |
Delete branch |
The above is the detailed content of Case plus analysis! Let you summarize common Git operation commands. For more information, please follow other related articles on the PHP Chinese website!