• Git


    Git安装、配置以及文件添加.Git常用指令为add,commit,status,log

    一.Git安装和配置
    1.    在window中安装完成git后,打开“Git bash”,配置用户名
    2.    git config –global user.name “myname”
    git config –global user.email “myemail@xx.xx”
    3.    mkdir gittest 创建一个版本库,在指定地方创建一个名为gittest的空目录。
    4pwd 显示当前目录
    5. git init 把当前目录变成Git可管理的仓库(repository),里面生成一个 .git的目录,用来跟踪管理版本库
    
    MINGW64 /d/Git/gittest
    $ pwd
    /d/Git/gittest
    52136@CQCA52136 MINGW64 /d/Git (master)
    $ cd gittest/
    
    MINGW64 /d/Git/gittest
    $ git init
    Initialized empty Git repository in D:/Git/gittest/.git/
    
    二、添加文件到Git库,两步:add和commit
    1.git add mytest.txt 把mytest.txt文件添加到仓库,mytest.txt文件在gittest目录下,
    mytest.txt内容为一行文字:
    git learn.
    没有消息返回,添加成功。 
    MINGW64 /d/Git/gittest (master)
    $ git add mytest.txt
    2. git  commit –m “Create the test file” 把文件提交到仓库,并添加本次修改的说明
    $ git commit -m "Create the test file"
    [master (root-commit) 65ce3f4] Create the test file
     1 file changed, 1 insertion(+)
     create mode 100644 mytest.txt
    
    三、版本管理;git status查看文件是否有修改,git diff查看修改信息
    将mytest.txt文件修改为如下内容:
    git learn.
    Creted by xx.
    
    
    1.    git status 查看当前文件状态,新修改的文件没有添加到仓库中
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   mytest.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    2git diff 查看对文件所做的修改,
    
    $ git diff
    diff --git a/mytest.txt b/mytest.txt
    index 00388d0..a59e99a 100644
    --- a/mytest.txt
    +++ b/mytest.txt
    @@ -1 +1,2 @@
    -git learn.
     No newline at end of file
    +git learn.
    +Creted by xx.
     No newline at end of file
    
    3.git add
    4. git commit –m “add a new line”
    
    $ git add mytest.txt
    
    $ git commit -m "add a new line"
    [master 8aee487] add a new line
    1    file changed, 2 insertions(+), 1 deletion(-)
    
    再次运行git status 后
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    
    四、版本回退。git log 和git reset命令
    1.git log 查看历史版本信息,由最近版本从上向下显示,添加—pretty=oneline参数简化输出内容
    $ git log
    commit 8aee4877b034b1371bdabe0495ea906ce0ea9371 (HEAD -> master)
    Author: name <name@xxx.com.cn>
    Date:   Mon Nov 13 18:18:11 2017 +0800
    
        add a new line
    
    commit 65ce3f4af21274e46d83a48d57235a01ab420c12
    Author: name <name@xxx.com.cn>
    Date:   Mon Nov 13 18:03:49 2017 +0800
    
        Create the test file
    
    $ git log --pretty=oneline
    8aee4877b034b1371bdabe0495ea906ce0ea9371 (HEAD -> master) add a new line
    65ce3f4af21274e46d83a48d57235a01ab420c12 Create the test file
    
    2git reset –hard HEAD^  回退上一版本, HEAD标识当前版本,HEAD^表示上一版本,HEAD^^倒数第二次版本,65ce3f4af21274e46d83a48d57235a01ab420c12 标识commit id
    $ git reset --hard HEAD^
    HEAD is now at 65ce3f4 Create the test file
    
    MINGW64 /d/Git/gittest (master)
    $ git log
    commit 65ce3f4af21274e46d83a48d57235a01ab420c12 (HEAD -> master)
    Author: name <name@xxx.com.cn>
    Date:   Mon Nov 13 18:03:49 2017 +0800
    
        Create the test file
    3. git reset --hard id回退到指定的版本,id可以只写commit id的前几位,git可自动查找
    $ git reset --hard 8aee
    HEAD is now at 8aee487 add a new line
    4.git reflog 查看版本切换历史,通常在电脑关闭,git窗口关闭后查看切换记录
    $ git reflog
    8aee487 (HEAD -> master) HEAD@{0}: reset: moving to 8aee
    65ce3f4 HEAD@{1}: reset: moving to HEAD^
    8aee487 (HEAD -> master) HEAD@{2}: commit: add a new line
    65ce3f4 HEAD@{3}: commit (initial): Create the test file
    
    五.版本库(Repository)
    工作区gittest目录中的隐藏目录.git为Git的版本库。暂存区(stage/index)保存于.git,Git在创建版本库后自动创建一个master分支,以及指向master的一个HEAD指针。
    Git添加文件的两个步骤,add实际上是把文件修改添加到暂存区(stage),commit提交更改,把暂存区的所有内容提交到当前分支(前述例子中的master)
    六,修改撤销
    1.    尚未add添加到暂存区:git checkout -- mytest.txt 撤销最后一次的修改(尚未add或commit),回到最后一次commit或add的状态
    $ git checkout -- mytest.txt
    
    MINGW64 /d/Git/gittest (master)
    $ cat mytest.txt
    git learn. 
    2.    已add暂存区,尚未commit到版本库;git reset HEAD file 撤销暂存区的修改,两步:回退到HEAD,再checkout。
    $ git add mytest.txt
    
    MINGW64 /d/Git/gittest (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   mytest.txt
    
    
    MINGW64 /d/Git/gittest (master)
    $ git reset HEAD mytest.txt
    Unstaged changes after reset:
    M       mytest.txt
    
    MINGW64 /d/Git/gittest (master)
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   mytest.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    MINGW64 /d/Git/gittest (master)
    $ git checkout -- mytest.txt
    
    MINGW64 /d/Git/gittest (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    七、文件删除
    1.删除一个被add和commit的文件后,git status查看状态,git rm 删除,并且再次git commit,git checkout – deletefile 恢复误删的文件至最新版本
  • 相关阅读:
    A. Playing with Paper
    手摇算法
    perl之创建临时文件夹遇到同名文件该咋办
    B. Two Buttons
    A Pangram
    shell的面试题
    A. Game
    B. Drazil and His Happy Friends
    A. Drazil and Date
    2道阶乘的算法题
  • 原文地址:https://www.cnblogs.com/Pan-Z/p/7828025.html
Copyright © 2020-2023  润新知