zoukankan      html  css  js  c++  java
  • Git学习01 --git add, git commit , git log ,git status, git reset --hard, head

    Git官方提供的快速入门教程:https://try.github.io/levels/1/challenges/1

    特点:Git极其强大的分支管理;分布式版本

    集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器。中央服务器就好比是一个图书馆,你要改一本书,必须先从图书馆借出来,然后回到家自己改,改完了,再放回图书馆。集中式版本控制系统最大的毛病就是必须联网才能工作。

    使用Git

    1.创建版本库

       首先,选择一个合适的地方,创建一个空目录,通过git init命令把这个目录变成Git可以管理的仓库

       

    zhangqulideMacBook-Air:~ zhangquli$ mkdir git_test
    zhangqulideMacBook-Air:~ zhangquli$ cd git_test
    zhangqulideMacBook-Air:git_test zhangquli$ git init
    Initialized empty Git repository in /Users/zhangquli/git_test/.git/
    

      

    2。把文件添加到版本库

      所有的版本控制系统,其实只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外。版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词“Linux”,在第8行删了一个单词“Windows”。而图片、视频这些二进制文件,虽然也能由版本控制系统管理,但没法跟踪文件的变化,只能把二进制文件每次改动串起来,也就是只知道图片从100KB改成了120KB,但到底改了啥,版本控制系统不知道,也没法知道。

      

    把一个文件放到Git仓库只需要两步。

    第一步,用命令git add告诉Git,把文件添加到仓库

    第二步,用命令git commit告诉Git,把文件提交到仓库:

    zhangqulideMacBook-Air:git_test zhangquli$ git add a.txt
    zhangqulideMacBook-Air:git_test zhangquli$ git commit a.txt
    [master (root-commit) 0c9192c] add a file a.txt
    
    #可以使用git commit -m "add a file a.txt“   添加描述
    

      

    3.修改文件

       修改文件后,运行git status命令看看结果:

        

    zhangqulideMacBook-Air:git_test zhangquli$ vi a.txt
    zhangqulideMacBook-Air:git_test zhangquli$ 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:   a.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")#a.txt被修改过了,但还没有准备提交的修改
    

     看看具体修改了什么内容,需要用git diff这个命令看看

      

    zhangqulideMacBook-Air:git_test zhangquli$ git diff a.txt
    diff --git a/a.txt b/a.txt
    index de891d0..14078f4 100644
    --- a/a.txt
    +++ b/a.txt
    @@ -1 +1,2 @@
     heiheiehi
    +changed
    

      

        知道了对readme.txt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步 git add   git commit

    版本回退

        版本控制系统肯定有某个命令可以告诉我们历史记录,在Git中,我们用git log命令查看:

       

    zhangqulideMacBook-Air:git_test zhangquli$ git log
    commit 8ed1c1a1d5666a98d2464787177f61e2a23126a6   #版本号
    Author: zqlmmd <zhangquli@zhangqulideMacBook-Air.local>
    Date:   Sun May 29 23:46:24 2016 +0800
    
        add a line   #commit的描述
    
    commit 0c9192cfde234fc37b351f959cc791ebb1c3f68a
    Author: zqlmmd <zhangquli@zhangqulideMacBook-Air.local>
    Date:   Sun May 29 23:29:35 2016 +0800
    
        add a file a.txt
    

       我们要把当前版本回退到上一个版本,首先要知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。然后使用git reset命令  --hard参数

    zhangqulideMacBook-Air:git_test zhangquli$ git reset --hard HEAD^
    HEAD is now at 0c9192c add a file a.txt
    
    zhangqulideMacBook-Air:git_test zhangquli$ git log
    commit 0c9192cfde234fc37b351f959cc791ebb1c3f68a
    Author: zqlmmd <zhangquli@zhangqulideMacBook-Air.local>
    Date:   Sun May 29 23:29:35 2016 +0800
    
        add a file a.txt
    

      再回去

    $ git reset --hard 3628164   
    HEAD is now at 3628164 append GPL
    

        

  • 相关阅读:
    最大生成树
    Codeforces#363 Div2
    AOJ2249最短路+最小费用
    Codeforces#364Div2
    POJ3268Dijkstra
    POJ3259负环判定
    Codeforces#362
    POJ3169差分约束系统
    POJ3723最小生成树
    hdu 4038 2011成都赛区网络赛H 贪心 ***
  • 原文地址:https://www.cnblogs.com/zqlmmd/p/5540882.html
Copyright © 2011-2022 走看看