git日常操作记录说明

  • 1. 仅限最后一次commit增补
  • 2. 检出操作
    • 2.1 恢复工作区域文件
    • 2.2 切换分支恢复工作区域文件
  • 3. 重置操作
    • 3.1 常用命令
  • 4. 增删改文件操作
    • 4.1 增加文件
    • 4.2 修改文件名称
    • 4.3 删除某文件
  • 5. 合并到当前分支
    • 5.1 创建分支
    • 5.2 合并分支
    • 5.3 冲突解决
    • 5.4 bug修复
    • 5.5 强行删除分支
    • 5.6 多人协作
    • 5.7 标签


1. 仅限最后一次commit增补

在git中经常会遇到这样一件事,在提交一个任务后,发现忘记提交某些文件,但是又不想再次创建一个commit,因此此时有一种增补的方式,很轻松的实现再次添加数据
仅限最后一次commit的增补
具体操作

git add license
git commit --amend " file and add license" #实现增补

考虑:能否通过调整HEAD,来某个节点的增补?

2. 检出操作

检出操作checkout最主要从什么地方拿会代码,是从暂存区域拿回代码呢,还是从某个commit的点拿回代码,还是从不同的分支去拿回代码,从不同的分支拿回代码就可以从不同的点进行编辑代码,因此这个操纵极为重要。

2.1 恢复工作区域文件

主要是将暂存区或commit的文件恢复到工作区域
git checkout:列出暂存区可以被检出的文件

git checkout file从暂存区检出文件到工作区,就是覆盖工作区域文件,但是暂存区的文件并不会删除。
在此分两种情况

  • 情况1:当fiel文件在工作区域修改后,但是尚未add到暂存区域,此时检出修改,相当于回到版本库中的内容,在系统中会有提示。
  • 情况2:当file文件在工作区域修改后,而且add到暂存区域,此时检出修改,相当于从暂存区域回到工作区域,但是暂存区域不会发生此文件

git checkout commit HEAD~n就坐着时光机回到了过去,就是从某个提交点进行检出,通过git log可知,完全回到了古代,现代的历史日志完全没有了,当然在没有关闭终端时,可以根据终端历史查询过的最后一次的hash ID再次回到现代,这些操作都是比较危险,慎用。

git checkout commit file 检出某个commit的文件到工作区域

[root@localhost cmdb]#cat index.html 
welcome to about
welcome to about
welcome to about
welcome to about
[root@localhost cmdb]#git checkout
[root@localhost cmdb]#git log
commit ee82e0f38ee0b37fcb67864a25f2515a0d9d2cf3
Author: xuewb @126.com>
Date:   Wed May 23 00:51:30 2018 +0800

    04index

commit 5fb4132b497e3ec4b7bfa7f0b3e98f21b87b5ec4
Author: xuewb @126.com>
Date:   Wed May 23 00:51:21 2018 +0800

    03index

commit 97efd1249f8894f6f6b48d1e2b8a8ab275b44bf0
Author: xuewb @126.com>
Date:   Wed May 23 00:51:09 2018 +0800

    02index

commit 7a38ddb0c9d8c541e3bd0adbd128f694a194089e
Author: xuewb @126.com>
Date:   Wed May 23 00:50:53 2018 +0800

    01index
[root@localhost cmdb]#git checkout HEAD^^^ index.html
[root@localhost cmdb]#ll
total 4
-rw-r--r-- 1 root root 17 May 23 00:52 index.html
[root@localhost cmdb]#cat index.html 
welcome to about

git checkout .检出暂存区的所有文件

2.2 切换分支恢复工作区域文件

3. 重置操作

reset就是用最后一次commit覆盖指定的文件

3.1 常用命令

git reset
查看可以覆盖的有哪些文件,

git reset file
可以通过git diff git diff HEAD git diff --cached验证,重置暂存区域的某文件,相当于删除暂存区域的某文件,用最后一次commit的文件替代add的文件

[root@localhost cmdb]#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:   index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost cmdb]#git add index.html 
[root@localhost cmdb]#git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   modified:   index.html
#
[root@localhost cmdb]#git reset index.html 
Unstaged changes after reset:
M   index.html
[root@localhost cmdb]#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:   index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

git reset --hard重置暂存区域和工作区域的某文件,相当于删除暂存区域和工作区域的某文件,用最后一次commit的文件替代add的文件

[root@localhost cmdb]#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:   index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost cmdb]#vim index.html 
[root@localhost cmdb]#git add index.html 
[root@localhost cmdb]#git reset --hard index.html
fatal: Cannot do hard reset with paths.
[root@localhost cmdb]#git reset --hard     #代码指令操作
HEAD is now at 6101de7 index.html last
[root@localhost cmdb]#cat index.html 
welcome to about
Stupid Boss

4. 增删改文件操作

4.1 增加文件

git add file
git commit -m "msessage"

4.2 修改文件名称

主要分两种情况,文件在暂存区和版本库中,但是不管在哪都可以通过git mv src dst进行修改

[root@localhost cmdb]#cat python.htm 
Python
[root@localhost cmdb]#git add python.htm 
[root@localhost cmdb]#git commit -m "add python"
[master 8fc0186] add python
 1 file changed, 1 insertion(+)
 create mode 100644 python.htm
[root@localhost cmdb]#git mv python.htm python.py
[root@localhost cmdb]#git commit -m "mv python"
[master 5d5bafc] mv python
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename python.htm => python.py (100%)

4.3 删除某文件

将暂存区文件进行删除git rm --cached python.py,但是文件并未真正的删除,在工作区域依然存在,

[root@localhost cmdb]#git rm --cached python.py 
rm 'python.py'
[root@localhost cmdb]#git diff --cached
diff --git a/python.py b/python.py
deleted file mode 100644
index 46c5d2c..0000000
--- a/python.py
+++ /dev/null
@@ -1 +0,0 @@
-Python
[root@localhost cmdb]#git commit -m "dele python"
[master 84455d1] dele python
 1 file changed, 1 deletion(-)
 delete mode 100644 python.py

从版本库和工作区域删除某文件git rm python.py

[root@localhost cmdb]#ll
total 4
-rw-r--r-- 1 root root 20 May 23 04:00 python.py
[root@localhost cmdb]#git add python.py 
[root@localhost cmdb]#git commit -m "add python.py"
[master 6721c87] add python.py
 1 file changed, 1 insertion(+)
 create mode 100644 python.py
[root@localhost cmdb]#git rm python.py 
rm 'python.py'
[root@localhost cmdb]#ll
total 0

5. 合并到当前分支

5.1 创建分支

创建分支分为两种情况,第一种为在当前的commit点创建分支,另外是在历史的某一个commit点创建分支,这两种情况通常都会使用到
当前commit点创建分支
方法1:

[root@localhost cmdb]#git checkout -b devv
Switched to a new branch 'devv'

方法2:

[root@localhost cmdb]#git branch devv
[root@localhost cmdb]#git checkout devv
Switched to branch 'devv'

历史commit点创建分支

语法格式如下:

git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] []

具体操作如下:

[root@localhost cmdb]#git checkout -b dev 6b0616408ab3750997
Switched to a new branch 'dev'
[root@localhost cmdb]#cat readme 
This is a file that to ensure a problem

5.2 合并分支

合并分支时就是将某一个分支合并到当前分支时要注意如下两个参数,并详细了解此参数,两参数分别为--ff--no-ff

--ff

When the merge resolves as a fast-forward, only update the branch pointer, without creating a merge commit.
当合并到当前分支时,仅仅是跟新到当前分支中,并不会创建合并commit点
  • 默认为这种操作,这种操纵是当删除分支信息后,会丢失分支信息
  • 当合并到当前分支时,仅仅是跟新到当前分支中,并不会创建合并commit点
    --no-ff
Create a merge commit even when the merge resolves as a fast-forward.
在创建合并的同时创建了一个commit
  • 这种模式下操作是:当删除分支信息后,回保留分支信息
  • 在创建合并的同时创建了一个commit点

5.3 冲突解决

冲突原因:如何才能触发冲突,不同分支,在同一文件相同的位置做了不同的修改,这就触发了冲突,
解决方法:手动改写后提交

[root@localhost cmdb]#git merge con
Auto-merging readme
CONFLICT (content): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
[root@localhost cmdb]#cat readme 
This is a file that to ensure a problem
rr
<<<<<<< HEAD
creating a a a test
=======
creating a test
>>>>>>> con

5.4 bug修复

当工作到一半时,发现上一个版本有bug,而且这时及时修改,但是现在任务又作了一半,不能删除丢失现在已经完成的任务,此时就可以通过stash暂时存档,暂时存档和dato中存储档案一样,当一段时间后可以重新载入档案
暂时存档特性:
- 暂时存档后,工作区域的内容变为上一次commit的内容
- 暂时存档后,可以通过git stash list查看存档的相关资料
- 暂时存档后,可以通过如下方法重新载入回来
方法1:通过git stash apply载入,git stash drop删除档案

[root@localhost cmdb]#git stash list
stash@{0}: WIP on master: ae55a70 Merge branch 'dev'
[root@localhost cmdb]#git stash apply
# 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:   readme
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost cmdb]#cat readme 
This is a file that to ensure a problem
rr
<<<<<<< HEAD
creating a a a test
=======
creating a test
>>>>>>> con
test
I'am so quickly
I'am so quickly
[root@localhost cmdb]#git stash drop
Dropped refs/stash@{0} (35c07f3dce697aa3b3889b4e92d75dd7919536df)

方法2:通过git stash pop一次性载入并删除档案

[root@localhost cmdb]#git stash
Saved working directory and index state WIP on master: ae55a70 Merge branch 'dev'
HEAD is now at ae55a70 Merge branch 'dev'
[root@localhost cmdb]#vim readme 
[root@localhost cmdb]#git stash list
stash@{0}: WIP on master: ae55a70 Merge branch 'dev'
[root@localhost cmdb]#git stash pop
# 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:   readme
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8a4250ab48f5aede36544c52c15d6daa77493365)

5.5 强行删除分支

在有些时候,checkout出一个分支,最后准备合并分支时,发现不需要此功能,而且因保密原因必须要删除此分支,但是当删除此分支时,系统会提醒error: The branch 'dev' is not fully merged因为没有合并,无法删除,此时就要强行删除此分支,通过-D参数

[root@localhost cmdb]#git checkout master
Switched to branch 'master'
[root@localhost cmdb]#git branch -d dev
error: The branch 'dev' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev'.
[root@localhost cmdb]#git branch -D dev
Deleted branch dev (was 8b0709b).

5.6 多人协作

查看远程仓库

有两种方式,一种方式为git remote,另外一种方式为git remote -v

$ git remote
origin
76774@DESKTOP-PS81HPT MINGW64 /d/PycharmProjects/cmdb (master)
$ git remote -v
origin  git@172.16.102.200:my/cmdb.git (fetch)
origin  git@172.16.102.200:my/cmdb.git (push)

推送某一分支
将某一分支推向服务器,这个分支可以是具体的某一个分支
git push origin master
git push origin dev

抓取某一分支
将从服务器端抓取具体某一分支

5.7 标签

打标签
打标签是为了方便记忆commit点,因为默认情况下commit点都是sha1哈希值,哈希值太长无法记忆,因此通过标签的方式进行捆绑,方便操作。

git tag 给当前HEAD指向的commit打标签
git tag -a -m "blablabla..."给指定的commit打标签
git tag查看所有标签
操作标签
标签也可以和远程仓库同步
git push origin ,将某个标签推向远程仓库进行关联
git push origin --tags,将所有标签推向远程仓库进行关联
git tag -d ,将某一个标签删除
git push origin :refs/tags/,将远程的某一个标签进行删除

你可能感兴趣的:(python)