zoukankan      html  css  js  c++  java
  • GitHub-版本管理

    参考博文:廖雪峰Git教程

     

     

    1. 管理修改

           现在,假定你已经完全掌握了暂存区的概念。下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。

           你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。

           为什么说Git管理的是修改,而不是文件呢?我们还是做实验。第一步,对README.txt做一个修改,比如加一行内容:

     1 [root@mini05 zhangtest]# cat README.md 
     2 # zhangtest
     3 zhangtest
     4 张三
     5 Git is a distributed version control system.
     6 Git is free software.
     7 111
     8 222
     9 Git tracks changes.  # 添加的内容
    10 [root@mini05 zhangtest]# git add README.md
    11 [root@mini05 zhangtest]# git  status
    12 # On branch master
    13 # Your branch is ahead of 'origin/master' by 3 commits.
    14 #   (use "git push" to publish your local commits)
    15 #
    16 # Changes to be committed:
    17 #   (use "git reset HEAD <file>..." to unstage)
    18 #
    19 #    modified:   README.md
    20 #

           然后,再修改readme.txt:

    1 [root@mini05 zhangtest]# cat README.md 
    2 # zhangtest
    3 zhangtest
    4 张三
    5 Git is a distributed version control system.
    6 Git is free software.
    7 111
    8 222
    9 Git tracks changes of files.  # 之前行字符串,换为了这个

           之后提交:

    1 [root@mini05 zhangtest]# git commit -m "git tracks changes"
    2 [master b293c46] git tracks changes
    3  1 file changed, 1 insertion(+)

           提交后,再看看状态:

     1 [root@mini05 zhangtest]# git status
     2 # On branch master
     3 # Your branch is ahead of 'origin/master' by 4 commits.
     4 #   (use "git push" to publish your local commits)
     5 #
     6 # Changes not staged for commit:
     7 #   (use "git add <file>..." to update what will be committed)
     8 #   (use "git checkout -- <file>..." to discard changes in working directory)
     9 #
    10 #    modified:   README.md
    11 #
    12 no changes added to commit (use "git add" and/or "git commit -a")

           怎么第二次的修改没有被提交?

           别激动,我们回顾一下操作过程:

           第一次修改 -> git add -> 第二次修改 -> git commit ,中间缺少了一个git add

           我们前面讲了,Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。

           提交后,用git diff HEAD -- README.txt命令可以查看工作区和版本库里面最新版本的区别:

     1 [root@mini05 zhangtest]# git diff HEAD -- README.md
     2 diff --git a/README.md b/README.md
     3 index 8de2e69..3b892c6 100644
     4 --- a/README.md
     5 +++ b/README.md
     6 @@ -5,4 +5,4 @@ Git is a distributed version control system.
     7  Git is free software.
     8  111
     9  222
    10 -Git tracks changes.
    11 +Git tracks changes of files.

           可见,第二次修改确实没有被提交。

           那怎么提交第二次修改呢?你可以继续git addgit commit,也可以别着急提交第一次修改,先git add第二次修改,再git commit,就相当于把两次修改合并后一块提交了:

           第一次修改 -> git add -> 第二次修改 -> git add -> git commit

    2. 撤销修改

    2.1. 场景1

           当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

    添加了如下信息:

     1 [root@mini05 zhangtest]# cat README.md 
     2 # zhangtest
     3 zhangtest
     4 张三
     5 Git is a distributed version control system.
     6 Git is free software.
     7 111
     8 222
     9 Git tracks changes of files.
    10 aabbccdd   # 添加的信息

    查看状态信息

     1 [root@mini05 zhangtest]# git status
     2 # On branch master
     3 # Your branch is ahead of 'origin/master' by 4 commits.
     4 #   (use "git push" to publish your local commits)
     5 #
     6 # Changes not staged for commit:
     7 #   (use "git add <file>..." to update what will be committed)
     8 #   (use "git checkout -- <file>..." to discard changes in working directory)
     9 #
    10 #    modified:   README.md
    11 #
    12 no changes added to commit (use "git add" and/or "git commit -a")

    丢弃工作区的修改并再次查看信息

     1 [root@mini05 zhangtest]# git checkout -- README.md  # 丢弃工作区的修改
     2 [root@mini05 zhangtest]# cat README.md 
     3 # zhangtest
     4 zhangtest
     5 张三
     6 Git is a distributed version control system.
     7 Git is free software.
     8 111
     9 222
    10 Git tracks changes.
    11 [root@mini05 zhangtest]# git status
    12 # On branch master
    13 # Your branch is ahead of 'origin/master' by 4 commits.
    14 #   (use "git push" to publish your local commits)
    15 #
    16 nothing to commit, working directory clean

    说明:这里有两种情况

           一种是README.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

           一种是README.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

           总之,就是让这个文件回到最近一次git commitgit add时的状态。

           git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令。

    2.2. 场景2

           你不但写了一些废话,还git add到暂存区了。

           用命令git reset HEAD <file>可以把暂存区的修改撤销掉(unstage),重新放回工作区:

           git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。

    增加信息并添加到暂存区

     1 [root@mini05 zhangtest]# cat README.md 
     2 # zhangtest
     3 zhangtest
     4 张三
     5 Git is a distributed version control system.
     6 Git is free software.
     7 111
     8 222
     9 Git tracks changes.
    10 aabbccdd  # 添加的信息
    11 [root@mini05 zhangtest]# git add README.md  # 添加到暂存区

    查看状态信息

     1 [root@mini05 zhangtest]# git status
     2 # On branch master
     3 # Your branch is ahead of 'origin/master' by 4 commits.
     4 #   (use "git push" to publish your local commits)
     5 #
     6 # Changes to be committed:
     7 #   (use "git reset HEAD <file>..." to unstage)
     8 #
     9 #    modified:   README.md
    10 #

    回到最新版本

    1 [root@mini05 zhangtest]# git reset HEAD README.md
    2 Unstaged changes after reset:
    3 M    README.md

    再次查看状态信息

           再用git status查看一下,现在暂存区是干净的,工作区有修改:

     1 [root@mini05 zhangtest]# git status
     2 # On branch master
     3 # Your branch is ahead of 'origin/master' by 4 commits.
     4 #   (use "git push" to publish your local commits)
     5 #
     6 # Changes not staged for commit:
     7 #   (use "git add <file>..." to update what will be committed)
     8 #   (use "git checkout -- <file>..." to discard changes in working directory)
     9 #
    10 #    modified:   README.md
    11 #
    12 no changes added to commit (use "git add" and/or "git commit -a")

    丢弃工作区的修改

     1 [root@mini05 zhangtest]# git checkout -- README.md  
     2 [root@mini05 zhangtest]# git status
     3 # On branch master
     4 # Your branch is ahead of 'origin/master' by 4 commits.
     5 #   (use "git push" to publish your local commits)
     6 #
     7 nothing to commit, working directory clean
     8 [root@mini05 zhangtest]# cat README.md 
     9 # zhangtest
    10 zhangtest
    11 张三
    12 Git is a distributed version control system.
    13 Git is free software.
    14 111
    15 222
    16 Git tracks changes.

    说明:

           现在,假设你不但改错了东西,还从暂存区提交到了版本库,怎么办呢?还记得版本回退一节吗?可以回退到上一个版本。不过,这是有条件的,就是你还没有把自己的本地版本库推送到远程。一旦你提交推送到远程版本库,你就真的惨了……

    2.3. 小结

           场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

           场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD <file>,就回到了场景1,第二步按场景1操作。

           场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。

    3. 删除文件

    3.1. 删除文件

    添加文件并提交

    1 [root@mini05 zhangtest]# echo "111" > aaa.txt
    2 [root@mini05 zhangtest]# git add aaa.txt
    3 [root@mini05 zhangtest]# git commit -m "add aaa.txt" 
    4 [master 6d0226b] add aaa.txt
    5  1 file changed, 1 insertion(+)
    6  create mode 100644 aaa.txt

    本地删除文件并查看状态

     1 [root@mini05 zhangtest]# rm -f aaa.txt # 本地删除 
     2 [root@mini05 zhangtest]# git status 
     3 # On branch master
     4 # Your branch is ahead of 'origin/master' by 5 commits.
     5 #   (use "git push" to publish your local commits)
     6 #
     7 # Changes not staged for commit:
     8 #   (use "git add/rm <file>..." to update what will be committed)
     9 #   (use "git checkout -- <file>..." to discard changes in working directory)
    10 #
    11 #    deleted:    aaa.txt
    12 #
    13 no changes added to commit (use "git add" and/or "git commit -a")

    git 删除文件并查看状态

     1 [root@mini05 zhangtest]# git rm aaa.txt
     2 rm 'aaa.txt'
     3 [root@mini05 zhangtest]# git commit -m "del aaa.txt"
     4 [master c795cfc] del aaa.txt
     5  1 file changed, 1 deletion(-)
     6  delete mode 100644 aaa.txt
     7 [root@mini05 zhangtest]# git status 
     8 # On branch master
     9 # Your branch is ahead of 'origin/master' by 6 commits.
    10 #   (use "git push" to publish your local commits)
    11 #
    12 nothing to commit, working directory clean

           现在,文件就从版本库中被删除了。

    3.2. 误删恢复

           如果误删除本地文件了,但是没有删除版本库中的,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本.

    1 [root@mini05 zhangtest]# git checkout -- aaa.txt 

      

           git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

    3.3. 小结

           命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

  • 相关阅读:
    DataGridView 中的复选框DataGridViewCheckBoxColumn 添加验证
    DataGridView删除行 提示 索引-1没有值 索引0没有值 等 解决办法
    convert mov mp4 to jpg
    拼图
    remove ad of chrome how to block ad of newssysstem.net
    redis3常用命令
    python_爬虫总结
    boot_自定义异常
    简答题总结
    springmvc_文件上传
  • 原文地址:https://www.cnblogs.com/zhanglianghhh/p/9739154.html
Copyright © 2011-2022 走看看