博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git 常用基础命令学习
阅读量:4229 次
发布时间:2019-05-26

本文共 55068 字,大约阅读时间需要 183 分钟。

参考链接: 

以前用 IDE 的时候只知道一个劲的 commit,现在该好好回想一下 Git 那些最基础的命令了。

Git 安装最新版本

系统:centOS7

就我知道的,在 centos 下可以只用 yum install git 来安装 git,但是这样往往安装的不是最新版本的 git,然后有些新功能可能会用不了。比如说 git -C <path> 之类。所以,新版本最好的办法还是源码安装。

1. 下载安装包

[root@master packages]# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.28.0.tar.gz

2. 解压安装包

[root@master packages]# tar -zxvf git-2.28.0.tar.gz

3. 设置安装路径

[root@master packages]# cd git-2.28.0[root@master packages]# ./configure --prefix=/usr/local/git

4. 编译安装(时间稍长)

[root@master git-2.28.0]# make && make install

5. 设置软连接 

[root@master git-2.28.0]# ln -s /usr/local/git/bin/git /usr/bin/git

6. 查看版本

[root@master git-2.28.0]# git versiongit version 2.28.0

账号配置

git config # 查看本机是否配置了个人信息 git config --global user.name "……" # 定义全局的用户名 git config --global user.email "……" # 定义全局的邮件地址 git config --list # 查看配置信息

Git 的三种状态

已提交(commited),已修改(modified)和已暂存(staged)

1. 已提交(commited):表示数据已经安全的保存在本地数据库中。

2. 已修改(modified):已经修改了文件,但是还没有保存到本地数据库中。

3. 已暂存(staged):表示对一个已经修改的文件的当前版本做了标记,使其包含在了下次的提交中。

基本的 Git 工作流程

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

2. 暂存文件,将文件的快照放入暂存区。(staged)

3. 提交更新。找到暂存区域的文件,将快照永久性的存储到Git仓库。(commited)

git init : 初始化Git仓库。 该命令会在你当前所在的目录创建一个.git文件夹git add . :将当前所在目录中的所有文件加入到暂存区git add README.md : 将README.md文件加入到暂存区(让git去跟踪它)git commit -m "Initial commit" : 将暂存区的文件提交到Git仓库git clone https://github.com/2392863668/GitTest.git

工作目录中的文件主要有两种状态:已追踪(tracked)和未追踪(untracked)

1.已追踪(tracked):已追踪的文件指的是已经被纳入到版本控制的文件。已追踪的这些文件又可能处于已修改(modified),未修改(unmodified),已暂存(staged)的状态。

当你克隆了一个远程仓库到本地的时候,工作目录中所有的文件都是已追踪的文件,而且都是处于未修改的状态。

2.未追踪(untracked):未追踪的文件表示不被纳入版本控制的文件,除了已追踪的文件之外所有的文件都是未追踪文件。例如当你克隆了一个项目到本地之后,你在本地的项目中新添加了一个文件,那么这个文件就是未追踪的状态。

1.未追踪的文件(Untracked)在 git add 之后会变成已暂存(staged)状态。2.未修改的文件(Unmodified)在被修改编辑之后会变成已修改(modified)状态。3.已修改(modified)的文件在 git add之后会变成已暂存(staged)状态。4.已暂存的文件(staged)在 git commit 之后会变成未修改(Unmodified)状态。5.未修改的文件(Unmodified)在 git rm 之后会变成未跟踪(Untracked)状态(注意git rm会删除掉指定的文件)。

常用命令 

git status

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean

git push -f origin master

强制推送

git remote add upstream <url>

git remote rm upstream

git diff

 1. git diff :查看尚未暂存的文件更新了哪些部分。

 2. git diff --staged :查看已暂存的文件更新了哪些部分。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ vim c.txt  # 修改文件 c.txt ,新行添加内容 hello world (c.txt 已 tracked)$ git diffdiff --git a/c.txt b/c.txtindex 7209f0a..c068359 100644--- a/c.txt+++ b/c.txt@@ -1,3 +1,4 @@ rebase test add one line in c.txt by user B add one line in c.txt by user B again+hello worldAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git diff -- staged--------------------------------------------------------------------------Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git add c.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git diff$ git diff --stageddiff --git a/c.txt b/c.txtindex 7209f0a..c068359 100644--- a/c.txt+++ b/c.txt@@ -1,3 +1,4 @@ rebase test add one line in c.txt by user B add one line in c.txt by user B again+hello world

git add 

当在工作目录新建一个文件,运行 git status ,会显示这个文件是untracked状态。这时候需要使用 git add  将这个文件加入暂存区,也就是告诉Git需要去跟踪这个文件。git add 使用文件或者目录的路径作为参数;如果参数是目录的路径,那么将该目录下所有的文件加入暂存区,Git将会跟踪这个目录下面的所有文件。

如果文件是tracked状态,当修改了Git追踪的一个文件之后,运行 git status 会看到 Changes not staged for commit ,说明已跟踪的文件发生了变化但是还没有被加入到暂存区,这时候需要运行 git add 将该文件放入暂存区。

.gitignore

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ touch e.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is up to date with 'origin/master'.Untracked files:  (use "git add 
..." to include in what will be committed) e.txtnothing added to commit but untracked files present (use "git add" to track)Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ touch .gitignoreAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ vi .gitignore # 在 .gitignore 添加以下两行内容.gitignoree.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git status # e.txt 不再显示为 Untracked filesOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean

git blame

查看最新文件的具体行是由谁最后修改的(对于要找应该是谁来背这个锅来说很方便)。

[root@master GitTest]# git blame hello.txtb8bbe18a (2392863668 2018-11-28 08:57:15 +0800 1) hello world640fd3c8 (2392863668 2020-07-29 23:49:11 +0800 2) hello world3f206129 (looking    2020-08-20 23:02:46 +0800 3) hello world. I am Looking50a084c8 (looking    2020-08-31 21:16:51 +0800 4) nice72017cd6 (looking    2020-08-31 21:59:19 +0800 5) nice to meet you too

git mv

可以使用 git mv 来将文件重命名(在被 tracked 的情况下),会将 oldFile 重新命名为 newFile 。

$ git mv 
这条命令会:1.将文件改名;2.将改名之后的文件add进暂存区。等价于下面三条命令。$ mv oldFile newFile$ git rm oldFile$ git add newFile
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git add test.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git mv test.txt ./test/test.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD 
..." to unstage) renamed: test.txt -> test/test.txt

git clone

1. git clone

[looking@master workspace]$ git clone https://github.com/2392863668/GitTest.gitCloning into 'GitTest'...remote: Enumerating objects: 48, done.remote: Counting objects: 100% (48/48), done.remote: Compressing objects: 100% (34/34), done.remote: Total 269 (delta 18), reused 34 (delta 10), pack-reused 221Receiving objects: 100% (269/269), 1.27 MiB | 7.00 KiB/s, done.Resolving deltas: 100% (102/102), done.[looking@master workspace]$ cd GitTest/[looking@master GitTest]$ lsb.txt  c.txt  hello.txt  looking.txt  myGit.txt  newBranch.txt  readme.txt  test  world.txt

2. git clone --bare

裸克隆,这个仓库只保存 git 历史提交的版本信息,而不允许用户在上面进行各种 git commit 操作。

[looking@master GitTest]# git clone --bare git@github.com:2392863668/GitTest.gitCloning into bare repository 'GitTest.git'...remote: Enumerating objects: 48, done.remote: Counting objects: 100% (48/48), done.remote: Compressing objects: 100% (34/34), done.remote: Total 269 (delta 18), reused 34 (delta 10), pack-reused 221Receiving objects: 100% (269/269), 1.27 MiB | 8.00 KiB/s, done.Resolving deltas: 100% (102/102), done.[root@master GitTest]# cd GitTest.git/[root@master GitTest.git]# lsbranches  config  description  HEAD  hooks  info  objects  packed-refs  refs

3. git clone --mirror

镜像克隆,与裸克隆一样,镜像克隆包括所有远程分支和标记,但是每次拉取时所有本地引用都将被覆盖,因此它始终与原始存储库相同。

[looking@master workspace]$ git clone --mirror https://github.com/2392863668/GitTest.gitCloning into bare repository 'GitTest.git'...remote: Enumerating objects: 48, done.remote: Counting objects: 100% (48/48), done.remote: Compressing objects: 100% (34/34), done.remote: Total 269 (delta 18), reused 34 (delta 10), pack-reused 221Receiving objects: 100% (269/269), 1.27 MiB | 5.00 KiB/s, done.Resolving deltas: 100% (102/102), done.[looking@master workspace]$ cd GitTest.git/[looking@master GitTest.git]$ lsbranches  config  description  HEAD  hooks  info  objects  packed-refs  refs

git stash

能将所有未提交的修改(工作区和暂存区,也即包括 git add 和没 git add 的文件)保存至堆栈中,用于后续恢复当前工作目录。

1. git stash

[root@master GitTest]# git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git restore --staged 
..." to unstage) deleted: b.txt modified: hello.txtChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: c.txt[root@master GitTest]# git stashSaved working directory and index state WIP on master: 2017dee add newline in c.txt[root@master GitTest]# git stash clear[root@master GitTest]# git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)nothing to commit, working tree clean

2. git stash save "message"

执行存储时,添加备注,方便查找,只有 git stash 也要可以的,但查找时不方便识别。

[root@master GitTest]# vim c.txt[root@master GitTest]# git statusOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: c.txtno changes added to commit (use "git add" and/or "git commit -a")[root@master GitTest]# git stash save "save modification for c.txt"Saved working directory and index state On master: save modification for c.txt[root@master GitTest]# git statusOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean

3. git stash list

查看 stash了哪些存储。

[root@master GitTest]# git stash liststash@{0}: On master: save modification for c.txtstash@{1}: WIP on master: e9a2ce2 add new line in d.txt

4. git stash show

显示做了哪些改动,默认 show 第一个存储。如果要显示其他存贮,后面加 stash@{$num},比如第二个 git stash show stash@{1}。

[root@master GitTest]# git stash show c.txt | 1 + 1 file changed, 1 insertion(+)[root@master GitTest]# git stash show stash@{0} c.txt | 1 + 1 file changed, 1 insertion(+)[root@master GitTest]# git stash show stash@{1} d.txt | 1 + 1 file changed, 1 insertion(+)

5. git stash show -p 

显示第一个存储的改动,比直接 git stash show 显示的信息更多,会显示修改变动的代码行。

[root@master GitTest]# git stash show -pdiff --git a/c.txt b/c.txtindex c068359..16eb67d 100644--- a/c.txt+++ b/c.txt@@ -2,3 +2,4 @@ rebase test add one line in c.txt by user B add one line in c.txt by user B again hello world+git stash test

6. git stash apply

应用某个存储,但不会把存储从存储列表中删除,默认使用第一个存储,即stash@{0},如果要使用其他的存储,git stash apply stash@{$num} , 比如第二个:git stash apply stash@{1} 。

[root@master GitTest]# git stash applyOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: c.txtno changes added to commit (use "git add" and/or "git commit -a")[root@master GitTest]# git stash liststash@{0}: On master: save modification for c.txtstash@{1}: WIP on master: e9a2ce2 add new line in d.txt

7. git stash pop

命令恢复之前缓存的工作目录,将缓存堆栈中的对应 stash 删除,并将对应修改应用到当前的工作目录下,默认为第一个 stash,即 stash@{0},如果要应用并删除其他 stash,命令:git stash pop stash@{$num} ,比如应用并删除第二个:git stash pop stash@{1}。

[root@master GitTest]# git stash liststash@{0}: WIP on master: aec1d4f Merge pull request #1 from 15249089066/masterstash@{1}: On master: save modification for c.txtstash@{2}: WIP on master: e9a2ce2 add new line in d.txt[root@master GitTest]# git stash popOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: c.txtno changes added to commit (use "git add" and/or "git commit -a")Dropped refs/stash@{0} (bed2edc094069409b106f301869a8ef0421ef0ad)[root@master GitTest]# git stash liststash@{0}: On master: save modification for c.txtstash@{1}: WIP on master: e9a2ce2 add new line in d.txt

8. git stash drop

丢弃 stash@{$num} 存储,从列表中删除这个存储。默认删除第一个存储,即 stash@{0}。

[root@master GitTest]# git stash liststash@{0}: On master: save modification for c.txtstash@{1}: WIP on master: e9a2ce2 add new line in d.txt[root@master GitTest]# git stash dropDropped refs/stash@{0} (4334160546f5e3a12db85ecafc4cd9cbb9779ff9)[root@master GitTest]# git stash liststash@{0}: WIP on master: e9a2ce2 add new line in d.txt

9. git stash clear

删除所有缓存的 stash。

[root@master GitTest]# git stash liststash@{0}: On master: save modification for c.txtstash@{1}: WIP on master: e9a2ce2 add new line in d.txt[root@master GitTest]# git stash clear[root@master GitTest]# git stash list[root@master GitTest]#

git restore

1. git restore --staged <filename>

将 git add 的文件变成 git add 之前的状态。

[looking@master GitTest]$ vim b.txt[looking@master GitTest]$ git add b.txt[looking@master GitTest]$ git statusOn branch masterYour branch is up to date with 'origin/master'.Changes to be committed:  (use "git restore --staged 
..." to unstage) modified: b.txt[looking@master GitTest]$ git restore --staged b.txt[looking@master GitTest]$ git statusOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit: (use "git add
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: b.txtno changes added to commit (use "git add" and/or "git commit -a")

2. git restore -s HEAD~1 <filename>

将 git commit 的文件变为 git add 之前的状态。

[looking@master GitTest]$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)nothing to commit, working tree clean[looking@master GitTest]$ git restore -s HEAD~1 hello.txt[looking@master GitTest]$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")

git commit

1. git commit

在暂存区准备完毕之后,就可以commit了。注意:在提交之前一定要确认还有什么没有修改过的或新建的文件还没有 git add 过,否则commit的时候不会记录这些还没暂存起来的变化,这些修改过的文件只会保留在本地磁盘(也就是工作区)。在每次提交之前,可以使用 git status 查看一下,文件是不是已经都暂存起来了。

1.  git commit 如果不指定-m参数,那么git会打开一个编辑器,让你输入提交信息。

2. git commit -m <info>  指定-m参数,可以在后面直接指定提交信息。(推荐使用)

3. git commit -a -m <info>有时候,在修改某一个已经被 tracked 的文件之后,可以直接使用 git commit -a -m "info" 跳过add步骤而将这个文件提交。注意:这条命令会将已经被 tracked 的文件加入到暂存区,然后进行提交;对于 untracked 的文件(例如新添加的文件),在修改之后是不能使用这条命令直接进行提交的。

commit记录的是放在暂存区域的快照,每一次提交操作都是对项目做一次快照,以后可以回到这个状态,或者进行比较。

$ git commit -m "modify c.txt"[master 70f19e6] modify c.txt 1 file changed, 1 insertion(+)Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git showcommit 70f19e6d0788ad7fca07b129a8615084fe08f1e9 (HEAD -> master)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 21:58:26 2020 +0800    modify c.txtdiff --git a/c.txt b/c.txtindex 7209f0a..c068359 100644--- a/c.txt+++ b/c.txt@@ -1,3 +1,4 @@ rebase test add one line in c.txt by user B add one line in c.txt by user B again+hello world

2. git commit -am "commit content"

可以认为是 git add file 和 git commit -m 的合体,直接用这个可以跳过 add 步骤而将这个文件提交。

[root@master GitTest]# git commit -am 'nice to meet you too'[master a60f009] nice to meet you too 1 file changed, 1 insertion(+)[root@master GitTest]# git logcommit a60f009eadfb8215c2424b91a584018a191ee992Author: looking 
Date: Sat Aug 29 15:27:15 2020 +0800 nice to meet you toocommit 7144239e8e9ca4cbceb596d497b7fd926e41c963Author: looking
Date: Sat Aug 29 15:00:05 2020 +0800 nice to meet you

3. git commit --amend

        有时候我们在提交完成之后才发现有几个文件没有提交,或者发现提交信息填写错了,这时候可以使用 git commit --amend 尝试重新进行提交。

1. 这条命令会将暂存区中的文件进行提交

2. 在提交的时候可以修改上一次 commit 的提交信息

3. 最终只有一次commit,第二次的 commit 会替代第一次的 commit (也就是只会产生一个commit节点,使用 git log 会看到上一次的 commit 不见了,被这次的新 commit 替换)

$ git commit --amend- mv test.txt to test directory+ mv test.txt to test dir# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date:      Wed Jul 29 22:28:31 2020 +0800## On branch master# Your branch is ahead of 'origin/master' by 2 commits.#   (use "git push" to publish your local commits)## Changes to be committed:#       renamed:    test.txt -> test/test.txt#$ git commit --amend[master db41223] mv test.txt to test dir Date: Wed Jul 29 22:28:31 2020 +0800 1 file changed, 1 insertion(+) rename test.txt => test/test.txt (71%)

git show

1. git show

查看最新的 commit 的修改提交记录。

[root@master GitTest]# git showcommit 3f2061298d921378da14f4003753f1f277e67243Author: looking 
Date: Thu Aug 20 23:02:46 2020 +0800 I am Looking add one line in hello.txt Signed-off-by: Lu Kaiyi
diff --git a/hello.txt b/hello.txtindex 10bda51..3319af8 100644--- a/hello.txt+++ b/hello.txt@@ -1,2 +1,3 @@ hello world hello world+hello world. I am Looking

2. git show HEAD~1

查看前一次 commit 的修改提交记录。

[root@master GitTest]# git logcommit 72017cd6e0222ff5fb544ce460ad3f2b046952edAuthor: looking 
Date: Mon Aug 31 21:59:19 2020 +0800 nice to meet you toocommit 50a084c8cce985ba6b5ee9bb7bce7a950671bb8eAuthor: looking
Date: Mon Aug 31 21:16:51 2020 +0800 nicecommit 3f2061298d921378da14f4003753f1f277e67243Author: looking
Date: Thu Aug 20 23:02:46 2020 +0800 I am Looking add one line in hello.txt Signed-off-by: Lu Kaiyi
------------------------------------------------------------[root@master GitTest]# git show HEAD~1commit 50a084c8cce985ba6b5ee9bb7bce7a950671bb8eAuthor: looking
Date: Mon Aug 31 21:16:51 2020 +0800 nicediff --git a/hello.txt b/hello.txtindex 3319af8..f0ce462 100644--- a/hello.txt+++ b/hello.txt@@ -1,3 +1,4 @@ hello world hello world hello world. I am Looking+nice

3. git show commit-id

[root@master GitTest]# git show 85c5449ca5228c2de015ee218ec75e6d5db9dddfcommit 85c5449ca5228c2de015ee218ec75e6d5db9dddfAuthor: looking 
Date: Tue Aug 11 22:49:28 2020 +0800 modify b.txt in branch testdiff --git a/b.txt b/b.txtindex 3b310f5..67947b0 100644--- a/b.txt+++ b/b.txt@@ -1,5 +1,5 @@ hahahahaha-hohohohoho+hoho hehehehehe rebase test add one line in b.txt by user B

4. git show commit-id filename

查看某次 commit 中具体某个文件的修改。

[root@master GitTest]# git show 85c5449ca5228c2de015ee218ec75e6d5db9dddf b.txtcommit 85c5449ca5228c2de015ee218ec75e6d5db9dddfAuthor: looking 
Date: Tue Aug 11 22:49:28 2020 +0800 modify b.txt in branch testdiff --git a/b.txt b/b.txtindex 3b310f5..67947b0 100644--- a/b.txt+++ b/b.txt@@ -1,5 +1,5 @@ hahahahaha-hohohohoho+hoho hehehehehe rebase test add one line in b.txt by user B[root@master GitTest]# git show 85c5449ca5228c2de015ee218ec75e6d5db9dddf c.txt

5. git show tag-name

[root@master GitTest]# git show v1.1commit f376be22b18d839c18cf6596f1c156a757c734e8Author: 2392863668 <2392863668@qq.com>Date:   Wed Jun 5 14:11:52 2019 +0800    add something to test.txtdiff --git a/test.txt b/test.txtindex e69de29..b7d1e74 100644--- a/test.txt+++ b/test.txt@@ -0,0 +1 @@+I am looking

6. git show tag-name^{tree} 

显示标签指向的树

[root@master GitTest]# git show v1.1^{tree}tree v1.1^{tree}b.txthello.txtlooking.txtmyGit.txtnewBranch.txtreadme.txttest.txtworld.txt

7. git show -s --format=%s tag-name^{commit}

显示 tag-name 指向的提交主题

[root@master GitTest]# git show -s --format=%s v1.1^{commit}add something to test.txt

git push

1. git push origin master

$ git pushEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 8 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.Total 3 (delta 2), reused 0 (delta 0)remote: Resolving deltas: 100% (2/2), completed with 2 local objects.To github.com:2392863668/GitTest.git   082b011..70f19e6  master -> master

2. git push origin --delete dev

删除远程分支dev

假设你已经通过远程分支完成了所有的工作,也就是所远程分支上的代码已经合并到了master中,并且这个远程分支决定以后不再使用,可以使用带有--delete的git push命令来删除远程分支。

git rm

1. git rm <filename>

执行以下命令:删除一个文件。

$ git rm 
$ git commit -m
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git rm d.txtrm 'd.txt'Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is up to date with 'origin/master'.Changes to be committed:  (use "git reset HEAD 
..." to unstage) deleted: d.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git commit -m "delete d.txt"[master 32b9c4e] delete d.txt 1 file changed, 4 deletions(-) delete mode 100644 d.txt

如果被删除的文件修改过,而且已经被放入了暂存区,那么需要加上 -f 参数,强制删除。这是一种安全特性,用于防止删除还没有被添加快照的数据,这种数据无法被Git恢复。

2. git rm --cached <filename>

如果你只是想从暂存区删除文件,但是工作区的文件保持不变(将文件保存在磁盘),也就是说将文件保存在磁盘但是不想让 Git 进行跟踪,使用如下命令即可:

$ git rm --cached 
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ vi test.txt  # 新行添加 hello worldgitAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: test.txtno changes added to commit (use "git add" and/or "git commit -a")Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git rm --cached test.txtrm 'test.txt'Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git status # 显示文件 test.txt 为 Untracked filesOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)Changes to be committed: (use "git reset HEAD
..." to unstage) deleted: test.txtUntracked files: (use "git add
..." to include in what will be committed) test.txt

 git log

1. git log 

列出所有commit,最新的commit在最上面。会显示每个提交的作者,提交信息等。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git logcommit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:28:31 2020 +0800    mv test.txt to test directorycommit 32b9c4ee0b74b629e1d1e99f1d0173a831638d7cAuthor: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:15:01 2020 +0800    delete d.txtcommit 70f19e6d0788ad7fca07b129a8615084fe08f1e9 (origin/master, origin/HEAD)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 21:58:26 2020 +0800    modify c.txt...

2. git log -p -2

显示最近的两次提交每个文件修改了哪些地方。-p用来显示每次提交修改了哪些地方。-2用于指定只显示最近的两次提交。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git log -p -2commit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:28:31 2020 +0800    mv test.txt to test directorydiff --git a/test.txt b/test/test.txtsimilarity index 71%rename from test.txtrename to test/test.txtindex 42dfcfc..041d1e3 100644--- a/test.txt+++ b/test/test.txt@@ -1,2 +1,3 @@ I am looking nice to meet you+hello worldcommit 32b9c4ee0b74b629e1d1e99f1d0173a831638d7cAuthor: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:15:01 2020 +0800    delete d.txtdiff --git a/d.txt b/d.txtdeleted file mode 100644index 92faea9..0000000--- a/d.txt+++ /dev/null@@ -1,4 +0,0 @@-testing-hello, hello-conflict testing-rebase test(END)

3. git log --stat

显示每次提交简略的统计信息。具体包括:在每次提交的下面列出所有被修改过的文件、有多少文件被修改了、被修改的文件有哪些行被移除或添加以及显示提交信息。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git log --statcommit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:28:31 2020 +0800    mv test.txt to test directory test.txt => test/test.txt | 1 + 1 file changed, 1 insertion(+)commit 32b9c4ee0b74b629e1d1e99f1d0173a831638d7cAuthor: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:15:01 2020 +0800    delete d.txt d.txt | 4 ---- 1 file changed, 4 deletions(-)commit 70f19e6d0788ad7fca07b129a8615084fe08f1e9 (origin/master, origin/HEAD)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 21:58:26 2020 +0800    modify c.txt c.txt | 1 + 1 file changed, 1 insertion(+)

4. git log --pretty=oneline

将指定使用不同与默认格式的方式展示提交历史。 比如oneline会将提交历史展示成一行,类似于下面这样。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git log --pretty=onelineffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master) mv test.txt to test directory32b9c4ee0b74b629e1d1e99f1d0173a831638d7c delete d.txt70f19e6d0788ad7fca07b129a8615084fe08f1e9 (origin/master, origin/HEAD) modify c.txt082b01177112fc3ccb93f234435fb06a930c19fb add hello world in b.txtd38440866faa62ff1a2c383be1fc780bbbb859f7 mv a.txt to test directory648e3e12da7fc4f1a13d21e3f0c50c94d5950c25 Merge branch 'dev' to 'master'd2009f78a7f83f8ae431caabac79d6a3978ada7a add one line in c.txt by user B again4505d98d63671b307aa5fcf093412d9be09012e5 add one line in b.txt by user B againcc12a897b784bc4ca660650446e66d3e81b73263 add one line in a.txt by user A again5afad542370810acaba829a1c7d8cdbb0c18dde9 add one line in c.txt by user Ba7b960d2fa25a0af07816bfd804cbf681894a180 add one line in b.txt by user B3ec68b58c6a825e8e87cb6af42395832280531f1 add one line in a.txt by user A26a6eb5ffbeafd862cfcf244453f27a14ad3b417 add one line in d.txtc0527a3f99463edae7363a0776a7b1444330f161 add one line in a.txtdeba76262ef797c862feeb5c8560314d45589092 Merge branch 'master' of github.com:2392863668/GitTest...

5. git log /path/to/file  

命令可以查看指定路径的历史记录

$ git log ./testcommit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 22:28:31 2020 +0800    mv test.txt to test directorycommit d38440866faa62ff1a2c383be1fc780bbbb859f7Author: 2392863668 <2392863668@qq.com>Date:   Wed Jul 29 20:53:13 2020 +0800    mv a.txt to test directoryAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$

6. git log --oneline --decorate --color --graph 

查看高级日志图

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git log --oneline --decorate --color --graph*   cd3e336 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'master' of github.com:2392863668/GitTest|\| * 3fceb8a add hello world in looking.txt* | 640fd3c add another hello world in hello.txt|/* db41223 mv test.txt to test dir* 32b9c4e delete d.txt* 70f19e6 modify c.txt* 082b011 add hello world in b.txt* d384408 mv a.txt to test directory*   648e3e1 Merge branch 'dev' to 'master'|\| * d2009f7 add one line in c.txt by user B again| * 4505d98 add one line in b.txt by user B again* | cc12a89 add one line in a.txt by user A again|/* 5afad54 add one line in c.txt by user B* a7b960d add one line in b.txt by user B* 3ec68b5 add one line in a.txt by user A* 26a6eb5 add one line in d.txt* c0527a3 add one line in a.txt*   deba762 Merge branch 'master' of github.com:2392863668/GitTest|\| * 44fb153 testing again* | 6565d76 (origin/dev) add something in b.txt* | 2a79103 add something in a.txt|/* 2db0e2b conflict solved* 28914cb testing again* e9a2ce2 add new line in d.txt* 45d0478 modify d.txt* 3c85912 add d.txt* 34a912e testing again

查看高级日志图

git checkout

1. git checkout -- <filename>

  当你对一个文件进行了修改,还没有add进暂存区,你想要放弃所有的修改,将文件恢复到没有修改之前的样子,这时候可以使用 git checkout -- <filename> 

这条命令会将文件恢复到上一次 commit 的时候这个文件的样子。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git checkout -- myGit.txt # 回到了我用 vi 修改文件 myGit.txt 之前的样子Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits.  (use "git push" to publish your local commits)nothing to commit, working tree clean

2. git checkout commit-id

将切换到指定 commit-id 的版本

[root@master GitTest]# cat hello.txt hello worldhello worldhello world. I am Lookingnice to meet you[root@master GitTest]# git checkout 3f2061298d921378da14f4003753f1f277e67243Note: checking out '3f2061298d921378da14f4003753f1f277e67243'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example:  git checkout -b new_branch_nameHEAD is now at 3f20612... I am Looking[root@master GitTest]# git branch* (detached from 3f20612)  dev  master  test[root@master GitTest]# cat hello.txt hello worldhello worldhello world. I am Looking

3. git checkout master

切换回当前最新的 commit 状态

[root@master GitTest]# git checkout masterPrevious HEAD position was 3f20612... I am Looking# 从之前的 commit 切换到 masterSwitched to branch 'master'Your branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)[root@master GitTest]# git logcommit 7144239e8e9ca4cbceb596d497b7fd926e41c963Author: looking 
Date: Sat Aug 29 15:00:05 2020 +0800 nice to meet youcommit 3f2061298d921378da14f4003753f1f277e67243Author: looking
Date: Thu Aug 20 23:02:46 2020 +0800 I am Looking add one line in hello.txt Signed-off-by: Lu Kaiyi
commit 6cbeadaba19d4e4c5613969155cb82ec19138960Merge: 6dd0fcc 85c5449Author: looking
Date: Tue Aug 11 23:02:07 2020 +0800 deal with merge conflict in b.txt[root@master GitTest]# git reflog7144239 HEAD@{0}: checkout: moving from 3f2061298d921378da14f4003753f1f277e67243 to master3f20612 HEAD@{1}: checkout: moving from master to 3f2061298d921378da14f4003753f1f277e672437144239 HEAD@{2}: commit: nice to meet you

git reset HEAD~ 

1. git reset HEAD~

有的时候你只需要撤销 commit,但并不想将 commit下的代码也撤销,那么可以先找到你的最新的 commit ID,撤销上一次的 commit。

git reset HEAD~  或者  git reset HEAD~1

如果你提交了多个commit,那么可以通过修改HEAD~之后的数字,如撤销前2次的commit

git reset HEAD~2  

注:使用此命令,你原来提交的代码都在,不会被撤销,也即只会撤销  commit 记录(不对文件内容进行修改)。

[root@master GitTest]# git status# On branch masternothing to commit, working directory clean[root@master GitTest]# vim b.txt # 删除了文件最后一行# 下面 commit 失败,因为修改的文件还没添加到暂存区[root@master GitTest]# git commit -m "delete last line in b.txt"# On branch master# Changes not staged for commit:#   (use "git add 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: b.txt#no changes added to commit (use "git add" and/or "git commit -a")------------------------------------------------------------[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "delete last line in b.txt"[master 131c2e9] delete last line in b.txt 1 file changed, 1 deletion(-)[root@master GitTest]# git logcommit 131c2e9e676726168a87db678c17e8ec404b8c4eAuthor: looking
Date: Sat Aug 8 01:35:09 2020 +0800 delete last line in b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt------------------------------------------------------------# 注意啦,放大招啦。[root@master GitTest]# git reset HEAD~Unstaged changes after reset:M b.txt# 这个时候回到了 add 和 commit 提交之前,但是在文件 b.txt 修改之后的那个状态[root@master GitTest]# git status# On branch master# Changes not staged for commit:# (use "git add
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: b.txt#no changes added to commit (use "git add" and/or "git commit -a")------------------------------------------------------------# 因为 commit 被撤销了,所以我们再重新提交一次[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "delete last line in b.txt"[master 33796e9] delete last line in b.txt 1 file changed, 1 deletion(-)[root@master GitTest]# git logcommit 33796e9844af4b6e1ee23c2d98ec5be4c5fef3e6Author: looking
Date: Sat Aug 8 01:44:27 2020 +0800 delete last line in b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txtcommit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8a[root@master GitTest]#

2. git reset HEAD~2

[root@master GitTest]# vim b.txt [root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "delete one line in b.txt"[master 290d1c2] delete one line in b.txt 1 file changed, 1 deletion(-)[root@master GitTest]# git logcommit 290d1c2f6721b57cb8bd986974fedc16431221dcAuthor: looking 
Date: Sat Aug 8 01:50:27 2020 +0800 delete one line in b.txtcommit 33796e9844af4b6e1ee23c2d98ec5be4c5fef3e6Author: looking
Date: Sat Aug 8 01:44:27 2020 +0800 delete last line in b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits.------------------------------------------------------------ [root@master GitTest]# cat b.txthahahahahahohohohohorebase testadd one line in b.txt by user Badd one line in b.txt by user B again------------------------------------------------------------[root@master GitTest]# git reset HEAD~2Unstaged changes after reset:M b.txt------------------------------------------------------------# 注意看,git reset HEAD~2 执行前后, b.txt 文件内容并没有发生变化哟!# 但是 git log 的最近两次 commit 没有了!------------------------------------------------------------[root@master GitTest]# cat b.txthahahahahahohohohohorebase testadd one line in b.txt by user Badd one line in b.txt by user B again------------------------------------------------------------[root@master GitTest]# git logcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txtcommit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8aAuthor: 2392863668 <2392863668@qq.com>Date: Wed Jul 29 23:49:34 2020 +0800 Merge branch 'master' of github.com:2392863668/GitTest------------------------------------------------------------[root@master GitTest]# git status# On branch master# Changes not staged for commit:# (use "git add
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: b.txt#no changes added to commit (use "git add" and/or "git commit -a")# 我们仍然可以用 git diff 查看修改[root@master GitTest]# git diffdiff --git a/b.txt b/b.txtindex 3b310f5..ff8ec56 100644--- a/b.txt+++ b/b.txt@@ -1,7 +1,5 @@ hahahahaha hohohohoho-hehehehehe rebase test add one line in b.txt by user B add one line in b.txt by user B again-hello world

3. git reset --hard HEAD^1

注:使用了之后,你最新的 commit 命令下修改的内容将完全被撤销,最近一次的 commit 记录也没了。

适用情况:你本地 commit 的一个 patch 最终被 committer 进行 reject 了,你需要把这次修改和记录都撤销掉。

[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "modify file b.txt"[master be7abc1] modify file b.txt 1 file changed, 2 deletions(-)[root@master GitTest]# git logcommit be7abc12a1a94e9390bd6c7f4bc747f9bbe86ab9Author: looking 
Date: Sat Aug 8 02:01:32 2020 +0800 modify file b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt------------------------------------------------------------ [root@master GitTest]# cat b.txt hahahahahahohohohohorebase testadd one line in b.txt by user Badd one line in b.txt by user B again------------------------------------------------------------ [root@master GitTest]# git reset --hard HEAD^1HEAD is now at f3b4d4a This is a combination of 2 commits.------------------------------------------------------------ # 注意看,git reset --hard HEAD^1 执行前后, b.txt 文件内容发生变化了哟!# git log 的最近一次 commit 没有了!------------------------------------------------------------[root@master GitTest]# cat b.txt hahahahahahohohohohoheheheheherebase testadd one line in b.txt by user Badd one line in b.txt by user B againhello world------------------------------------------------------------ [root@master GitTest]# git logcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt

4. git reset --hard commitId(谨慎使用)

如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法。

[root@master GitTest]# git reset --hard 3f2061298d921378da14f4003753f1f277e67243HEAD is now at 3f20612 I am Looking[root@master GitTest]# git reflog3f20612 HEAD@{0}: checkout: moving from master to master3f20612 HEAD@{1}: reset: moving to 3f2061298d921378da14f4003753f1f277e67243a60f009 HEAD@{2}: commit: nice to meet you too7144239 HEAD@{3}: checkout: moving from dev to master

5. git reset HEAD <filename>

 使用 git reset HEAD <filename> 可以将暂存(已经 git add)的文件变为未暂存的文件。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ vi myGit.txt # 添加新行 hello worldAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits.  (use "git push" to publish your local commits)Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: myGit.txtno changes added to commit (use "git add" and/or "git commit -a")Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git add myGit.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits)Changes to be committed: (use "git reset HEAD
..." to unstage) modified: myGit.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git reset HEAD myGit.txtUnstaged changes after reset:M myGit.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits)Changes not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: myGit.txtno changes added to commit (use "git add" and/or "git commit -a")

强制拉取覆盖本地

如果你想以远程仓库的版本内容为准,丢弃本地所有修改(当然,本地的 untracked file 不属于版本控制的范围,不会受到这条命令的影响),又不想删除本地仓库以后再次 git  clone,就按下边这么做吧。

git fetch --all && git reset --hard origin/master && git pull

git rebase

rebase的工作流就是:
git rebase while(存在冲突) {    git status    找到当前冲突文件,编辑解决冲突    git add -u    git rebase --continue    if( git rebase --abort )        break; //回到rebase之前

git rebase 是通过一种续接的方式两分支的最新 commit 揉合到一个新的 commit 上达到合并效果的。

1. git pull --rebase

git pull --rebase = git fetch + git rebase

2. git rebase --continue

在rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决 冲突;rebase 和 merge 的另一个区别是 rebase 的冲突是一个一个解决,如果有十个冲突,先解决第一个,然后用命令 git rebase --continue 继续进行 rebase。继续后才会出现第二个冲突,直到所有冲突解决完,而 merge 是所有的冲突都会显示出来。

3. git rebase --abort

如果 rebase 过程中,你想中途退出,恢复 rebase 前的代码则可以用命令 git rebase --abort。

4. git rebase -i HEAD~2

如果你想把本地的多个 commit 合并成一个,就用这个吧(从 git log 可以看到,两次 commit 变成了一个,两次 commit 所做的修改也反映到了一个 commit 上):

[root@master GitTest]# git status# On branch master# Your branch is ahead of 'origin/master' by 2 commits.#   (use "git push" to publish your local commits)#nothing to commit, working directory clean[root@master GitTest]# git rebase -i HEAD~2pick 1a6cb23 add hello in new line of looking.txtsquash 636b8a7 add world in new line of looking.txt# Rebase cd3e336..636b8a7 onto cd3e336## Commands:#  p, pick = use commit#  r, reword = use commit, but edit the commit message#  e, edit = use commit, but stop for amending#  s, squash = use commit, but meld into previous commit#  f, fixup = like "squash", but discard this commit's log message#  x, exec = run command (the rest of the line) using shell## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST."~/GitTest/.git/rebase-merge/git-rebase-todo" 20L, 706C[root@master GitTest]# git rebase -i HEAD~2[detached HEAD f3b4d4a] This is a combination of 2 commits. 1 file changed, 2 insertions(+)Successfully rebased and updated refs/heads/master.[root@master GitTest]# git logcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking 
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txtcommit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8aAuthor: 2392863668 <2392863668@qq.com>Date: Wed Jul 29 23:49:34 2020 +0800 Merge branch 'master' of github.com:2392863668/GitTestcommit 640fd3c8eb8ab632691d448b1df216ab031a9071[root@master GitTest]# git log --statcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt looking.txt | 2 ++ 1 file changed, 2 insertions(+)commit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8aAuthor: 2392863668 <2392863668@qq.com>Date: Wed Jul 29 23:49:34 2020 +0800 Merge branch 'master' of github.com:2392863668/GitTest

git tag

1. git tag

git tag 会按照字母顺序列出所有的标签

git tag -l 'v1.0*' 列出v1.0系列的标签。

[root@master GitTest]# git tagv1.0v1.1

                                             

2. git show <tag name>

显示标签的相关信息,比如被打在了哪个 commit 上。

[root@master GitTest]# git show v1.1commit f376be22b18d839c18cf6596f1c156a757c734e8Author: 2392863668 <2392863668@qq.com>Date:   Wed Jun 5 14:11:52 2019 +0800    add something to test.txtdiff --git a/test.txt b/test.txtindex e69de29..b7d1e74 100644--- a/test.txt+++ b/test.txt@@ -0,0 +1 @@+I am looking

3. git push origin <tag name>

如果想将本地的tag上传到远程仓库,则必须显示的将tag push上去。默认情况下 git push  是不会将tag上传到远程仓库的。一次推送许多标签: git push origin --tags  会将不在远程仓库的所有标签上传上去。

[root@master GitTest]# git tag -a v1.2 -m "version 1.2"[root@master GitTest]# git tagv1.0v1.1v1.2[root@master GitTest]# git push origin v1.2Counting objects: 1, done.Writing objects: 100% (1/1), 154 bytes | 0 bytes/s, done.Total 1 (delta 0), reused 0 (delta 0)To git@github.com:2392863668/GitTest.git * [new tag]         v1.2 -> v1.2

                                       

4. git tag -d <tagname>

[root@master GitTest]# git tag -d v1.2Deleted tag 'v1.2' (was d332b35)[root@master GitTest]# git push origin :refs/tags/v1.2To git@github.com:2392863668/GitTest.git - [deleted]         v1.2

git remote

1. git remote -v 

查看远程URL,如果有多个的话会都列出来(比如可能会同时 push 到多个仓库的话,就有多个 push 的 URL)

[root@master GitTest]# git remote -vorigin	git@github.com:2392863668/GitTest.git (fetch)origin	git@github.com:2392863668/GitTest.git (push)[root@master GitTest]#

2. git remote add origin git@github.com:2392863668/GitTest.git

[root@master GitTest]# git remote add origin git@github.com:2392863668/GitTest.git

3. git push origin master

注意:只有当你对远程仓库具有写入权限的时候,这条命令才会生效。当你在与别人合作开发一个项目的时候,如果,他们先推送了自己的代码到了远程仓库,你在直接进行推送,会被拒绝,你必须先将他们的代码先拉取下来进行合并(merge or rebase)之后,才能进行推送。

[root@master GitTest]# git push origin masterEverything up-to-date

4. git remote show origin 

[root@master GitTest]# git remote show origin* remote origin  Fetch URL: git@github.com:2392863668/GitTest.git  Push  URL: git@github.com:2392863668/GitTest.git  HEAD branch: master  Remote branch:    master tracked  Local branch configured for 'git pull':    master merges with remote master  Local ref configured for 'git push':    master pushes to master (up to date)

5. git remote rm <remote-name>

移除远程仓库(这块我就不多尝试了)

[root@master GitTest]# git remote rm origin[root@master GitTest]# git remote add origin [repo-url]

6. git remote rename <old-name> <new-name>

远程仓库重命名

git branch

注意:分支切换会改变你本地的工作区域的内容。会将本地工作区域的内容恢复到被切换到的分支上最后一个提交时候的样子。

1. git branch

查看已有分支

[root@master GitTest]# git branch  dev* master

2. git branch <branch name>

新建分支

[root@master GitTest]# git branch test[root@master GitTest]# git branch  dev* master  test

3. git checkout <branch-name>

切换分支

[root@master GitTest]# git checkout testSwitched to branch 'test'[root@master GitTest]# git branch  dev  master* test

4. git branch -d test

删除一个分支

[root@master GitTest]# git branch -d testDeleted branch test (was f3b4d4a).
 

5. git branch -v 

查看每一个分支上的最后一次commit

[root@master GitTest]# git branch -v  dev    d2009f7 add one line in c.txt by user B again* master 6cbeada [ahead 3] deal with merge conflict in b.txt  test   85c5449 modify b.txt in branch test

6. git branch --merged(--no-merged)

查看已经(没有)合并到当前分支的所有分支

[root@master GitTest]# git branch --merged  dev* master  test[root@master GitTest]# git branch --no-merged

7. git branch -vv

[root@master GitTest]# git branch -vv  dev    6cbeada deal with merge conflict in b.txt* master 6cbeada [origin/master] deal with merge conflict in b.txt  test   85c5449 modify b.txt in branch test

8. git log --decorate

命令查看各个分支当前所指的对象

[root@master GitTest]# git log --decorate commit f3b4d4abd65a59c8a03df6caccf28f33422b4614 (HEAD, origin/master, test, master)Author: looking 
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txtcommit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8aAuthor: 2392863668 <2392863668@qq.com>Date: Wed Jul 29 23:49:34 2020 +0800 Merge branch 'master' of github.com:2392863668/GitTestcommit 640fd3c8eb8ab632691d448b1df216ab031a9071Author: 2392863668 <2392863668@qq.com>Date: Wed Jul 29 23:49:11 2020 +0800

9. git checkout -b <branch-name>

创建一个分支并且切换到这个分支

[root@master GitTest]# git checkout -b testSwitched to a new branch 'test'[root@master GitTest]# git branch  dev  master* test

git merge <branch-name>

1. 无冲突的 git merge

我切换到 test 分支提交了一个 commit:

[root@master GitTest]# git checkout -b testSwitched to a new branch 'test'[root@master GitTest]# git branch  dev  master* test[root@master GitTest]# vim looking.txt [root@master GitTest]# git add looking.txt [root@master GitTest]# git commit -m "do something in looking.txt with branch test" [test e233561] do something in looking.txt with branch test 1 file changed, 1 insertion(+)[root@master GitTest]# git logcommit e233561b7b56948d721931c412c5f9e239ac3470Author: looking 
Date: Tue Aug 11 22:41:50 2020 +0800 do something in looking.txt with branch testcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt

然后切换回 master 分支:

[root@master GitTest]# git checkout masterSwitched to branch 'master'[root@master GitTest]# git logcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking 
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txtcommit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8aAuthor: 2392863668 <2392863668@qq.com>

然后适用 git merge 进行分支合并:

[root@master GitTest]# git merge testUpdating f3b4d4a..e233561Fast-forward looking.txt | 1 + 1 file changed, 1 insertion(+)[root@master GitTest]# git logcommit e233561b7b56948d721931c412c5f9e239ac3470Author: looking 
Date: Tue Aug 11 22:41:50 2020 +0800 do something in looking.txt with branch testcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt

上面是没有冲突的合并,如果是有冲突的呢?

2. 有冲突的 git merge

在分支对 b.txt 进行修改:

[root@master GitTest]# git checkout -b testSwitched to a new branch 'test'[root@master GitTest]# lsb.txt  c.txt  hello.txt  looking.txt  myGit.txt  newBranch.txt  readme.txt  test  world.txt[root@master GitTest]# vim b.txt [root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m 'modify b.txt in branch test'[test 85c5449] modify b.txt in branch test 1 file changed, 1 insertion(+), 1 deletion(-)

切换回主分支以后对 b.txt 相同地方进行修改:

[root@master GitTest]# git checkout masterSwitched to branch 'master'[root@master GitTest]# vim b.txt [root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "modify b.txt in the same place with master branch"[master 6dd0fcc] modify b.txt in the same place with master branch 1 file changed, 1 insertion(+), 1 deletion(-)

然后再次进行 git merge:

[root@master GitTest]# git merge testAuto-merging b.txtCONFLICT (content): Merge conflict in b.txtAutomatic merge failed; fix conflicts and then commit the result.[root@master GitTest]# cat b.txt hahahahaha<<<<<<< HEADhohohohohoyoyoyo=======hoho>>>>>>> testheheheheherebase testadd one line in b.txt by user Badd one line in b.txt by user B againhello world

这次合并有冲突没能正常进行合并,不过告诉你了冲突的文件,需要手动处理冲突并 commit 结果:

其中在 ======= 的上半部分是 HEAD 所指向的master分支的内容,在 ======= 的下半部分是 test 分支的内容。你需要自己手动去选择保留 ======= 的上半部分或者下半部分,或者都保留。同时你也需要删除<<<<<<< HEAD 、=======   以及  >>>>>>> test 这些特殊标记。

当你自己手动解决了所有的冲突之后,你需要运行 git add <filename> 来将每一个存在冲突的文件标记为冲突已解决的状态(没有 git add 的话,会 git commit 失败哟 --可以看我下面的实例),并且暂存这些文件。通常你还需要运行 git status 来确认所有的冲突是否都已经解决了。最后运行 git commit 来完成合并提交。

我这里选择保留主分支对 b.txt 的修改。当然,合并之前冲突的两次 commit 记录还是在 log 里的:

[root@master GitTest]# cat b.txt hahahahahahohohohohoyoyoyoheheheheherebase testadd one line in b.txt by user Badd one line in b.txt by user B againhello world------------------------------------------------------------[root@master GitTest]# git status# On branch master# Your branch is ahead of 'origin/master' by 1 commit.#   (use "git push" to publish your local commits)## You have unmerged paths.#   (fix conflicts and run "git commit")## Unmerged paths:#   (use "git add 
..." to mark resolution)## both modified: b.txt#no changes added to commit (use "git add" and/or "git commit -a")------------------------------------------------------------[root@master GitTest]# git commit -m "deal with merge conflict in b.txt"U b.txterror: 'commit' is not possible because you have unmerged files.hint: Fix them up in the work tree,hint: and then use 'git add/rm
' ashint: appropriate to mark resolution and make a commit,hint: or use 'git commit -a'.fatal: Exiting because of an unresolved conflict.------------------------------------------------------------[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "deal with merge conflict in b.txt"[master 6cbeada] deal with merge conflict in b.txt[root@master GitTest]# git logcommit 6cbeadaba19d4e4c5613969155cb82ec19138960Merge: 6dd0fcc 85c5449Author: looking
Date: Tue Aug 11 23:02:07 2020 +0800 deal with merge conflict in b.txtcommit 6dd0fcc7dc9b61caaf732cdcec11ddd5f387266aAuthor: looking
Date: Tue Aug 11 22:53:55 2020 +0800 modify b.txt in the same place with master branchcommit 85c5449ca5228c2de015ee218ec75e6d5db9dddfAuthor: looking
Date: Tue Aug 11 22:49:28 2020 +0800 modify b.txt in branch testcommit e233561b7b56948d721931c412c5f9e239ac3470Author: looking
Date: Tue Aug 11 22:41:50 2020 +0800 do something in looking.txt with branch test

合并记录: 

[root@master GitTest]# git log --oneline --decorate --color --graph*   6cbeada (HEAD, master) deal with merge conflict in b.txt|\  | * 85c5449 (test) modify b.txt in branch test* | 6dd0fcc modify b.txt in the same place with master branch|/  * e233561 (origin/master) do something in looking.txt with branch test* f3b4d4a This is a combination of 2 commits.

git 获取某次 commit 的指定信息

HEAD 可以用具体的 commit-id 代替

%H 提交对象(commit)的完整哈希字串 %h 提交对象的简短哈希字串 %T 树对象(tree)的完整哈希字串 %t 树对象的简短哈希字串 %P 父对象(parent)的完整哈希字串 %p 父对象的简短哈希字串 %an 作者(author)的名字 %ae 作者的电子邮件地址 %ad 作者修订日期(可以用 -date= 选项定制格式) %ar 作者修订日期,按多久以前的方式显示 %cn 提交者(committer)的名字 %ce 提交者的电子邮件地址 %cd 提交日期 %cr 提交日期,按多久以前的方式显示 %s 提交说明

1. git rev-parse HEAD(commit-id)

获取最新的 commit-id

[root@master GitTest]# git rev-parse HEAD3f2061298d921378da14f4003753f1f277e67243[root@master GitTest]# git rev-parse be9f1c9c3af65d8dcbabf8219014dcd0146d3c74be9f1c9c3af65d8dcbabf8219014dcd0146d3c74[root@master GitTest]# git log --pretty=format:"%H" HEAD -13f2061298d921378da14f4003753f1f277e67243

2. git rev-parse --show-toplevel

获取仓库根目录所在路径(top level path)

[root@master test]# pwd/root/GitTest/test[root@master test]# git rev-parse --show-toplevel/root/GitTest

3. git log -n1 --pretty=format:"%an" HEAD(author name)

获取某个最新 commit-id 对应的作者

[root@master GitTest]# git log -n1 --pretty=format:"%an" HEADlooking[root@master GitTest]# git log -n1 --pretty=format:"%an" be9f1c9c3af65d8dcbabf8219014dcd0146d3c742392863668

4.  git log -n1 --pretty=format:"%ae" HEAD(author email)

[root@master GitTest]# git log -n1 --pretty=format:"%ae" HEADlooking@qq.com[root@master GitTest]# git log -n1 --pretty=format:"%ae" be9f1c9c3af65d8dcbabf8219014dcd0146d3c742392863668@qq.com

5. git log -n1 --pretty=format:"%cd" HEAD(commit date)

[root@master GitTest]# git log -n1 --pretty=format:"%cd" HEADMon Aug 31 22:35:21 2020 +0800[root@master GitTest]# git log -n1 --pretty=format:"%cd" be9f1c9c3af65d8dcbabf8219014dcd0146d3c74Sun Jul 26 18:37:21 2020 +0800

6. git log -n1 --pretty=format:"%ci" HEAD(commit time) 

[root@master GitTest]# git log -n1 --pretty=format:"%ci" HEAD2020-08-31 22:35:21 +0800[root@master GitTest]# git log -n1 --pretty=format:"%ci" be9f1c9c3af65d8dcbabf8219014dcd0146d3c742020-07-26 18:37:21 +0800

7. git log -n1 --pretty=format:"%s" HEAD(commit comment)

[root@master GitTest]# git log -n1 --pretty=format:"%s" HEADnice to meet you too[root@master GitTest]# git log -n1 --pretty=format:"%s" be9f1c9c3af65d8dcbabf8219014dcd0146d3c74add words "I am Looking"

8. git -C '/root/GitTest' log -n1 --pretty=format:"%an" HEAD

在指定目录下执行 git 命令(也即当前所在目录可能并不是一个 git repo)。

[root@master ~]# git -C '/root/GitTest' log -n1 --pretty=format:"%an" HEADlooking

9. git diff HEAD~2..HEAD

与之前某个 commit 进行对比,查看有哪些改动。

[root@master GitTest]# git diff HEAD~2..HEADdiff --git a/hello.txt b/hello.txtindex 3319af8..12649d5 100644--- a/hello.txt+++ b/hello.txt@@ -1,3 +1,5 @@ hello world hello world hello world. I am Looking+nice+nice to meet you too

git 获取 tag 信息

1. git describe --tags `git rev-list --tags --max-count=1`

获取最近的 tag 

[root@master GitTest]# git describe --tags `git rev-list --tags --max-count=1`v1.1

2. git show-ref --tags

获取 tag 的 commit-id 信息

[root@master GitTest]# git show-ref --tags90d62194e03629449fca0e2a93d176951e6a26b7 refs/tags/v1.0f376be22b18d839c18cf6596f1c156a757c734e8 refs/tags/v1.1

3. git show-ref

获取分支、tag 等的 commit-id 信息

[root@master GitTest]# git show-ref6cbeadaba19d4e4c5613969155cb82ec19138960 refs/heads/dev72017cd6e0222ff5fb544ce460ad3f2b046952ed refs/heads/master85c5449ca5228c2de015ee218ec75e6d5db9dddf refs/heads/test3f2061298d921378da14f4003753f1f277e67243 refs/remotes/origin/master12cace283d753253fea83bdaa0794047e0b0df1f refs/stash90d62194e03629449fca0e2a93d176951e6a26b7 refs/tags/v1.0f376be22b18d839c18cf6596f1c156a757c734e8 refs/tags/v1.1

 

转载地址:http://dcjqi.baihongyu.com/

你可能感兴趣的文章
一个递归+二分法的洗牌程序
查看>>
YUV格式注释
查看>>
一维、二维数组传参
查看>>
判断当前时间的下一秒是多少
查看>>
从文本文件中读取数据排序并输出到文本
查看>>
求一个整数数组中第二大的数
查看>>
删除一个链表中的节点
查看>>
计算机网络面试整理【转】
查看>>
cookie和session区别详解
查看>>
程序员失业第一步?斯坦福研究员用AI从编译器反馈中学习改Bug
查看>>
原创 | 电视广告流量预测中的“常识”陷阱,你掉进去了吗?
查看>>
DeepMind发布最新《神经网络中持续学习》综述论文!
查看>>
本科三篇顶会一作、超算竞赛冠军,2020清华本科特奖结果出炉
查看>>
多语言互通:谷歌发布实体检索模型,涵盖超过100种语言和2000万个实体
查看>>
你的房东可能正用AI筛查你的犯罪记录,决定要不要租房给你
查看>>
AI把爱豆变胖视频火遍B站,我们找到了背后的技术团队:你是怎么把刘亦菲变胖的?...
查看>>
白硕:区块链技术与数据隐私(附视频)
查看>>
数据蒋堂 | 报表工具的SQL植入风险
查看>>
AAC ADTS LATM 格式分析
查看>>
【转载】嵌入式系统 Boot Loader 技术内幕
查看>>